eaglewap Data Scientist | Software Engineer | Google Code U Alumni | Former WSO2 Software Engineering Intern | "Google Code U"Alumni | Former WSO2 Software Engineering Intern. No comments

Mediator Design Pattern

•Define an object that encapsulates how a set of objects interact
•Promotes loose coupling by keeping objects from referring to each other explicitly
•Lets you vary their interaction independently
Mediator Design Pattern

Purpose

  • Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. 
  • Allows for the actions of each object set to vary independently of one another.

Use When

  • n Communication between sets of objects is well defined and complex.
  • n Too many relationships exist and common point of control or communication is needed.

Class diagram for Mediator Design Pattern

Mediator

  • Defines simplified communication between classes
  • This is the scenario where there should be a mediator to pass the message to all others. It's how group chat works. Here is the sample java code. Try to do it your own before seeing it.
Example Java Code for Mediator 😊
ChatMediator.java
1
2
3
4
public interface ChatMediator {
    public void sendMessage(String msg, User user);
    void addUser(User user);
}
ChatMediatorImpl.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
import java.util.ArrayList;
import java.util.List;

public class ChatMediatorImpl implements ChatMediator {

    private List<User> users;

    public ChatMediatorImpl() {
        this.users = new ArrayList<>();
    }

    @Override
    public void addUser(User user) {
        this.users.add(user);
    }

    @Override
    public void sendMessage(String msg, User user) {
        for (User u : this.users) {
            //message should not be received by the user sending it
            if (u != user) {
                u.receive(msg);
            }
        }
    }

}
User.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public abstract class User {
    protected ChatMediator mediator;
    protected String name;
     
    public User(ChatMediator med, String name){
        this.mediator=med;
        this.name=name;
    }
     
    public abstract void send(String msg);
     
    public abstract void receive(String msg);
}
UserImpl.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class UserImpl extends User {
  
    public UserImpl(ChatMediator med, String name) {
        super(med, name);
    }
 
    @Override
    public void send(String msg){
        System.out.println(this.name+": Sending Message="+msg);
        mediator.sendMessage(msg, this);
    }
    @Override
    public void receive(String msg) {
        System.out.println(this.name+": Received Message:"+msg);
    }
 
}
ChatClient.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class ChatClient {
  
    public static void main(String[] args) {
        ChatMediator mediator = new ChatMediatorImpl();
        User user1 = new UserImpl(mediator, "a");
        User user2 = new UserImpl(mediator, "b");
        User user3 = new UserImpl(mediator, "c");
        User user4 = new UserImpl(mediator, "d");
        mediator.addUser(user1);
        mediator.addUser(user2);
        mediator.addUser(user3);
        mediator.addUser(user4);
         
        user1.send("Hi All");
         
    }
 
}

Comments (0)

Post a Comment

Cancel