The Mediator pattern is one of the Gang of Four (GoF) Behavioural patterns that restricts complex relationships between objects by providing a mediator object to encapsulate their interaction.
The mediator object acts as a bridge to facilitate the communication of multiple components. It removes direct dependencies between them and provides a loosely coupled implementation.
This pattern has multiple real applications:
Airport Traffic Control: The control tower decides which flight will arrive next by receiving real-time updates from each plane to coordinate the landing process. Aircraft pilots don't communicate with each other at any time. All information is sent to the central tower.
Chat application: Several participants are enrolled in a chat to exchange messages. There is a central location where all participants will connect to send and receive messages — no peer-to-peer communication to prevent privacy and security issues.
Live captioning system: "Each of the attendees of the call will set the captions to their language. When A speaks something, the captioning system (mediator) will display the message in the necessary language on the screen of the respective attendee. In no way the participants will communicate directly with each other." Mediator Design Pattern - Scaler Topics
Web applications: It's commonly used in Web APIs and Microservices Architecture by handling request/response messaging. It is also applicable in GUIs to coordinate multiple web components.
The Mediator pattern is useful when you have tightly coupled objects that are difficult to change. It lets you encapsulate the component communication through a mediator entity to isolate and manipulate each object independently of any other object.
ELEMENTS
Mediator: It's the interface or abstract class that defines the contract for communication between various components.
Concrete Mediator: It's the concrete implementation of the Mediator. It manages the communication between multiple components in the system. It knows its components and how they should interact. It usually contains methods for sending messages and registering new colleagues.
Colleague: It´s the individual component in the communication system. It is unaware of its colleagues and relies on the mediator to facilitate their interactions.
BENEFITS
Extensibility: New components can be added independently from any existing object.
Isolation: Each individual component is implemented separately, following the Single Responsibility Principle (SRP). So, every object is easier to maintain and test.
Open/Closed Principle: It's possible to introduce new mediators without modifying the actual components.
DRAWBACKS
Centralization of Control: "The mediator pattern exchanges interaction complexity for mediator complexity. A mediator might become more complicated than any individual colleague since it incorporates protocols. This can result in the mediator becoming a monolith that is difficult to maintain." Mediator Design Pattern - Scaler Topics
Check out my video:
Comments