Mediator Design Pattern in Java

Filed Under: Design Patterns

Mediator design pattern is one of the behavioral design pattern, so it deals with the behaviors of objects. Mediator design pattern is used to provide a centralized communication medium between different objects in a system.

Mediator Design Pattern

mediator pattern, mediator design pattern, mediator pattern java

According to GoF, mediator pattern intent is:

Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.

Mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly, the system components are tightly-coupled with each other that makes higher maintainability cost and not hard to extend. Mediator pattern focuses on provide a mediator between objects for communication and help in implementing lose-coupling between objects.

Air traffic controller is a great example of mediator pattern where the airport control room works as a mediator for communication between different flights. Mediator works as a router between objects and it can have it’s own logic to provide way of communication.

The system objects that communicate each other are called Colleagues. Usually we have an interface or abstract class that provides the contract for communication and then we have concrete implementation of mediators.

For our example, we will try to implement a chat application where users can do group chat. Every user will be identified by it’s name and they can send and receive messages. The message sent by any user should be received by all the other users in the group.

Mediator Pattern Interface

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


public interface ChatMediator {

	public void sendMessage(String msg, User user);

	void addUser(User user);

Mediator Pattern Colleague Interface

Users can send and receive messages, so we can have User interface or abstract class. I am creating User as abstract class like below.


public abstract class User {
	protected ChatMediator mediator;
	protected String name;
	public User(ChatMediator med, String name){
	public abstract void send(String msg);
	public abstract void receive(String msg);

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

Concrete Mediator

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


import java.util.ArrayList;
import java.util.List;

public class ChatMediatorImpl implements ChatMediator {

	private List<User> users;
	public ChatMediatorImpl(){
		this.users=new ArrayList<>();
	public void addUser(User user){
	public void sendMessage(String msg, User user) {
		for(User u : this.users){
			//message should not be received by the user sending it
			if(u != user){


Mediator Design Pattern Concrete Colleague

Now we can create concrete User classes to be used by client system.


public class UserImpl extends User {

	public UserImpl(ChatMediator med, String name) {
		super(med, name);

	public void send(String msg){
		System.out.println(": Sending Message="+msg);
		mediator.sendMessage(msg, this);
	public void receive(String msg) {
		System.out.println(": Received Message:"+msg);


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

Mediator Pattern Example Client Program Code

Let’s test this our chat application with a simple program where we will create mediator and add users to the group and one of the user will send a message.


public class ChatClient {

	public static void main(String[] args) {
		ChatMediator mediator = new ChatMediatorImpl();
		User user1 = new UserImpl(mediator, "Pankaj");
		User user2 = new UserImpl(mediator, "Lisa");
		User user3 = new UserImpl(mediator, "Saurabh");
		User user4 = new UserImpl(mediator, "David");
		user1.send("Hi All");


Notice that client program is very simple and it has no idea how the message is getting handled and if mediator is getting user or not.

Output of the mediator pattern example program is:

Pankaj: Sending Message=Hi All
Lisa: Received Message:Hi All
Saurabh: Received Message:Hi All
David: Received Message:Hi All

Mediator Pattern Class Diagram

mediator pattern class diagram, mediator design pattern java

Mediator Pattern Example in JDK

Mediator Design Pattern Important Points

  • Mediator pattern is useful when the communication logic between objects is complex, we can have a central point of communication that takes care of communication logic.
  • Java Message Service (JMS) uses Mediator pattern along with Observer pattern to allow applications to subscribe and publish data to other applications.
  • We should not use mediator pattern just to achieve lose-coupling because if the number of mediators will grow, then it will become hard to maintain them.

That’s all for mediator design pattern and it’s implementation in java.


  1. Fatih says:

    This is something mix of mediator and visitor

  2. mohamed says:

    nice explanation, but i can’t quite see the difference between mediator pattern and facade pattern, what is the difference between these two ?

    1. John says:

      Because it’s a “nice explanation” of nothing. If you want to understand, read GoF

  3. fatemeh says:

    hello,please help me
    i want to know, how use jms from mediator pattern?

  4. robothy says:

    Nice example.

  5. David says:

    I knew at this and I can´t see the differences between mediator pattern and observe pattern

    1. Hari K Bista says:

      you are right.

    2. Hiccup says:

      I think the difference between mediator and observer is
      mediator : intent is any object want to initiate communication but in obeservation subject state triggers and it’s mainly broacast..

      here i think example is taken is not compete use of mediator. Mediator is not about broadcast’s a selective communication. Interested object communicate and that mapping is with the mediator either in the form of hash map or any other data structure…

    3. vinod says:

      In observer, many objects are interested in the state change of one object. They are not interested in each other. So the relation is one to many.

      In mediator, many objects are interested to communicate many other objects. Here the relation is many to many.

  6. Annappa says:

    Nice explanation.

  7. Ashwin says:

    Really crisp and good writeups

  8. Aditya says:

    how mediator pattern is implemented in ExecutorService API.Kindly explain

  9. mohammad says:

    tanck you for your nice tutorial.

  10. Mohit Gupta says:

    Awesome explaination thanks 🙂

  11. abc says:

    who is the mediator here?

    1. Leo says:


  12. Avinash Pandey says:

    Your tutorials are nice and explanatory.

  13. Ilia Sokolovski says:


    That was actually very helpful, Thanks!

  14. Lalit Upadheyay says:

    I think that the line(s) mediator.addUser in ChatClient can be avoided by delegating this call in User constructor by appending one line

    Also we should implemnet Mediator as a singleton, to avoid scenario when different mediator instances can be tied to a partcipating object. This would cause synchronized message relay issue and the implementation will fail to serve its purpose.

    1. Rishi says:

      Agree 100%

      1. venkatesh says:

        how would we use multiple group chats if mediator is singleton

    2. Shyam says:

      This is an classic example for people to understand what is the use of mentioned designed pattern. Later after understanding its up to one how to use his knowledge.
      If you are so great at making code perfect, make an tutorial and help others.
      Please don’t boast too much.

  15. Koteshwar says:

    Excellent..Thank you so much 🙂

  16. Krishna says:

    Thanks for best and easiest example with explanation

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content