The Mediator Design Pattern is a behavioral design pattern that promotes loose coupling between objects by encapsulating how they interact. It centralizes complex communication logic between multiple objects into a mediator object, thus reducing direct dependencies between them. This promotes easier maintenance and scalability of the system.
The Mediator Design Pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object. This pattern is used to centralize complex communications and control between related objects in a system. The Mediator object acts as the communication center for all objects. That means when an object needs to communicate with another object, it does not call the other object directly. Instead, it calls the mediator object, and it is the responsibility of the mediator object to route the message to the destination object.
Components of Mediator Design Pattern
- Mediator: Defines an interface for communication between colleague objects.
- Colleague: It is an abstract class, and Concrete Colleague classes will implement this abstract class.
- ConcreteMediator: Implements the mediator interface, coordinating communication between colleague objects.
- ConcreteColleague: Implements the colleague interface and communicates with other colleagues through the mediator.