Overview
The Mediator pattern centralizes communication between objects so they do not refer to each other directly. Instead of every component knowing every other component (n x n links), each talks to the mediator, which coordinates them. Chat rooms, air traffic control, UI dialogs, and elevator dispatchers are classic examples.
Mediators reduce coupling and make interactions easier to change in one place. The risk is the mediator becoming a god object that knows everything; keep it focused on coordination, with business rules in the colleagues.
Pilots do not negotiate directly with every other plane about runways. They talk to the tower, which coordinates who lands and takes off. Adding a plane does not require it to know all the others.
When to use it
- Many objects interact in complex, tangled ways.
- Components should be reusable without knowing each other.
- Coordination logic changes more often than the components.
Where it shows up in interviews
Recognize it when: many objects must react to each other.
- Design a chat room
- Design an elevator dispatcher
- Design an auction system
Recognize it when: form fields enable/disable each other.
- Design a flight booking form
- Design a settings panel
Where it is used in real software
Slack and Discord servers route messages between clients; clients never connect to each other directly.
A dispatcher assigns hall calls to cars instead of cars negotiating.
In-app event buses (MediatR in .NET) mediate between handlers.
Key terms
- Mediator
- Object that coordinates colleagues.
- Colleague
- Component that talks only to the mediator.
- Many-to-many to one-to-many
- The coupling reduction mediators provide.
How it works, step by step
- 1Identify interacting components
Users in a chat, cars in a building.
- 2Define the mediator interface
send(message, from), requestElevator(floor).
- 3Colleagues hold a mediator reference
Not references to each other.
- 4Mediator implements coordination rules
Routing, assignment, enabling.
- 5Keep rules small
Split into several mediators if it grows.
Coupling with and without a mediator
10 components that interact
| Design | References | Adding an 11th component |
|---|---|---|
| Direct peer references | Up to 10 x 9 = 90 | Touch up to 10 classes |
| Mediator | 10 (each to mediator) | Register with mediator |
NOWDesign: Direct peer references | References: Up to 10 x 9 = 90 | Adding an 11th component: Touch up to 10 classes
Coordination moves to one place; components become independent.
Implementation
interface ChatMediator { join(user: ChatUser): void; send(message: string, from: ChatUser, to?: string): void;} class ChatRoom implements ChatMediator { private users = new Map<string, ChatUser>(); join(user: ChatUser) { this.users.set(user.name, user); } send(message: string, from: ChatUser, to?: string) { if (to) return this.users.get(to)?.receive(message, from.name); // direct message for (const u of this.users.values()) if (u !== from) u.receive(message, from.name); // broadcast }} class ChatUser { readonly inbox: string[] = []; constructor(readonly name: string, private room: ChatMediator) { room.join(this); } say(message: string, to?: string) { this.room.send(message, this, to); } receive(message: string, from: string) { this.inbox.push(`${from}: ${message}`); }} const room = new ChatRoom();const ana = new ChatUser("ana", room);const bo = new ChatUser("bo", room);ana.say("hi all");bo.say("hi ana", "ana");Complexity and performance
Coupling reduction.
Often O(n).
Trade-offs
The mediator can accumulate all logic; keep domain rules in colleagues.
Interactions are less obvious from reading a colleague's code.
Variants and related techniques
Observer broadcasts to subscribers; mediator actively coordinates specific interactions.
A generic mediator based on published events.
Common mistakes
- Mediator knowing all internal details of colleagues.
Fix: Communicate through small interfaces.
- Colleagues bypassing the mediator.
Fix: Enforce that peers never reference each other.
Interview questions
Mediator vs Facade?
A facade simplifies access to a subsystem for outside clients, and subsystem classes do not know the facade. A mediator coordinates peers that do know the mediator and communicate through it.
How would an elevator system use a mediator?
A dispatcher receives hall calls and assigns them to cars based on position and direction; buttons and cars only talk to the dispatcher, so scheduling policies can change in one place.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Chat room with direct and broadcast messages | Easy | Routing. |
| Elevator dispatcher with strategies | Medium | Mediator + Strategy. |