Skip to content

Latest commit

 

History

History
48 lines (30 loc) · 1.84 KB

File metadata and controls

48 lines (30 loc) · 1.84 KB

Intend

As stated in GoF, p273 :

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from refereing to each other explicitly, an it lets you vary their interaction independently.

In other words, make mediation between various items an independent concept by introducing an object that deals with it.

How it's done

UML

Participants

  • Mediator : Interface for communicating with Colleague objects.
  • ConcreteMediator: Implements Mediator interface by coordinating Colleague objects. Knows and maintains Colleagues.
  • Colleagues classes :
    • Each instance has a reference to its Mediator object (or knows about it in some way).
    • Communicates with other Colleague using Mediator only.

How to implement

  1. Identify a collection of objects that would benefit from mutual decoupling.
  2. Encapsulate those interactions in the abstraction of the Mediator.
  3. Create your Mediator and rework all "peer" objects to interact with it only.
  4. Be careful not to create a God object.

Note : UML class diagram taken from here

Pros & cons

Pros

  • Decouples colleagues : Promotes loose coupling between colleagues.
  • Abstracts how objects interact
  • Centralizes control
  • Simplify objects protocol (from many to many to many to one.)

Cons

  • The complexity that previously was in the interaction between objects is now in the Mediator. This can produce a complex object.

Notes

Here are some usefull ressources :