2924. Design Pattern - Mediator
Mediator Pattern


Behavioral Pattern: Mediator Pattern.

1. Mediator

The Mediator pattern is used to provide a centralized communication medium between different objects in a system.

2. Example

2.1 User

public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(String name){
        this.name  = name;
    }

    public void sendMessage(String message){
        ChatRoom.showMessage(this,message);
    }
}

2.2 ChatRoom

public class ChatRoom {
    public static void showMessage(User user, String message){
        System.out.println(new Date().toString() + " [" + user.getName() + "] : " + message);
    }
}

2.3 Client

public class Client {
    public void run() {
        User robert = new User("Robert");
        User john = new User("John");

        robert.sendMessage("Hi! John!");
        john.sendMessage("Hello! Robert!");
    }
}

Output.

Fri Nov 30 16:08:46 PST 2018 [Robert] : Hi! John!
Fri Nov 30 16:08:46 PST 2018 [John] : Hello! Robert!

3. Source Files

4. References