Mediator
Last updated
Last updated
Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects.
The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.
The Mediator pattern is a behavioral design pattern that promotes loose coupling between objects by introducing a central component (the Mediator) that facilitates communication between them.
Instead of objects directly communicating with each other, they communicate through the Mediator, which encapsulates the interaction logic.
In JavaScript, you can implement the Mediator pattern using a simple object or a class.
In this example:
ChatRoom
acts as the Mediator. It keeps track of users and facilitates communication between them.
User
represents the Colleague objects. They send messages to each other through the ChatRoom
Mediator.
This pattern helps in decoupling the communication logic from the individual objects, making it easier to maintain and extend the codebase.