Design Pattern 8 Intermediary Pattern

background

There is such a scene, if you have many friends, friends are also friends. If your QQ number changes, all your friends need to know, you will tell your friend that your QQ number has changed, and the friend will tell his friend. In this way, all friends need to notify each other that your QQ number has changed, so that the whole body will be affected and the coupling is very great.

grid

In order to reduce the coupling between the mesh structure can be changed to star mode. An intermediary is needed here to inform all friends.

Star structure

This form later developed into an intermediary model.

What is the intermediary model

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. (Use an intermediary object to encapsulate a series of object interactions, the intermediary makes Each object does not need to interact explicitly, so that it is loosely coupled, and the interaction between them can be changed independently.)

In the intermediary model, there is an intermediary object used to encapsulate the interaction between a series of objects, which makes the coupling between the original objects looser.

Using the intermediary model can simplify the one-to-many relationship between the objects to a one-to-one relationship, which improves the flexibility of the system.

But if there are too many objects, the logic of the intermediary will be more complicated, and the maintenance of the intermediary will also require a certain cost.

The intermediary model is mainly composed of the following elements:

  • Abstract mediator (Mediator) role: It is the interface of the mediator and provides abstract methods for colleague object registration and forwarding colleague object information.

  • ConcreteMediator role: implement the mediator interface, define a List to manage colleague objects, and coordinate the interaction between the roles of colleagues, so it depends on the colleague role.

  • Abstract colleague class (Colleague) role: define the interface of colleague class, save intermediary object, provide abstract method for colleague object interaction, and realize the common functions of colleague class that affect each other.

  • Concrete Colleague role: It is the implementer of the abstract colleague class. When it needs to interact with other colleague objects, the intermediary object is responsible for the subsequent interaction.

Constituent elements

Code example

We write the code according to the relationship above

Mediator

public abstract class Mediator {
    public abstract void register(Colleague colleague);
    public abstract void relay(Colleague cl); //转发
}

ConcreteMediator

Colleague

ConcreteColleague1

Class ConcreteColleague2above.

Let's test it:

Test Results:

具体同事类1发出请求。
具体同事类2收到请求。
------------
具体同事类2发出请求。
具体同事类1收到请求。

Code explanation

First, we create a specific intermediary object, specific colleague object 1, and specific colleague object 2. Then execute:

md.register(c1);
md.register(c2);

At this time Listyou will be credited ConcreteColleague1and ConcreteColleague2objects.

Then execute:

c1.send()

send()Method internal printing [specific colleague class 1 requests. ]. Then execute the forwarding method of the intermediary:

mediator.relay(this);

This method will fetch the Listobjects in the collection excluding its caller Colleague. Then execute the receive()methods of all objects except itself .

The communication between colleagues and colleagues is handed over to the intermediary.

Thoughts on the Mode of Intermediary

When do we need to use the intermediary model?

When tight coupling occurs between multiple objects, the standard for tight coupling is: a spider web-like structure appears in the class diagram. In this case, we must consider using the intermediary model, which is conducive to combing the spider web into a star structure, making the originally complicated and chaotic relationship clear and simple.

However, only using the intermediary model also requires one's ability to do so. As mentioned at the beginning of the article, using the intermediary model will make the code logic of the intermediary class more complicated. So there needs to be a trade-off when using it.

In fact, our commonly used MVC model also uses the intermediary model, where C (Controller) is an intermediary, called the front controller (Front Controller), its role is to combine M (Model, business logic) and V (View , View) to isolate, coordinate the collaborative work of M and V, merge the results of M running and the view represented by V into a page that can be displayed on the front end, reducing the dependency between M and V.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

 

Guess you like

Origin blog.csdn.net/wujialv/article/details/109089830