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

  1. Provides loose coupling.
  2. 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:

  1. Server in applications like the chat room acts as a mediator between users.
  2. Executor pool in Java acts as mediator between threads.
  3. The dispatcher servlet of Spring framework acts as a mediator between web request, the controller objects, and the view objects.