Mediator mode, creator of terminator!

Today, I want to discuss with you the intermediary mode, what is the intermediary mode? There are three main types of design mode, one is creation mode, the second is behavior mode, the last is structure mode, which one belongs to? After reading this article, you will understand!

Then what is the intermediary model, what scenarios it is suitable for, how to achieve it, and what are its advantages and disadvantages? Listen to Lord Hong one by one.

(Intermediator mode is used to reduce the complexity of communication between objects and objects and provide an intermediary class to interact with them. What does it mean? Initially, people wanted to exchange things by doing peer-to-peer transactions, such as A found B today and wanted to replace B with a bull head. Slowly, with the progress of the times, it gradually evolved into a fixed oneLocal transactions, where people want to trade in exchange for other partner objects, can come to a common place, which is efficient and well maintained.

Mediator mode is used in many scenarios, such as chat rooms, such as QQ, Wechat, etc. A group is an intermediary object, providing a place for people to exchange information; the World Trade Organization, the United Nations and the World Health Organization are all products of this kind.

Clearly, the mediator mode is behavioral and loosely coupled, which focuses more on communication between objects.

So what? Let's take chat rooms as an example. First we need an intermediary class, a place where everyone can interact with WeChatRoom, second we need a participant RoomUser, and then we need to implement how RoomUser communicates with each other.

Class Diagram:

Code implementation:

public class WeChatRoom {

    public static Map<RoomUser, String> userMappingGroupChat = new ConcurrentHashMap<>();

    public static void printMessage(RoomUser roomUser, String message) {
        String groupChart = userMappingGroupChat.get(roomUser);
        if(Objects.isNull(groupChart)) {
        System.out.println(new Date().toString() + " " + roomUser.getName() + "towards" + groupChart + "Send a message: " + message);

public class RoomUser {

    private String name;

    public RoomUser(String name) { = name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public void sendMessage(String message) {
        WeChatRoom.printMessage(this, message);

public class Client {
    public static void main(String[] args) {
        RoomUser baidu = new RoomUser("baidu");
        RoomUser tenCent = new RoomUser("tenCent");
        RoomUser google = new RoomUser("google");

        WeChatRoom.userMappingGroupChat.put(baidu, "Group Chat 1");
        WeChatRoom.userMappingGroupChat.put(tenCent, "Group Chat 1");
        WeChatRoom.userMappingGroupChat.put(google, "Group Chat 2");

        baidu.sendMessage("hello everybody!");
        tenCent.sendMessage("i can hear you clearly");
        google.sendMessage("i can protobuf!!!");
Mon Oct 11 23:48:20 CST 2021 baidu Send a message to Group Chat 1: hello everybody!
Mon Oct 11 23:48:20 CST 2021 tenCent Send a message to Group Chat 1: i can hear you clearly
Mon Oct 11 23:48:20 CST 2021 google Send a message to Group Chat 2: i can protobuf!!!

The advantage of the mediator pattern reduces the complexity of classes and decouples them.

Intermediator mode may not have been heard by many people, because it is a computer science and technology term published only in 2018.

Well, that's all for this chapter. I hope it helps you!

May everyone read the article with suspicion and explore its principles.

The road is blocked and long, the past is the order, the future is the chapter.

Looking forward to our next meeting!

Tags: Java Programmer

Posted on Mon, 11 Oct 2021 12:02:34 -0400 by dadamssg