Mediator Pattern

The Mediator pattern is a behavioral design pattern in object-oriented programming (OOP) that promotes loose coupling between objects by encapsulating how they interact and communicate with each other. It defines an object (the mediator) that centralizes communication logic between multiple objects (colleague objects) instead of having them communicate directly with each other. This promotes better maintainability and reduces dependencies between objects.

 

The Mediator pattern involves the following key components:

  • Mediator: Defines an interface or an abstract class that encapsulates the communication logic between colleague objects. It typically includes methods for registering, unregistering, and facilitating communication among colleagues.

  • Concrete Mediator: Implements the Mediator interface and manages the communication and coordination between colleague objects. It knows about all the colleagues and facilitates their interactions.

  • Colleague: Represents an object that communicates with other colleagues through the mediator. Colleagues are typically decoupled from each other and rely on the mediator to facilitate communication.

  • Concrete Colleague: Implements the Colleague interface and represents a specific object that interacts with other colleagues through the mediator. It communicates with other colleagues by sending messages to the mediator.