Card 01/ 08
Mediator Pattern: Routing Communication Through One Coordinator
Five form fields that each update the others need ten links; with a mediator they need five.
The Mediator pattern puts one object between a group of objects that would otherwise talk to each other directly. Each object, called a colleague, tells the mediator when something happens, and the mediator decides which other colleagues to update. This card covers the problem that calls for it, the intent, how to count what it removes, and when it fits.
The problem is many objects referring to each other. In a form with five fields where each can affect the others, every field holds references to the fields it updates. A field cannot be reused in another form without bringing its neighbours along, and a new rule, such as "the book button is disabled while no seats are left", is spread across whichever fields it touches.
With n colleagues that can each talk to every other, there is one link per pair, which is n(n-1)/2: 10 for five fields, 28 for eight. Through a mediator there are n. The saving grows quickly, because the direct count grows with the square of n.
The Gang of Four state the intent as: define an object that encapsulates how a set of objects interact, which promotes loose coupling by keeping objects from referring to each other explicitly, and lets you vary their interaction independently.
Mediator is one of eleven behavioural patterns, the family concerned with which object does which part of the work, and how objects communicate.
| Pattern | What it does |
|---|---|
| Chain of Responsibility | Passes a request along a chain of handlers until one handles it. |
| Command | Turns a request into an object that can be stored, queued or undone. |
| Interpreter | Represents a small language's grammar as classes and evaluates its sentences. |
| Iterator | Visits the elements of a collection without exposing how it is stored. |
| Mediator (this topic) | Routes the communication between objects through one central object. |
| Memento | Captures an object's state so it can be restored later. |
| Observer | Notifies many objects when one object changes. |
| State | Changes an object's behaviour when its internal state changes. |
| Strategy | Swaps one algorithm for another behind a common interface. |
| Template Method | Fixes an algorithm's steps in a base class and lets subclasses fill some in. |
| Visitor | Adds operations to a class hierarchy without changing its classes. |
Use it when
- Several objects update each other in ways that are hard to follow, such as the fields of a form.
- The objects should be reusable elsewhere, which their references to each other prevent.
- The rules of how they interact change more often than the objects themselves.
Avoid it when
- Two or three objects with one simple interaction, where a direct call is clearer.
- The interaction is one object announcing a change to whoever listens, which Observer handles without a coordinator.
- The mediator would hold rules for unrelated parts of the application, which makes it the class everyone edits.
← → move through sections and cards