Mediator Design Pattern In Java

Let's finish some remaining design patterns to cover this topic. Today, I will discuss another design pattern - Mediator Design Pattern.

Mediator Design Pattern

  • The Mediator Design Pattern is one of the Gang of Four design patterns which is used to control and reduce complexity of communication among groups of objects.
  • The Mediator Pattern is a behavioral pattern which defines an object to encapsulate the interaction between objects.
  • The Mediator Pattern helps us to reduce chaotic dependencies between objects. 
  • The Mediator pattern helps us to restrict any direct communication between the objects and allow us to collaborate objects only via a mediator object.
  • By using Mediator object we can control communication between objects.
  • The Mediator object also hides the complexity of communication between objects.
  • The Mediator pattern defines a mediator class to handle all the communications between different classes. That also supports easy maintenance of the communication related code by using loose coupling.
  •  The Chatting Application is a good example of Mediator pattern where a group of chat users communicates with each other via the char server.
  • The Whatsapp is another good example of the Mediator pattern. Whatsapp server handles all the communication between different whatsapp users.
  • Air Traffic Control System is also another great example in which all flights doing takeoff or landing communicates with the Air Traffic Control to manage their safe and smooth movement.

mediator design pattern

Let's take an example of communication between different workers; each one is assigned to a dedicated type of job to perform. We will see how they can communicate with others with the help of manager for the jobs they are not doing. I will not use multi-threading or network related code just to make the example as simple as possible.