Mediator Design Pattern
Behavioral pattern. Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
The Mediator design pattern solves problems like: How can tight coupling between a set of interacting objects be avoided?
Open in the interactive referenceDownload the PDF reference
Solution
The Mediator design pattern provides a solution:
Define a separate Mediator
object that encapsulates how a set of objects interact.
Objects interact with a
Mediator object
instead of interacting with each other directly.
Describing the Mediator design in more detail is the
theme of the following sections.
See Applicability section for all problems Mediator can solve.
-
The key idea in this pattern is to let objects
interact with each other indirectly through a
common
Mediatorobject that controls and coordinates the interaction. -
Define a separate
Mediatorobject:-
Define an interface for interacting with colleagues
(
Mediator | mediate()). -
Define classes (
Mediator1,…) that implement the interaction behavior by controlling and coordinating the interaction between colleagues.
-
Define an interface for interacting with colleagues
(
-
This enables compile-time flexibility (via class inheritance).
New colleagues can be added and the interaction behavior of existing ones can be changed independently by defining newMediator(sub)classes. -
Colleagues delegate interaction
to a
Mediatorobject (mediator.mediate()). -
This makes colleagues loosely coupled
because they only refer to and know about their mediator
and have no explicit
knowledge of each other.
"[…] each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague." GoF [p277]
Applicability
Design Problems
-
Avoiding Tight Coupling Between Interacting Objects
- How can tight coupling between a set of interacting objects be avoided?
-
How can the interaction
between a set of objects
be changed independently from the objects?
Structure
Static Class Structure
-
Mediator- Defines an interface for controlling and coordinating the interaction among colleagues.
-
Mediator1,…-
Implement the
Mediatorinterface. -
Maintain explicit references
(
colleague1,colleague2,…) to colleagues.
-
Implement the
-
Colleague1,Colleague2,…-
Refer to the
Mediatorinterface instead of referring to each other directly.
-
Refer to the
Dynamic Object Collaboration
-
In this sample scenario,
a
Mediator1object mediates (controls and coordinates) the interaction betweenColleague1andColleague2objects (to synchronize their state, for example).
Let's assume thatColleague1andColleague2are configured with aMediator1object. -
Let's assume that the state of
Colleague1changes, which causesColleague1to callmediate(this)on itsMediator1object. -
Colleague1passes itself (this) to theMediator1so thatMediator1can call back and get the changed data. -
The
Mediator1gets the changed data fromColleague1and performs anaction2()onColleague2. -
Thereafter,
assuming that the state of
Colleague2changes,Colleague2callsmediate(this)on itsMediator1. -
The
Mediator1now gets the changed data fromColleague2and performs anaction1()onColleague1. -
See also Sample Code / Example 1.
Not included on this page: Intent, Problem, Motivation, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.