The 23 GoF design patterns and Dependency Injection – choose a pattern Abstract Factory design patternBuilder design patternFactory Method design patternPrototype design patternSingleton design patternAdapter design patternBridge design patternComposite design patternDecorator design patternFacade design patternFlyweight design patternProxy design patternChain of Responsibility design patternCommand design patternInterpreter design patternIterator design patternMediator design patternMemento design patternObserver design patternState design patternStrategy design patternTemplate Method design patternVisitor design patternDependency Injection design pattern

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 Mediator object that controls and coordinates the interaction.
  • Define a separate Mediator object:
    • 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.
  • 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 new Mediator (sub)classes.
  • Colleagues delegate interaction to a Mediator object (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 Mediator interface.
    • Maintain explicit references (colleague1,colleague2,…) to colleagues. 
  • Colleague1,Colleague2,…
    • Refer to the Mediator interface instead of referring to each other directly.

Dynamic Object Collaboration

  • In this sample scenario, a Mediator1 object mediates (controls and coordinates) the interaction between Colleague1 and Colleague2 objects (to synchronize their state, for example).
    Let's assume that Colleague1 and Colleague2 are configured with a Mediator1 object.
  • Let's assume that the state of Colleague1 changes, which causes Colleague1 to call mediate(this) on its Mediator1 object.
  • Colleague1 passes itself (this) to the Mediator1 so that Mediator1 can call back and get the changed data.
  • The Mediator1 gets the changed data from Colleague1 and performs an action2() on Colleague2.
  • Thereafter, assuming that the state of Colleague2 changes, Colleague2 calls mediate(this) on its Mediator1.
  • The Mediator1 now gets the changed data from Colleague2 and performs an action1() on Colleague1.
  • 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.