In this post, We will talk and learn about the Mediator Design Pattern in Java.
Key Points About Mediator Design Pattern :
- Mediator design pattern falls under behavioral design pattern. This Pattern deals with the behaviors of objects. This pattern is used to provide a centralized communication medium between different objects in the application.
- This pattern is really very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly than system components are tightly-coupled with each other that make higher maintainability cost and not hard to extend as well. The mediator pattern usually focuses to provide a mediator between objects for communication and helps in implementing lose-coupling between objects.
- In Mediator design pattern the system objects that communicate with each other are called Colleagues. Usually, we have an interface or abstract class that provides the contract for communication and then we need to create a concrete implementation of mediators.
- Mediator pattern is really useful when the communication logic between objects is complex and we can have a central point of communication that takes care of communication logic.
Mediator Pattern Example in JDK:
- java.util.Timer class scheduleXXX() methods
- Java Concurrency Executor execute() method.
- java.lang.reflect.Method invoke() method
Now Let’s move towards the implementation of the Mediator Design Pattern.
Here, Imeplemenation Shows an example of Group Chat Application where every user can be identified by their user name, and when one user sends a message in group than that message should be received by all other users in that group Chat.
Below is the complete source code:
ChattingMediator.java
1 2 3 4 5 6 7 8 9 10 11 |
package com.kkjavatutorials.chatting; /** * @author KK JavaTutorials * Contract to Adding new User and * sending message in group chat. */ public interface ChattingMediator { public abstract void sendMessage(String message,User user); public abstract void addUser(User user); } |
ChattingMediatorImpl.java
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 33 34 |
package com.kkjavatutorials.chatting; import java.util.ArrayList; import java.util.List; /** * @author KK JavaTutorials *Implelementation of ChattingMediator *which add users in group chat and *send message by user in group * */ public class ChattingMediatorImpl implements ChattingMediator { private List<User> userList; public ChattingMediatorImpl() { userList = new ArrayList<User>(); } @Override public void sendMessage(String message, User user) { for (User u : userList) { if(!u.getUserName().equalsIgnoreCase(user.getUserName())) { u.receiveMessage(message); } } } @Override public void addUser(User user) { userList.add(user); } } |
User.java
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 |
package com.kkjavatutorials.chatting; /** * @author KK JavaTutorials *This class initializes ChattingMediator & *userName in constructor so that subclass of this class *has to pass these values from their constructor *and implementation of methods sendMessage() & receiveMessage() *left on sub classes */ public abstract class User { private ChattingMediator chattingMediator; private String userName; public User(ChattingMediator chattingMediator, String userName) { super(); this.chattingMediator = chattingMediator; this.userName = userName; } public ChattingMediator getChattingMediator() { return chattingMediator; } public String getUserName() { return userName; } public abstract void sendMessage(String message); public abstract void receiveMessage(String message); } |
UserImpl.java
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 33 |
package com.kkjavatutorials.chatting; /** * @author KK JavaTutorials *This class passing values of instance chattingMediator *& userName to super class User and overridden * methods sendMessage & receiveMessage * to fulfill. * Method sendMessage -- > Send message in group chat * Method receiveMessage -- > receive message by every user in group chat */ public class UserImpl extends User { private ChattingMediator chattingMediator; private String userName; public UserImpl(ChattingMediator chattingMediator, String userName) { super(chattingMediator, userName); this.chattingMediator = chattingMediator; this.userName = userName; } @Override public void sendMessage(String message) { System.out.println(userName + " sending message = "+message); chattingMediator.sendMessage(message, this); } @Override public void receiveMessage(String message) { System.out.println(userName + " received message = "+message); } } |
ClientTest.java
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 33 34 35 36 |
package com.kkjavatutorials.client; import com.kkjavatutorials.chatting.ChattingMediator; import com.kkjavatutorials.chatting.ChattingMediatorImpl; import com.kkjavatutorials.chatting.User; import com.kkjavatutorials.chatting.UserImpl; /** * @author KK JavaTutorials *Client program which shows how Mediator *Design Pattern we use in Group Chat Application. */ public class ClientTest { public static void main(String[] args) { ChattingMediator chattingMediator = new ChattingMediatorImpl(); //Created few Users User user1 = new UserImpl(chattingMediator, "KK"); User user2 = new UserImpl(chattingMediator, "PK"); User user3 = new UserImpl(chattingMediator, "MK"); User user4 = new UserImpl(chattingMediator, "SK"); User user5 = new UserImpl(chattingMediator, "VK"); //Adding many users in Group Chat chattingMediator.addUser(user1); chattingMediator.addUser(user2); chattingMediator.addUser(user3); chattingMediator.addUser(user4); chattingMediator.addUser(user5); //User PK sending message in Group Chat. user2.sendMessage("Hi,All"); } } |
Output of this Program:
PK sending message = Hi,All
KK received message = Hi,All
MK received message = Hi,All
SK received message = Hi,All
VK received message = Hi,All
Above output shows message send my User: PK, Received all other Users in Group Chat
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Bridge Design Pattern in Java.
Chain of Responsibility Design Pattern in Java
Template Design Pattern in Java
That’s all about the Mediator Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.