Mediator Pattern:
- In our day to day Programming life, We are dealing with large set of projects In the samewhile frequently getting Changes in the existing project as well as adding new features in the project.In this total Life Cycle We are playing with huge set of objects.
- In this situation If objects are sharing their references with eachother. then It will be very difficult to maintian the project.
For this Scenario Mediator Pattern Comes to play,
Instead of eachother Communication.We introduce one Mediator who is responsible for the communication between the objects.
- Mediator holds the reference of all objects and
- Each object holds the reference of Mediator.
Example:
Group Chat is the best example for the Mediator Pattern.One Person in a group sends a message it need to deliever to all other Members in a group.
In this Situation,He need to send message to mediator and mediator is responsible to deliver message to all other members.
IGroupChatMediator.java
package com.example.mediator;
import com.example.user.User;
public interface IGroupChatMediator {
public void sendMessage(String message,User user);
public void addUser(User user);
}
GroupChatMediator.java
package com.example.mediator;
import java.util.ArrayList;
import java.util.List;
import com.example.user.User;
public class GroupChatMediator implements IGroupChatMediator{
private List<User> users;
public GroupChatMediator() {
users=new ArrayList<User>();
}
@Override
public void sendMessage(String message,User user) {
for (User eachUser : users) {
if (eachUser!=user) {
eachUser.recieveMessage(message);
}
}
}
@Override
public void addUser(User user) {
users.add(user);
}
}
AbstractUser.java
package com.example.user;
import com.example.mediator.IGroupChatMediator;
public abstract class AbstractUser {
protected IGroupChatMediator chatMediator;
protected String userName;
public String getUserName() {
return userName;
}
public AbstractUser(IGroupChatMediator groupChatMediator, String name) {
chatMediator=groupChatMediator;
userName=name;
}
public abstract void sendMessage(String message);
public abstract void recieveMessage(String message);
}
User.java
package com.example.user;
import com.example.mediator.IGroupChatMediator;
public class User extends AbstractUser{
public User(IGroupChatMediator groupChatMediator,String name) {
super(groupChatMediator, name);
}
@Override
public void sendMessage(String message) {
System.out.println(getUserName()+" sending message: "+message);
chatMediator.sendMessage(message,this);
}
@Override
public void recieveMessage(String message) {
System.out.println(getUserName()+" recieving message :"+message);
}
}
Client.java
package com.example.test;
import com.example.mediator.GroupChatMediator;
import com.example.mediator.IGroupChatMediator;
import com.example.user.User;
public class Client {
public static void main(String[] args) {
IGroupChatMediator chatMediator=new GroupChatMediator();
User user1=new User(chatMediator, "ganesh");
User user2=new User(chatMediator, "dinakar");
User user3=new User(chatMediator, "mahesh");
chatMediator.addUser(user1);
chatMediator.addUser(user2);
chatMediator.addUser(user3);
user1.sendMessage("hai all");
}
}