Mediator Design Pattern

This article is part 1 of the Design Pattern series.

Mediator Pattern is used to reduce communication complexity between multiple objects or classes. This pattern facilitates a mediator class which takes care of all the communication across different classes and supports easy maintenance of the code by loosely coupling. This pattern considered to be a behavioral pattern due to the way it can alter the program’s running behavior.

Usually, a program is made up of a large number of classes. However, as more classes are added to the program during maintenance, the problem of communicating between classes become more complex. This makes the program harder to maintain going forward.

So with mediator pattern, communication between objects is encapsulated with a mediator object. So the objects don’t need to do direct communication between each other instead communicate through the mediator. This process lowering the coupling.

The mediator is the communication centre of the objects. When an object needs to communicate to another object, it doesn’t call the other object directly. Instead it calls the mediator object which is to route the communication to the destination object.

Below is the example of the Mediator Design Pattern. You can assume mediator as a chat room, where multiple users can send messages to the chat room to show the message to all the users. Here we can create two classes ChatRoom and User. User objet will use ChatRoom’s method to share their messages.

MeditorPatternDemo class will use User object to show communication between them.


This article is part of the Design Pattern series. Articles in this series include:

  • Mediator Design Pattern
  • Flyweight Design Pattern

Related Posts