The Mediator Design Pattern
The Mediator pattern defines an object that encapsulates how a set of objects interact.
Use the Mediator pattern when you want to reduce coupling between classes that communicate with each other directly, or when you want to centralize external communications.
- TypeBehavioral
- Time ComplexityO(n) Linear Time – The time taken grows linearly with the size of the input. Examples include simple search algorithms.
- Efficiency5
-
Learning Effort
- Average6
- Conceptual7
- Debug and Maintain5
- Implementation5
- Prerequisites6
- Versatility8
Things you should know before you start
- Encapsulation Understanding of encapsulation, as the Mediator encapsulates the interaction logic.
- Polymorphism Understanding of polymorphism, as different types of mediators can be interchanged.
Minimal Example
Implementing a chat room where users can send messages without knowing the details of other users.
class ChatRoom implements Mediator { sendMessage(message: string, user: User): void { /* ... */ } } const chatRoom = new ChatRoom(); const user1 = new User(chatRoom); const user2 = new User(chatRoom); user1.sendMessage('Hello', user2);
What’s happening in thie example
In this example, the `ChatRoom` class serves as a ConcreteMediator, coordinating the interaction between `User` objects. The `clientCode` shows how users can send messages to each other through the mediator.
How the Mediator Works
The Mediator pattern promotes loose coupling by keeping objects from referring to each other explicitly.
This pattern is particularly useful for facilitating communication between different subsystems in a software application.
Entities in the Mediator Design Pattern
-
MediatorThe Mediator interface defines a set of methods for communicating with colleagues.
- Mediator has a one-to-many implements relationship with ConcreteMediator
What to watch out for when using or considering this design pattern.
Avoid using the Mediator pattern if you don’t have many interrelated dependencies between objects, or if centralizing control would create a mediator that’s overly complex.
Design patterns often confused with the Mediator Design Pattern
The following design patterns are often confused with the Mediator
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Observer – The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
The Observer pattern is similar to the Mediator in the following ways.
- Both are behavioral design patterns.
- Both deal with object communication.
- Mediator centralizes external communications between objects, while Observer distributes them.
- In Mediator, objects are aware of the mediator, whereas in Observer, subjects and observers are loosely coupled.
-
Facade – The Facade pattern provides a unified interface to a set of interfaces in a subsystem, making the subsystem easier to use.
The Facade pattern is similar to the Mediator in the following ways.
- Both patterns centralize communication.
- Both aim to simplify complex systems.
- Mediator focuses on interaction between objects, while Facade simplifies an interface.
- Mediator allows for more complex control and can be changed at runtime, while Facade is generally static.