I have a display object that displays a list of Users and provides a menu for acting on that list – adding new users, adding them to groups, deleting them, etc. Right now the display configures its own menu, so it can add a menu command like doCreateNewUsers(). Unfortunately, this means that every instance of the display always has the “create new” option.
I want to configure the menu differently for different instances of the display – in the “Users” tab, it should include the “create new” option, and in the “Groups” tab, it shouldn’t. My first thought was to externalize the menu, so that I could configure it differently. The problem is that then I lose the ability to call the private doCreateNewUsers() function!
Is there a design pattern for this situation? I don’t like the idea of making doCreateNewUsers public because it shows a dialog that shouldn’t be triggered by external classes. I could make the display abstract, so that I could define the menu in anonymous subclasses, but that kind of messes up the way I reuse widgets right now – I’d like to configure the menu after the display has been created and initialized. I’m hoping there’s some industry-standard way of dealing with this!
It sounds to me like you could possibly use the Strategy Pattern in this case.