Overview
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.
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
Each colleague class knows its mediator object and each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.

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.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.codenuclear; public interface SocialMediaGroup { /* * Contract method for sending notification message */ public void sendMessage(String msg, Subscriber user); /* * Contract method for add Subscriber */ void addSubscriber(Subscriber user); } |
Subscriber can send or receive new article shared on group, We can implement it with interface or abstract class.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.codenuclear; public abstract class Subscriber { protected SocialMediaGroup mediator; protected String name; public Subscriber(SocialMediaGroup med, String name){ this.mediator=med; this.name=name; } /* * Contract method to send notification message. */ public abstract void send(String msg); /* * Contract method to receive new notification message. */ public abstract void receive(String msg); } |
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.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.codenuclear; import java.util.ArrayList; import java.util.List; public class LearnAndShareGroup implements SocialMediaGroup{ private List<Subscriber> subscribers; public LearnAndShareGroup(){ this.subscribers=new ArrayList<>(); } @Override public void addSubscriber(Subscriber subscriber){ this.subscribers.add(subscriber); } @Override public void sendMessage(String msg, Subscriber senderSubscriber) { for(Subscriber subscriber : this.subscribers){ /* * Sender should not receive message. */ if(subscriber != senderSubscriber){ subscriber.receive(msg); } } } } |
Now we can create concrete Subscriber class to be used by group.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.codenuclear; public class SubscriberImpl extends Subscriber{ public SubscriberImpl(SocialMediaGroup med, String name) { super(med, name); } @Override public void send(String msg) { System.out.println(this.name+": Shared New Article on : "+msg); mediator.sendMessage(msg, this); } @Override public void receive(String msg) { System.out.println(this.name+": There is new Article Available on : "+msg); } } |
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.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.codenuclear; public class SocialMediaGroupTest { public static void main(String[] args) { SocialMediaGroup mediator = new LearnAndShareGroup(); /* * Creating a Subscribers */ Subscriber subscriber1 = new SubscriberImpl(mediator, "John"); Subscriber subscriber2 = new SubscriberImpl(mediator, "Smith"); Subscriber subscriber3 = new SubscriberImpl(mediator, "Greg"); Subscriber subscriber4 = new SubscriberImpl(mediator, "Devin"); mediator.addSubscriber(subscriber1); mediator.addSubscriber(subscriber2); mediator.addSubscriber(subscriber3); mediator.addSubscriber(subscriber4); Subscriber1.send("Learn Design Patterns."); } } |
Output
Benefits of Mediator Pattern
- Increases the reusability of the objects supported by the Mediator by decoupling them from the system.
- Simplifies maintenance of the system by centralizing control logic.
- 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
- java.util.Timer class scheduleXXX() methods.
- Java Concurrency Executor execute() method.
- java.lang.reflect.Method invoke() method.
That’s all for Mediator design pattern in java, Hope you liked it. Keep Learning and Sharing 🙂 !!
Have you ever considered about including a little bit more than just your articles? I mean, what you say is valuable and everything. But imagine if you added some great visuals or video clips to give your posts more, “pop”! Your content is excellent but with images and video clips, this blog could certainly be one of the very best in its niche. Great blog!|
kleine schwuchtel