The Mediator Pattern is a behavioral design pattern that defines an object (the Mediator) to encapsulate how a set of objects interact, promoting loose coupling by preventing direct communication between objects.
The Mediator Pattern centralizes communication between a group of objects by introducing a mediator object. Instead of objects referring to each other directly, they communicate through the mediator.
Key features:
- Centralized Communication: Mediator manages interactions between colleagues.
- Loose Coupling: Reduces dependencies between objects.
- Simplifies Object Relationships: Avoids tight interconnections between objects.
Mediatordefines an interface for communicating with colleague objects.Colleaguedefines an interface for interacting with the mediator.Planeconcrete implementation of theColleagueinterface.AirTrafficControlconcrete implementation of theMediatorinterface.Maindemonstrates the Mediator Pattern.
classDiagram
direction LR
class Mediator {
+mediate()
}
class ConcreteMediator {
+mediate()
}
class Colleague {
+operation()
}
class ConcreteColleagueA {
+operationA()
}
class ConcreteColleagueB {
+operationB()
}
Mediator <|-- ConcreteMediator
Colleague <|-- ConcreteColleagueA
Colleague <|-- ConcreteColleagueB
Colleague --> Mediator : mediator
ConcreteMediator --> ConcreteColleagueA
ConcreteMediator --> ConcreteColleagueB
Note
If the UML above is not rendering correctly, you can view the diagram from the mediator_uml.png file.
- Centralized Communication: The Mediator Pattern centralizes communication between objects.
- Loose Coupling: Objects interact through the mediator, reducing direct dependencies.
- Simplifies Object Relationships: Avoids complex interconnections between objects.
- Promotes Reusability: Encourages reuse of individual components.