# Purpose
The Mediator pattern aims to reduce the complexity of communications between multiple objects or classes by centralizing the interaction in a single place.
# Problem
-
High Coupling: Too many direct connections between classes make the system fragile.
-
Complex Communication: Difficult to understand and maintain the flow of messages between multiple components.
# Solution
-
Centralization: Classes communicate only through a central mediator object.
-
Simplification: Control logic of interaction resides in a single point, decoupling the colleagues.
# Structure
# Participants
- Mediator: Defines the communication interface.
- ConcreteMediator: Coordinates the actual communication between colleagues.
- Colleague: Base class/interface for components.
- ConcreteColleague: Objects communicating via the mediator.
# When to Use It
When there are many objects interacting in a complex but well defined way, and you want to centralize the communication logic.
# Advantages
-
verified
Low Coupling: Decreases direct dependencies.
-
verified
Maintainability: Communication changes in a single place.
# Disadvantages
-
warning
God Object: The mediator can become overly complex.
-
warning
Performance: Bottleneck in high-load systems.
# Example: Air Traffic Control
Management of control tower, airplanes and runways without direct communication between aircraft.
# Java Code
interface AirTrafficMediator {
void register(Component c);
void send(String msg, Component c);
}
class TowerMediator implements AirTrafficMediator {
private List<Component> comps = new ArrayList<>();
public void send(String msg, Component origin) {
for (Component c : comps) if (c != origin) c.receive(msg);
}
}
# Mapping Participants
- Tower (Mediator): Central coordination point.
- Airplane/Runway (Colleagues): Isolated components.
- AirportSystem: Client.
# Conclusions
Ideal for systems where multiple objects must collaborate without having a tangled network of dependencies.
# Related Patterns
Observer
Dynamically keeps colleagues in sync.
Facade
Mediator centralizes interaction, Facade simplifies external access.