Tuesday, August 3, 2010
Assingment: Case Study 11
If you have used any UI application, you would have seen that one control depends on the selection of the other. So let us say that you have three combo boxes Country, state and city on a webpage or a dialog. Country and state acts as filter for the city combo box. If the Country combo is changed the state and City combo has to be repopulated. How would you model this behavior? One possible solution is that City combo can register a listener for change in selection with Country and State Combo, what problems do you foresee? You can think of having 10 such controls that depend on each other to come up with a solution.
Subscribe to:
Post Comments (Atom)
We can use mediator pattern to solve this problem.
ReplyDeleteClass Mediator
{
public:
void CountryChanged();
void StateChanged();
}
Class CountryControl{
private:
Mediator *m_pMediator;
public:
void EventHandlerSelectionChanged();
};
void CountryControl::EventHandlerSelectionChanged()
{
m_pMediator->CountryChanged();
}
Benifits over Observer Pattern:
1. There is a centralized Control. We need not change many classes to change the handling of events. It can be done only by changing the mediator.
2. loose coupling
We can further define subclasses of the mediator to make the design further understandable.