Mediator Design Pattern

The Mediator Design Pattern reduce communication complexity between objects through mediator object. It decouple the direct interaction between objects by providing a mediator, that facilitates the communication between the objects. Instead of classes communicating directly with each other, classes send messages to the mediator and the mediator sends these messages to the other classes.

When an object wants to communicate with another object(s), it does not interact with other object(s) directly. Instead, it sends its message to the mediator object whose responsibility is to route the messages to destination object(s).

Real life Example

  • In Air traffic controller. All incoming or outgoing flights don’t communicate with each other directly, Instead they communicate with airport’s air traffic controller.
  • Group chat room. One member of group sends message to chat server, then it is chat server’s responsibility to propagate this message to all other members of the group.

Mediator Pattern evolves three actors

  • Mediator Interface : Interface used by Colleagues to interact with Mediator.
  • Concrete Mediator : Implements Mediator Interface and maintains a list of Colleagues communicating with each other. It facilitates the communication between Colleages by routing their messages to destination colleague(s). It is the communication center for the Colleagues.
  • Colleague(s) : Object(s) communicating with other object(s). It contains a reference of Mediator object.

Advantages of Mediator Pattern

  • It facilitate communication between objects such that objects are not aware of the existence of other objects.
  • The mediator can implement additional logic like access violations, security, sub-group communication, encryption etc.
  • All Colleague objects will communicate only with the mediator objects. They don’t have to follow different protocols to communicate with different objects.
  • Any change in the Mediator object will not affect Colleagues as long as the Mediator interface remains same.

When we should use Mediator Pattern

  • When we want to simplify the communication between lots of object interacting with each other.
  • When we want to centrally manage all communication between Colleagues.

Implementation of Mediator Design Pattern

Here, we will implement a chat group using mediator pattern. ChatServer is the Mediator interface class that is used by the char participants to interact with mediator object.

Mediator_Pattern

ChatServerMediator is the concrete implementation of ChatServer interface. It will act as a mediator object, whose primary responsibility is to route messages between participants.

Implementation of Mediator Design Pattern

ChatServerMediator.java

import java.util.List;
import java.util.ArrayList;
 
public class ChatServerMediator implements ChatServer {
    private List<Participant> participantList;
  
    public ChatServerMediator(){
       participantList = new ArrayList<Participant>();
    }
  
    public void addUser(Participant user){
       participantList.add(user);
    }
     
    public void sendMessage(Participant user, String message){
      for(Participant p : participantList){
         if(p != user){
            p.receiveMessage(message, user);
         }
      }
    }
}

Participants is the member of the chat group, communication with each other by calling mediator(ChatServerMediator).

Implementation of Mediator Design Pattern 1

Participant.java

public class Participant {
    private String userName;
    private ChatServer charServerMediator;
      
    public Participant(String name){
        this.userName = name;
    }
     
    public String getUserName(){
     return userName;
    }
    public void joinChatGroup(ChatServer chatGroup){
     charServerMediator = chatGroup;
     charServerMediator.addUser(this);
    }
          
    public void sendMessage(String message){
     System.out.println(userName +", Sending this message : \""
            + message + "\"");
     charServerMediator.sendMessage(this, message);
    }
      
    public void receiveMessage(String message, Participant user){
     System.out.println(userName + ", Received : \"" + message 
            + "\", From : " + user.userName);
    }
}

MediatorPatternExample will simulate the communication between participants using ChatServerMediator.

Mediator Design Pattern

MediatorPatternExample.java

public class MediatorPatternExample {
    public static void main (String args[]){
 ChatServer chatServer = new ChatServerMediator();
   
 Participant jack = new Participant("Jack");
 Participant george = new Participant("George");
 Participant emilly = new Participant("Emilly");
   
 jack.joinChatGroup(chatServer);
 george.joinChatGroup(chatServer);
 emilly.joinChatGroup(chatServer);
   
 // Jack is sending message
 jack.sendMessage("Hi Everyone, I am Jack");
 // Emilly replying to Jack
 emilly.sendMessage("Hi Jack, How are you");
    }
}

Output

Jack, Sending this message : "Hi Everyone, I am Jack"
George, Received : "Hi Everyone, I am Jack", From : Jack
Emilly, Received : "Hi Everyone, I am Jack", From : Jack
Emilly, Sending this message : "Hi Jack, How are you"
Jack, Received : "Hi Jack, How are you", From : Emilly
George, Received : "Hi Jack, How are you", From : Emilly