Design Pattern > Behavioral Design Pattern > Mediator Design Pattern
There are scenarios when we create lots of classes which interact or communicate among each other and going forward most of the classes have many to many relation.
Here the problem is that now application become difficult to maintaine, beacause change in once class may require change in several other classes too and this violates the principle of high cohesion and loose coupling.
To resolve this problem we can use mediator design pattern where a mediator class work as a central hub for communicating classes and defince a set of rules for them to communicate. By this mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
Benefits of Using Mediator Pattern :
Key Terms :
Mediator - defines an interface for communicating with Colleague objects.
ConcreteMediator - knows the colleague classes and keep a reference to the colleague objects.
- implements the communication and transfer the messages between the colleague classes
Colleague classes - keep a reference to its Mediator object
- communicates with the Mediator whenever it would have otherwise communicated with another Colleague.
Java Implementation :
/**
* Mediator Interface
*/
package com.jaistutorial.mediator;
import java.util.List;
public interface ChatRoom {
void registerUser(User collegue);
void BroadcastMessage(User collegue,String message);
}
/**
* Mediator Class
*/
package com.jaistutorial.mediator;
import java.util.ArrayList;
import java.util.List;
public class ChatRoomeImpl implements ChatRoom {
List colleagueList = new ArrayList();
/* (non-Javadoc)
* @see com.jaistutorial.mediator.ChatRoom#registerUser(com.jaistutorial.mediator.User)
*/
public ChatRoomeImpl(String chatRoomName){
System.out.println(chatRoomName + " initialized");
}
@Override
public void registerUser(User collegue) {
System.out.println("regestering new collegue "+ collegue.getname());
colleagueList.add(collegue);
}
/* (non-Javadoc)
* @see com.jaistutorial.mediator.ChatRoom#BroadcastMessage(com.jaistutorial.mediator.User, java.lang.String)
*/
@Override
public void BroadcastMessage(User senderCollegue, String message) {
for(User user : colleagueList){
if(user!=senderCollegue){ // sender should not receive his own message
user.receiveMessage(message);
}
}
}
}
/**
* Colleague Interface
*/
package com.jaistutorial.mediator;
public interface User {
void setname(String name);
String getname();
void sendMessage(ChatRoom mediator,String message);
void receiveMessage(String message);
}
/**
* Colleague Class
*/
package com.jaistutorial.mediator;
public class UserImpl implements User {
private String userName;
/* (non-Javadoc)
* @see com.jaistutorial.mediator.User#sendMessage(com.jaistutorial.mediator.ChatRoom, java.lang.String)
*/
@Override
public void sendMessage(ChatRoom mediator, String message) {
mediator.BroadcastMessage(this, message);
}
/* (non-Javadoc)
* @see com.jaistutorial.mediator.User#receiveMessage(java.lang.String)
*/
@Override
public void receiveMessage(String message) {
System.out.println(this.getname() + " received message : " + message);
}
@Override
public void setname(String name) {
this.userName = name;
}
@Override
public String getname() {
return userName;
}
}
/**
* Client Class
*/
package com.jaistutorial.mediator;
public class MediatorDemo {
public static void main(String[] args) {
// create a mediator - chatroom1
ChatRoomeImpl chatroom1 = new ChatRoomeImpl("chatroom1");
// create colleagues
UserImpl user1 = new UserImpl();
user1.setname("user1");
UserImpl user2 = new UserImpl();
user2.setname("user2");
UserImpl user3 = new UserImpl();
user3.setname("user3");
UserImpl user4 = new UserImpl();
user4.setname("user4");
// register colleagues for chatroom1(mediator)
chatroom1.registerUser(user1);
chatroom1.registerUser(user2);
chatroom1.registerUser(user3);
// user1 sends message to chatroom1(mediator) users(colleagues)
user1.sendMessage(chatroom1, "hello from user1");
// create another mediator - chatroom2
ChatRoomeImpl chatroom2 = new ChatRoomeImpl("chatroom2");
// register colleagues for chatroom2(mediator)
chatroom2.registerUser(user3);
chatroom2.registerUser(user4);
// user3 sends message to chatroom1(mediator) users(colleagues)
user3.sendMessage(chatroom2, "hello from user3");
}
}
Output :
chatroom1 initialized
regestering new collegue user1
regestering new collegue user2
regestering new collegue user3
user2 received message : hello from user1
user3 received message : hello from user1
chatroom2 initialized
regestering new collegue user3
regestering new collegue user4
user4 received message : hello from user3
There are scenarios when we create lots of classes which interact or communicate among each other and going forward most of the classes have many to many relation.
Here the problem is that now application become difficult to maintaine, beacause change in once class may require change in several other classes too and this violates the principle of high cohesion and loose coupling.
To resolve this problem we can use mediator design pattern where a mediator class work as a central hub for communicating classes and defince a set of rules for them to communicate. By this mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
Benefits of Using Mediator Pattern :
- The mediator encapsulate the logic of mediation between the colleagues. so its easy to maintain, understand and change the communication logic.
- The colleague classes(classes in communication) are totally decoupled. Adding a new colleague class is very easy due to this decoupling level.
- The colleague objects need to communicate only with the mediator objects. Practically the mediator pattern reduce the required communication channels(protocols) from many to many to one to many and many to one.
Key Terms :
Mediator - defines an interface for communicating with Colleague objects.
ConcreteMediator - knows the colleague classes and keep a reference to the colleague objects.
- implements the communication and transfer the messages between the colleague classes
Colleague classes - keep a reference to its Mediator object
- communicates with the Mediator whenever it would have otherwise communicated with another Colleague.
Java Implementation :
/**
* Mediator Interface
*/
package com.jaistutorial.mediator;
import java.util.List;
public interface ChatRoom {
void registerUser(User collegue);
void BroadcastMessage(User collegue,String message);
}
/**
* Mediator Class
*/
package com.jaistutorial.mediator;
import java.util.ArrayList;
import java.util.List;
public class ChatRoomeImpl implements ChatRoom {
List
/* (non-Javadoc)
* @see com.jaistutorial.mediator.ChatRoom#registerUser(com.jaistutorial.mediator.User)
*/
public ChatRoomeImpl(String chatRoomName){
System.out.println(chatRoomName + " initialized");
}
@Override
public void registerUser(User collegue) {
System.out.println("regestering new collegue "+ collegue.getname());
colleagueList.add(collegue);
}
/* (non-Javadoc)
* @see com.jaistutorial.mediator.ChatRoom#BroadcastMessage(com.jaistutorial.mediator.User, java.lang.String)
*/
@Override
public void BroadcastMessage(User senderCollegue, String message) {
for(User user : colleagueList){
if(user!=senderCollegue){ // sender should not receive his own message
user.receiveMessage(message);
}
}
}
}
/**
* Colleague Interface
*/
package com.jaistutorial.mediator;
public interface User {
void setname(String name);
String getname();
void sendMessage(ChatRoom mediator,String message);
void receiveMessage(String message);
}
/**
* Colleague Class
*/
package com.jaistutorial.mediator;
public class UserImpl implements User {
private String userName;
/* (non-Javadoc)
* @see com.jaistutorial.mediator.User#sendMessage(com.jaistutorial.mediator.ChatRoom, java.lang.String)
*/
@Override
public void sendMessage(ChatRoom mediator, String message) {
mediator.BroadcastMessage(this, message);
}
/* (non-Javadoc)
* @see com.jaistutorial.mediator.User#receiveMessage(java.lang.String)
*/
@Override
public void receiveMessage(String message) {
System.out.println(this.getname() + " received message : " + message);
}
@Override
public void setname(String name) {
this.userName = name;
}
@Override
public String getname() {
return userName;
}
}
/**
* Client Class
*/
package com.jaistutorial.mediator;
public class MediatorDemo {
public static void main(String[] args) {
// create a mediator - chatroom1
ChatRoomeImpl chatroom1 = new ChatRoomeImpl("chatroom1");
// create colleagues
UserImpl user1 = new UserImpl();
user1.setname("user1");
UserImpl user2 = new UserImpl();
user2.setname("user2");
UserImpl user3 = new UserImpl();
user3.setname("user3");
UserImpl user4 = new UserImpl();
user4.setname("user4");
// register colleagues for chatroom1(mediator)
chatroom1.registerUser(user1);
chatroom1.registerUser(user2);
chatroom1.registerUser(user3);
// user1 sends message to chatroom1(mediator) users(colleagues)
user1.sendMessage(chatroom1, "hello from user1");
// create another mediator - chatroom2
ChatRoomeImpl chatroom2 = new ChatRoomeImpl("chatroom2");
// register colleagues for chatroom2(mediator)
chatroom2.registerUser(user3);
chatroom2.registerUser(user4);
// user3 sends message to chatroom1(mediator) users(colleagues)
user3.sendMessage(chatroom2, "hello from user3");
}
}
Output :
chatroom1 initialized
regestering new collegue user1
regestering new collegue user2
regestering new collegue user3
user2 received message : hello from user1
user3 received message : hello from user1
chatroom2 initialized
regestering new collegue user3
regestering new collegue user4
user4 received message : hello from user3
No comments:
Post a Comment