Mediator Patternijc

design patterns, core java design, creational design, abstract factory pattern, singleton design patterns, adapter, visitor pattern, template pattern, command pattern, state pattern, java

Mediator Patternijc

Mediator Pattern

A Mediator Pattern says that "to define an object that encapsulates how a set of objects interact".

I will explain the Mediator pattern by considering a problem. When we begin with development, we have a few classes and these classes interact with each other producing results. Now, consider slowly, the logic becomes more complex when functionality increases. Then what happens? We add more classes and they still interact with each other but it gets really difficult to maintain this code now. So, Mediator pattern takes care of this problem.

Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintainability of the code by loose coupling.


Benefits:

  • It decouples the number of classes.
  • It simplifies object protocols.
  • It centralizes the control.
  • The individual components become simpler and much easier to deal with because they don't need to pass messages to one another.
  • The components don't need to contain logic to deal with their intercommunication and therefore, they are more generic.

Usage:

  • It is commonly used in message-based systems likewise chat applications.
  • When the set of objects communicate in complex but in well-defined ways.

UML for Mediator Pattern:

Participants:

  • ApnaChatroom :- defines the interface for interacting with participants.
  • ApnaChatroomImpl :- implements the operations defined by the Chatroom interface. The operations are managing the interactions between the objects: when one participant sends a message, the message is sent to the other participants.
  • Participant :- defines an interface for the users involved in chatting.
  • User1, User2, ...UserN :- implements Participant interface; the participant can be a number of users involved in chatting. But each Participant will keep only a reference to the ApnaChatRoom.

Implementation of Mediator Pattern:

Step 1:

Create a ApnaChatRoom interface.

  1. //This is an interface.  
  2. public interface ApnaChatRoom {  
  3.       
  4.     public void showMsg(String msg, Participant p);  
  5.   
  6. }// End of the ApnaChatRoom interface.  

Step 2:

Create a ApnaChatRoomIml class that will implement ApnaChatRoom interface and will also use the number of participants involved in chatting through Participant interface.

  1. //This is a class.  
  2. import java.text.DateFormat;  
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. public class ApnaChatRoomImpl implements ApnaChatRoom{  
  7.     //get current date time   
  8.     DateFormat dateFormat = new SimpleDateFormat("E dd-MM-yyyy hh:mm a");  
  9.     Date date = new Date();  
  10.     @Override  
  11.     public void showMsg(String msg, Participant p) {  
  12.           
  13.         System.out.println(p.getName()+"'gets message: "+msg);  
  14.         System.out.println("\t\t\t\t"+"["+dateFormat.format(date).toString()+"]");    
  15.     }     
  16. }// End of the ApnaChatRoomImpl class.  

Step 3:

Create a Participant abstract class.

  1. //This is an abstract class.  
  2. public abstract class Participant {  
  3.       public abstract void sendMsg(String msg);  
  4.       public abstract void setname(String name);  
  5.       public abstract String getName();  
  6. }// End of the Participant abstract class.  

Step 4:

Create a User1 class that will extend Participant abstract class and will use the ApnaChatRoom interface.

  1. //This is a class.  
  2.   
  3. public class User1 extends Participant {  
  4.       
  5.     private String name;  
  6.     private ApnaChatRoom chat;  
  7.       
  8.     @Override  
  9.     public void sendMsg(String msg) {  
  10.     chat.showMsg(msg,this);  
  11.           
  12.     }  
  13.   
  14.     @Override  
  15.     public void setname(String name) {  
  16.         this.name=name;  
  17.     }  
  18.   
  19.     @Override  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.       
  24.     public User1(ApnaChatRoom chat){  
  25.         this.chat=chat;  
  26.     }     
  27.       
  28. }// End of the User1 class.  

Step 5:

Create a User2 class that will extend Participant abstract class and will use the ApnaChatRoom interface.

  1. //This is a class.  
  2.   
  3. public class User2 extends Participant {  
  4.   
  5.     private String name;  
  6.     private ApnaChatRoom chat;  
  7.       
  8.     @Override  
  9.     public void sendMsg(String msg) {  
  10.     this.chat.showMsg(msg,this);  
  11.           
  12.     }  
  13.   
  14.     @Override  
  15.     public void setname(String name) {  
  16.         this.name=name;  
  17.     }  
  18.   
  19.     @Override  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.       
  24.     public User2(ApnaChatRoom chat){  
  25.         this.chat=chat;  
  26.     }  
  27.   
  28.       
  29.       
  30. }  
  31. // End of the User2 class.  

Step 6:

Create a MediatorPatternDemo class that will use participants involved in chatting.

  1. //This is a class.  
  2.   
  3. public class MediatorPatternDemo {  
  4.       
  5.     public static void main(String args[])  
  6.     {  
  7.           
  8.           ApnaChatRoom chat = new ApnaChatRoomImpl();  
  9.       
  10.           User1 u1=new User1(chat);  
  11.           u1.setname("Ashwani Rajput");  
  12.           u1.sendMsg("Hi Ashwani! how are you?");  
  13.         
  14.                 
  15.           User2 u2=new User2(chat);  
  16.           u2.setname("Soono Jaiswal");  
  17.           u2.sendMsg("I am Fine ! You tell?");  
  18.     }  
  19.   
  20. }// End of the MediatorPatternDemo class.