Original Post
I am writing a C++ application where i have an object model and a UI (as most applications do). For the model, i have a main object and a number of specialised subclasses. The UI has context menus and property dialogs that i want to be specific to each object type. The main object has a number of actions that are available in the menu, but the subclasses need to add menu items (actions). And similarly for the property dialog - it needs to display data specific to each subclass.
My question is what is a good way to design this?
I can put all the menu creation code in each class, so that the specialised classes override the function to give their own menus. Like this (pseudo-code):
But that seems like the model classes are doing too much. They shouldn't be responsible for creating and managing UI objects.
So maybe i could create a separate object hierarchy for creating the menus. Each class would be responsible for creating the menu for it's respective model object. Something like this:
But then i'm not sure where the logic for performing each menu action should go. Since each action operates on the model object, those MenuController classes above would need to know what object the action is for.
I am using Qt, so i could use their powerful signals and slots mechanism to do this, but i'm not quite sure how.
Does anyone have an idea of what Object Oriented design patterns to use here? I can provide more detail if my problem is unclear.
thanks
My question is what is a good way to design this?
I can put all the menu creation code in each class, so that the specialised classes override the function to give their own menus. Like this (pseudo-code):
class MainObject {
virtual MenuList getMenus() {
return Menu("do something"),
Menu("do something else"),
Menu("do a third thing"),
}
};
class SpecialObjectA : public MainObject {
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu A");
}
};
class SpecialObjectB : public MainObject {
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu B");
}
};
But that seems like the model classes are doing too much. They shouldn't be responsible for creating and managing UI objects.
So maybe i could create a separate object hierarchy for creating the menus. Each class would be responsible for creating the menu for it's respective model object. Something like this:
class MainMenuController {
MainObject* theObject;
virtual MenuList getMenus() {
return Menu("do something"),
Menu("do something else"),
Menu("do a third thing"),
}
};
class SpecialMenuControllerA : public MainMenuController {
SpecialObjectA* theObject;
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu A");
}
};
class SpecialMenuControllerB : public MainMenuController {
SpecialObjectB* theObject;
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu B");
}
};
class MainObject {
virtual MenuList getMenus() {
return new MainMenuController()->getMenus();
}
};
class SpecialObjectA : public MainObject {
virtual MenuList getMenus() {
return new SpecialMenuControllerA()->getMenus();
}
};
class SpecialObjectB : public MainObject {
virtual MenuList getMenus() {
return new SpecialMenuControllerB()->getMenus();
}
};
But then i'm not sure where the logic for performing each menu action should go. Since each action operates on the model object, those MenuController classes above would need to know what object the action is for.
I am using Qt, so i could use their powerful signals and slots mechanism to do this, but i'm not quite sure how.
Does anyone have an idea of what Object Oriented design patterns to use here? I can provide more detail if my problem is unclear.
thanks