Mediator
Defines an object that encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly.
errorWhat Problem Does the Mediator Pattern Solve?
When many objects communicate directly with each other, the system becomes a tangled web of dependencies where every object must know about every other. This makes adding, removing, or changing any single participant cascade into changes across the entire network of objects.
check_circleHow the Mediator Pattern Works
Introduce a mediator object that centralizes all inter-object communication. Each participant (colleague) holds a reference only to the mediator and sends all messages through it. The mediator knows all colleagues and orchestrates their interactions, reducing direct dependencies from O(n²) to O(n).
Mediator Pattern Architecture
Rendering diagram...
Implementation by Language
Mediator Pattern in the Real World
“An air traffic control tower is the classic example. Instead of every plane communicating directly with every other plane—a chaotic and dangerous mess—all aircraft talk only to the control tower. The tower mediates all interactions, directing each plane based on the overall picture it maintains. Planes are decoupled from each other entirely.”
Frequently Asked Questions
helpWhat is the difference between Mediator and Observer?
Observer creates a one-to-many broadcast — the subject notifies all observers of changes. Mediator creates a many-to-many coordination hub — components communicate through the mediator, which routes and transforms messages. Mediator encapsulates the interaction logic; Observer does not.
helpWhen does a Mediator become a 'God Object'?
When the mediator accumulates too much business logic and every component depends on it. Keep the mediator focused on coordination — routing messages between components — not on business rules. If the mediator grows large, split it into multiple focused mediators by domain.