vendredi 6 janvier 2017

Is Composition pattern good choice in this scenario?

Here is one design dilemma I have...

In my program, I have different kind of entries - numeric, textual, date, image, etc.

My first idea was to have model structured with inheritance like this:

Entry 
    -- NumericEntry (extends Entry)
    -- TextualEntry (extends Entry)
    -- DateEntry (extends Entry)
    -- ImageEntry (extends Entry)

Then I can have a list of Entry objects, and each object will know how to handle & expose its data through common members (i.e. showData(), makeSummary() etc.) If I want to add new Entry object, I will just add another class with that specific type.

But, java limitations, and also android orm libraries limitations makes this pretty complicated.

So, I have turned to composite pattern, but I am not sure if I am approaching it right.

So, now I have this (pseudocode):

 class Entry
    {
    Type type;
    (nullable)NumericEntry  numericEntry;
    (nullable)TextualEntry textualEntry;
    (nullable)DateEntry dateEntry;
    (nullable)ImageEntry imageEntry;

    public showData() 
    { 
        swicth (type) 
        {
          case numeric: ..
          case textual: ..
          case date: ..
          case image: ..
         }
    }
}

But this seems to me too wired, doesn't it? What would be right approach in the described scenario?

Aucun commentaire:

Enregistrer un commentaire