Mediator pattern
The mediator pattern provides an object that encapsulates how a set of objects interact. Objects no longer communicate directly with each other, but instead communicate through the mediator. In other words, mediator links various objects that have to collaborate, so as to minimize the knowledge they have about each other.
Mediator is also known as controller in the MVC architectural pattern.
advantages
- Provides loose coupling.
- Improves code readability and maintainability.
mediator vs facade
Both patterns work with many objects, so they can be confused. The facade pattern only exposes the existing functionality. The mediator pattern adds new functionality by providing communication between objects.
example
There are some real life examples:
- Server in applications like the chat room acts as a mediator between users.
- Executor pool in Java acts as mediator between threads.
- The dispatcher servlet of Spring framework acts as a mediator between web request, the controller objects, and the view objects.