Mediator: Centralized Communication
The Problem It Solves
A chat room’s participants react to each other: new message updates every user list, typing indicators, unread counts. Direct wiring makes each participant reference all others — mesh coupling. Add a participant type (bot, moderator) and every existing class edits. Mediator inserts a hub: colleagues talk only to the mediator; it owns routing, and peers become mutually anonymous.
MESH: n(n-1)/2 direct links STAR: everyone knows only the tower
A ───── B 4 users = A B
│ ╲ ╱ │ 6 links ╲ ╱
│ ╳ │ 10 users = [ Mediator/Tower ]
│ ╱ ╲ │ 45 links ╱ ╲
C ───── D C D
links grow quadratically links grow linearly; adding a peer
touches nobody else
The air-traffic-control analogy is exact: planes never negotiate landing with each other; the tower sequences everyone.
Mechanics
interface ChatMediator {
void sendMessage(String msg, User sender);
void addUser(User u);
}
class ChatRoom implements ChatMediator {
private final List<User> users = new CopyOnWriteArrayList<>();
public void addUser(User u) { users.add(u); }
public void sendMessage(String msg, User sender) {
for (User u : users)
if (u != sender) u.receive(sender.name() + ": " + msg);
}
}
class User {
private final String name;
private final ChatMediator mediator; // knows ONLY the hub
void send(String msg) { mediator.sendMessage(msg, this); }
void receive(String msg) { display(msg); }
}
User never references another User — the entire peer graph lives inside ChatRoom, changeable without touching colleagues.
Structure Roles
| Role | Job |
|---|---|
| Mediator interface | Colleague-facing protocol |
| Concrete mediator | Routing, coordination policy, business rules of interaction |
| Colleagues | Hold mediator reference; no peer references |
vs Observer
| Observer | Mediator | |
|---|---|---|
| Direction | Subject broadcasts; observers listen | Bidirectional negotiation through hub |
| Coupling | One subject → many listeners | All peers → one hub |
| Fits | Event notification | Multi-party coordination (dialogs, workflows) |
A form where checkbox enables button, slider limits input, submit validates everything — classic mediator territory: widgets notify the mediator, mediator commands widgets.
The Known Cost — God Hub Risk
All coordination logic concentrates in the mediator; unmanaged it becomes a god object knowing every colleague’s needs. Controls:
- Split mediators per interaction domain (ChatRoom ≠ PresenceTracker).
- Keep colleagues’ business logic in colleagues; mediator does routing/sequencing only.
- Interface the mediator so alternative coordination policies are injectable.
Real-World Sightings
- Air traffic control towers; trading exchange matching engines (all orders via central book).
- Dialog controllers coordinating widget events; workflow orchestrators sequencing services.
- Message brokers at system scale are architectural mediators.
Interview Framing
- Leading with the quadratic-vs-linear link math justifies the pattern better than any UML.
- Naming the god-hub failure mode and its split-mediator remedy is the seniority signal.
Premium Content
Unlock Mediator and all premium lessons with a subscription.
From ₹199.99/year — See plans