How does it reduce coupling between objects?
The Mediator pattern reduces coupling between objects by replacing direct links between them with a single point of interaction, a centralized mediator.
1. Without a Mediator, a Full Mesh of Dependencies
If objects interact directly, each one has to know about the others: who exists in the system, what methods they have, when to call them.
Example: In a registration window, the
Loginbutton when to activate, and the button, in turn, callsPasswordFieldmethods for validation.
The result is a "web-like architecture", where changing one component requires edits everywhere else.
2. With a Mediator, All Links Go Through One Object
Each component only knows one participant, the mediator:
class Button {
private Mediator mediator;
public void click() {
mediator.notify(this, "click");
}
}The mediator receives events from components and decides who should react:
class DialogMediator implements Mediator {
private Button loginButton;
private TextBox emailField;
private TextBox passwordField;
public void notify(Component sender, String event) {
if (sender == emailField || sender == passwordField)
loginButton.setEnabled(!emailField.isEmpty() && !passwordField.isEmpty());
}
}Now EmailField, PasswordField, and Button don't know about each other,
they simply "report the news" to the mediator.
3. What This Gives You
- Weaker coupling, each object knows only the Mediator, not dozens of other components.
- Easy modification, a new component can be added without changing the others.
- Reusability, components become independent and can be used in other contexts.
- Simplified interaction logic, all the behavior between objects is now concentrated in one place.
4. Analogy
Without Mediator, it's as if every airplane pilot communicated directly with every other pilot. With Mediator, all pilots talk only to the dispatcher, and the dispatcher decides who should act and when.
Conclusion
The Mediator pattern reduces coupling because it:
- replaces many direct links with one central one,
- makes components independent and reusable,
- concentrates interaction in one place, simplifying maintenance and the evolution of the system.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.