0% found this document useful (0 votes)
26 views

Mediator Pattern

The Mediator pattern defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly and allows their interaction to vary independently. Some key points: - A mediator object manages the interaction between colleagues (e.g. dialog box objects), simplifying many-to-many communication to one-to-many. - It limits required subclassing to extend behavior and increases reusability by decoupling objects. - While it centralizes control and can become complex, it also abstracts cooperation between objects and makes their interactions easier to understand.

Uploaded by

amirreza alasti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Mediator Pattern

The Mediator pattern defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly and allows their interaction to vary independently. Some key points: - A mediator object manages the interaction between colleagues (e.g. dialog box objects), simplifying many-to-many communication to one-to-many. - It limits required subclassing to extend behavior and increases reusability by decoupling objects. - While it centralizes control and can become complex, it also abstracts cooperation between objects and makes their interactions easier to understand.

Uploaded by

amirreza alasti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Mediator Design Pattern

Dr. Abbas Rasoolzadegan


Problem

• Object oriented design encourages the distribution of behavior


among the various objects
• increasing reusability (high cohesion)
• decreasing reusability (high coupling) through
• proliferating interactions
• every object knows about every other
• changing the system’s behavior will be difficult

1
Problem (cont.)
Set of strongly interacting objects
 Interdependencies are unstructured and
difficult to understand.

 Distributed behavior between several


classes can not be customized or
extended without a lot of work, e.g. by
sub classing all participating objects.

 Participants can not be used in other


contexts – no reusability
2
Motivation: Dialog Box Example

3
Solution: Mediator Pattern
 Intent:

Define an object that encapsulates how a set of objects


interact. Mediator promotes loose coupling by keeping objects
from referring to each other explicitly, and it lets you vary
their interaction independently.

 Classification: Object Behavioral.

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 abstracts how objects cooperate


 Making mediation an independent concept and encapsulating it in an object
lets you focus on how objects interact apart from their individual behavior

 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

 Increased complexity of the  Omitting the abstract mediator


mediator. class

 Mediator becomes a monolithic  When colleagues work with only

structure that is hard to maintain. one mediator

 Implement Colleague-Mediator
communication using
 observer pattern

15
Dialog Box Example

16
Class Diagram

17
Sample code
class DialogDirector { class Widget {

public: public:

virtual ~DialogDirector(); Widget(DialogDirector*);

virtual void ShowDialog(); virtual void Changed();

virtual void virtual void


WidgetChanged(Widget*) = 0; HandleMouse(MouseEvent& event);

protected: // ...

DialogDirector(); private:

virtual void CreateWidgets() = 0; DialogDirector* _director;

}; };

void Widget::Changed() {

_director->WidgetChanged(this); }

18
Sample code (cont.)
class ListBox : public Widget {

public:

ListBox(DialogDirector*);

virtual const char *GetSelection();

virtual void SetList(List<char*>* listItems);

virtual void HandleMouse(MouseEvent& event); // ...

};

class EntryField : public Widget {

public:

EntryField(DialogDirector*);

virtual void SetText(const char* text);

virtual const char* GetText();

virtual void HandleMouse(MouseEvent& event); // ...

};

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

You might also like