Mediator design pattern is a behavioral design pattern. It is used to provide a centralized communication medium between different objects in a system.

The Gang of Four has indicated that the mediator pattern is to define an object that encapsulates how a set of objects interacts. They also mentioned that the mediator promotes lose coupling by keeping objects from referring to each other explicitly, and it lets you vary the interaction independently. So, simply said, it basically lets you control the communication between each object. As the name implies: mediator.

Mediator Pattern is helpful in an Enterprise Applications where we have multiple modules communicating with each other, If the modules interact with each other directly, the modules are tightly coupled that make high maintenance cost and difficult to extend.

Mediator in the Real World

An airport control tower is an excellent example of the mediator pattern. The tower looks after who can take off and land – all communications are done from the airplane to control tower, rather than having plane-to-plane communication. This idea of a central controller is one of the key aspects of the mediator pattern.

Air Traffic Control
Air Traffic Control

Mediator Pattern Diagram

Mediator Pattern can also be known as dictator pattern. Let’s go ahead and take a look at the class diagram. The key players that are involved are: the mediator, concrete mediator and the colleague classes. The mediator defines an interface. That’s going to be the abstraction. It defines an interface for communicating with the colleague objects. The concrete mediator implements cooperative behavior by coordinating colleague objects. Next we have our colleague classes.

Each colleague class knows its mediator object and each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.

Mediator Pattern Diagram
Mediator Pattern Diagram

Java Implemantation of Mediator Pattern

Let’s understand Java implementation of the Mediator pattern with Social Media Group example. Let’s have a group called Learning And Sharing, anyone can subscribe to the group, the subscriber can share their views, ideas or articles and same is notified to other subscribers of the group. Here the group is performing as a Mediator and all subscribers are Colleagues.

First of all we will create Mediator interface that will define the contract for concrete mediators.

Subscriber can send or receive new article shared on group, We can implement it with interface or abstract class.

Notice that Subscriber has a reference to the mediator object, it’s required for the communication between different subscribers.

Now we will create concrete mediator class, It will have a list of subscribers in the group and provide logic for communication between the subscribers.

Now we can create concrete Subscriber class to be used by group.

Notice that send() method is using the mediator to send the message to the subscribes and it has no idea how it will be handled by the mediator.

Let’s create Test class to test Mediator Pattern.


John: Shared New Article on : Learn Design Patterns. Smith: There is new Article Available on : Learn Design Patterns. Greg: There is new Article Available on : Learn Design Patterns. Devin: There is new Article Available on : Learn Design Patterns.

Benefits of Mediator Pattern

  1. Increases the reusability of the objects supported by the Mediator by decoupling them from the system.
  2. Simplifies maintenance of the system by centralizing control logic.
  3. Simplifies and reduces the variety of messages sent between objects in the system.

Drawback of Mediator Pattern

A drawback of the Mediator pattern is that without proper design, the Mediator object itself can become overly complex.

Mediator Pattern Example in JDK

  1. java.util.Timer class scheduleXXX() methods.
  2. Java Concurrency Executor execute() method.
  3. java.lang.reflect.Method invoke() method.

That’s all for Mediator design pattern in java, Hope you liked it. Keep Learning and Sharing 🙂 !!

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *