Mediator Pattern
Mediator Pattern
1
Problem (cont.)
Set of strongly interacting objects
Interdependencies are unstructured and
difficult to understand.
3
Solution: Mediator Pattern
Intent:
4
Mediator Pattern
Advantages:
Limits required sub classing to extend:
when extending, subclass only the
mediator.
Decouples objects: reuse.
Simplifies communication: many-to-
one instead of many-to-many.
Co-operation abstraction encapsulated
to an object: promotes
understandability.
Disadvantage:
Centralized control: mediator may
become monolithic.
5
Interactions between the objects of a dialog box through a mediator
6
Sequence Diagram
7
Class Diagram
8
Applicability
Use the Mediator pattern when:
A set of objects communicate in well-defined but complex
ways. The resulting interdependencies are unstructured and
difficult to understand.
Reusing an object is difficult because it refers to and
communicates with many other objects.
A behavior that's distributed between several classes should
be customizable without a lot of subclassing.
9
Structure
10
A typical object structure
11
Participants
Mediator
Defines an interface for communicating with Colleague objects.
ConcreteMediator
Implements cooperative behavior by coordinating Colleague
objects.
Knows and maintains its colleagues.
Colleague classes
Each Colleague class knows its Mediator object.
Each colleague communicates with its mediator whenever it
would have otherwise communicated with another colleague.
12
Consequences
It limits subclassing
A mediator localizes behavior that otherwise would be
distributed among several objects.
Changing this behavior requires subclassing Mediator only.
Colleague classes can be reused as is.
It decouples colleagues
You can vary and reuse Colleague and Mediator classes
independently.
13
Consequences (cont.)
It simplifies object protocols
A mediator replaces many-to-many interactions with one-to-many
interactions between the mediator and its colleagues.
It centralizes control
The Mediator pattern trades complexity of interaction for complexity in the
mediator.
Because a mediator encapsulates protocols, it can become more complex
than any individual colleague.
14
Disadvantage Implementation Issues
Implement Colleague-Mediator
communication using
observer pattern
15
Dialog Box Example
16
Class Diagram
17
Sample code
class DialogDirector { class Widget {
public: public:
protected: // ...
DialogDirector(); private:
}; };
void Widget::Changed() {
_director->WidgetChanged(this); }
18
Sample code (cont.)
class ListBox : public Widget {
public:
ListBox(DialogDirector*);
};
public:
EntryField(DialogDirector*);
};
19
Sample code (cont.)
class Button : public Widget {
public: Button(DialogDirector*);
virtual void SetText(const char* text);
virtual void HandleMouse(MouseEvent& event); // ...
};
void Button::HandleMouse(MouseEvent& event) { // ...
Changed();
}
class FontDialogDirector : public DialogDirector {
public: FontDialogDirector();
virtual ~FontDialogDirector();
virtual void WidgetChanged(Widget*);
protected: virtual void CreateWidgets();
private: Button* _ok;
Button* _cancel;
ListBox* _fontList;
EntryField* _entryField;
};
20
Sample code (cont.)
Void FontDialogDirector::CreateWidgets() {
_ok = new Button(this);
_cancel = new Button(this);
_fontList = new ListBox(this);
_entryField = new EntryField(this);
// assemble the widgets in the dialog }
void FontDialogDirector::WidgetChanged(Widget* theChangedWidget){
if (theChangedWidget == _fontList) {
_entryField -> SetText(_fontList->GetSelection());
} else if (theChangedWidget == _ok) {
// apply font change and dismiss dialog
} else if (theChangedWidget == _cancel) {
// dismiss dialog
}
}// WidgetChanged ensures that the widgets work together properly
21
Related Patterns
Facade: Abstracts a subsystem of objects providing a more
convenient interface
Difference with mediator:
• Facade is an structural pattern but mediator is a behavioral pattern
• In Facade, protocol is unidirectional as opposed to mediator. Mediator is
a “two-way” version of facade
• Facade promotes weak coupling between the subsystem classes and its
client but mediator promotes weak coupling between a set of objects
• Facade shields clients from subsystem components, but mediator shields
objects of a set from each other
Observer: Colleagues can communicate with the mediator using
the Observer pattern.
22