I'm curious if there is a pattern that allows an object to change behavior based on context. I have looked at the State pattern, but would like to explain what i'm trying to do and see if there is maybe not a better way of achiving the same.
I am creating a webpage that loads menu items in a div on a page dynamically based on the context of the div. Each menu item is an object that I have created. Certain menu items are reused across different pages (example "add new") , but should behave differently on different divs.
I have an Abstract Employee class, and then Director Concrete Class, Manager Concrete Class ect.
I also have an Abstract MenuItem Class, and then AddNewMenuItem Concrete Class, RemoveMenuItem Concrete Class.
Both Concrete Employee classes loads AddNewMenuItem classes, but when the AddNewMenuItem is clicked, it should add a New Director or add a New Manager, based on the div it was loaded in.
What would be the best way to accomplish this?
Andre Smith, Jun 28, 2010
these are the codes for my answer:
public class ManagerMenuAction : IMenuAction
#region IMenuAction 成员
public void Add()
public void Remove()
public class MenuActionController
public void AddMenuAction(string key, IMenuAction action)
public void SetCurrentMenuAction(string key)
public class Invoker
Shmilya Golem, Jul 08, 2010
you can do it like this:
abstract a inerface contains all the actions in menus.
then create concrete class(Employee's /Director's and so on) implement this interface.
create a controller class,it has a interface container that could save the concrete class who implement the interface and has a method to set the current-menuaction.
Shmilya Golem, Jul 08, 2010
Hi Ian, Thank you for replying
That is exactly what i am doing, I have a menuFactory that returns a list of context-sensitive menu items, the problem is still letting each menu item do something differently when clicked, based on the context.
Loading an Employee would use the menuFactory to Load menu items ("Add","Remove","Refresh")
Loading a Director would use the menuFactory to Load menu items ("Add", "Remove", "Refresh", "Pay")
Loading a Manager would use the menuFactory to load menu items ("Add", "Remove", "Refresh", "Manage")
As you can see the menu items "Add", "Remove" and "Refresh" is common throughout all concrete classes.
Yet when I click on "Add", it needs to add an entity based on the concrete class
So the challange i'm facing is making the AddMenuItem object, behave differently, based on context.
Andre Smith, Jul 01, 2010
I am not quite sure whether I understand your setup, but I get a sense it is a bit over-architected.
Why not give the base class of the Context (the Employee, Director, Manager) a virtual CreateMenus method and let each derived type implement this. Or, if you don't want to pollute the Context objects, build a static MenuFactory class with a Create method. Pass in the Context (in the Create method) and out comes a context-sentive list of menu items.
Ian Barrymore, Jun 30, 2010