Design Patterns Mediator
The Mediator pattern places one central object between many components. Components stop talking to each other directly and speak only to the mediator. The mediator coordinates the conversation and keeps the web of dependencies tidy.
The Real-World Analogy
An airport control tower guides every aircraft. Pilots never negotiate runway use with one another. Each pilot contacts the tower, and the tower tells everyone when to land, wait, or depart.
Problem Without a Mediator
┌────────┐ ◀────────▶ ┌────────┐
│ Button │ │ TextBox│
└───┬────┘ ╲ ╱ └───┬────┘
│ ╲ ╱ │
│ ╳ │
│ ╱ ╲ │
┌───┴─────┐ ╱ ╲┌───┴─────┐
│ Checkbox│ ◀───────▶ │ Dropdown│
└─────────┘ └─────────┘
4 components, 6 direct links. 10 components, 45 links.
Every component knows several others. One change ripples through the entire form.
Solution With a Mediator
┌────────┐ ┌────────┐
│ Button │ │ TextBox│
└───┬────┘ └───┬────┘
│ │
▼ ▼
┌──────────────────────┐
│ DialogMediator │
└──────────────────────┘
▲ ▲
│ │
┌───┴─────┐ ┌───┴──────┐
│ Checkbox│ │ Dropdown │
└─────────┘ └──────────┘
4 components, 4 links. 10 components, 10 links.
Links grow in a straight line, and components become reusable because they know only the mediator.
Structure
┌──────────────────┐ ┌─────────────────────┐
│ «interface» │◀───────│ Component │
│ Mediator │ │ - mediator │
│ + notify(sender, │ │ + changed() │
│ event) │ └──────────▲──────────┘
└────────▲─────────┘ │
│ ┌──────────┴──────────┐
┌────────┴─────────┐ ConcreteComponentA ConcreteComponentB
│ ConcreteMediator │
└──────────────────┘
Code Example: A Sign-Up Form
interface Mediator { void notify(Object sender, String event); }
class SignUpMediator implements Mediator {
TextBox email;
Checkbox agree;
Button submit;
public void notify(Object sender, String event) {
if (sender == agree || sender == email) {
boolean ready = agree.isChecked() and email.hasText();
submit.setEnabled(ready); // coordinate here
}
}
}
class Checkbox {
private Mediator mediator;
private boolean checked;
void click() {
checked = !checked;
mediator.notify(this, "clicked"); // tell the hub only
}
}The Checkbox never mentions the Button. Which parts of the form react stays the mediator's decision.
Message Flow
User ticks checkbox
│
▼
Checkbox.click() ──▶ mediator.notify()
│
├─ reads email text
├─ reads checkbox state
└─ enables submit button
Real Projects Using Mediator
| Scenario | Role of the Mediator |
|---|---|
| Chat room | Delivers messages between users |
| Dialog window | Enables or hides fields by rules |
| Smart home hub | Links sensors, lights, and thermostats |
| Air traffic software | Schedules takeoffs and landings |
Mediator Versus Observer
Observer spreads one-way notifications from a subject to many listeners. Mediator handles two-way coordination among peers through a single hub. Teams sometimes build a mediator on top of Observer.
Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| Fewer direct links between classes | Mediator can swell into a giant class |
| Components stay reusable | All logic depends on one object |
| Interaction rules live in one place | Debugging requires reading the hub |
Step-by-Step Walkthrough
Follow a click on the agreement checkbox. The Checkbox flips its checked flag and calls notify() on the mediator. Beside that flag, the mediator reads the email box. Both conditions hold, and the mediator enables the submit button. Untick the box, and the same path disables the button.
The Button class contains no rules about the checkbox. Moving the Button to a different form requires no rewrite.
Keeping the Mediator Healthy
- Split big mediators by screen section or feature.
- Pass only events and small data objects to the mediator.
- Keep display work inside components, and keep coordination work inside the mediator.
Practice Idea
Sketch a login form with a username field, a password field, a "show password" checkbox, and a login button. List which events the mediator receives and which components it updates.
Plain-Words Summary
Mediator shines in screens with many fields that react to each other, such as booking forms and settings pages. Writing every rule inside the individual fields spreads logic thin, and nobody can see the whole picture. Moving the rules into one mediator creates a single place to read, test, and adjust. Teammates join projects faster when interaction rules live together.
Key Takeaways
- A mediator coordinates peers, and they never reference each other.
- Link counts drop from many-to-many to one-to-many.
- Keep the mediator focused, or it turns into a god class.
- Observer broadcasts events, and Mediator manages conversations.
