0

DisplayEngine has a list of DisplayableObjects. Each DisplayableObject derived class uses a different set of data. So I have created a base class for data so that I can pass BaseData into the update for each DisplayableObject without having to know anything about derived classes of DisplayableObject and BaseData. Then in each derived DisplayableObject update function I cast to the right type of data.

The problem is this is a semantic coupling. If I pass the wrong derived data class to one of the derived DisplayableObject classes this blows up, and really the update function really CAN’T handle ALL BaseData classes like it appears to on the outside.

Essentially what is happening here is Module1 passes BaseObject to Module2. Because Module2 knows that Module2 is really passing it DerivedObject, it casts BaseObject to DerivedObject and uses data that is specific to DerivedObject.

The trouble is, I can’t figure out a way to do this any other way. How can I have a list of DisplayableObjects that each take a different set of data and have the DisplayEngine know nothing about any of the derived classes so that I can reuse the DisplayEngine in another project?

This is a bit complicated, so thank you in advance for taking a look at it.

Class DisplayEngine{
    DisplayableObject displayableObjectsList[10];
    BaseData *dataList[10];
    // Each element in data list is updated somewhere else.

    void UpdateAll(){
        for(int i=0; i<10; i++){
            displayableObjectsList[i].Update(dataList[i]);
        }
    }
}

Class DisplayableObject{
    virtual void Update(BaseData bData);
}

Class BaseData {
    //empty.
}

Class Data1 : BaseData{
    String b;
}

Class Data2: BaseData{
    int a;    
}

Class DisplayableObject1: DisplayableObject{
    void Update(BaseData bData){
        Data1* d = (Data1*) bData;
        //Do Work with d, can use d.b
    }
}

Class DisplayableObject2: DisplayableObject{
    void Update(BaseData bData){
        Data2* d = (Data2*) bData;
        //Do Work with d, can use d.a
    }
}
Ganamede
  • 13
  • 2

1 Answers1

0

Try to use the Visitor pattern. you can read all about it here

if you have more question or you need additional example, feel free to ask.

RonenKr
  • 213
  • 1
  • 7
  • Thank you for your response. I haven't heard about the visitor pattern before and it's very interesting although I'm not sure it will work here. In this pattern, the visitor needs to implement a method for each element type. In my case, my list of displayableObjects doesn't need to do something different with each baseData derived type, it only deals with one of them. Each displayableObject will display the data from one baseData derived data class. I may, however, be able to use some of the concepts in the visitor pattern in a solution here, I'll have to spend more time with it. Thanks again! – Ganamede Oct 15 '13 at 18:50
  • Just implement an empty implementation in the base and override the one method fly hat handles your specific data type. – RonenKr Oct 15 '13 at 19:10
  • Right, that could work! The only problem I can foresee with that is on larger projects where you have a large number of displayableObjects and therefore data classes, you will end up having to modify each one every time you add a new one. This would make adding a new display object very time-consuming. Suggestions? – Ganamede Oct 15 '13 at 19:57
  • All you have to do is add forward declaration and an empty implementation in the base. But you are right and constantly updating the base class is the down side of the pattern. – RonenKr Oct 16 '13 at 02:15