Java 23 design patterns-19. Behavioral pattern intermediary pattern

In addition to the design principles in Java, there are also 23 design patterns.

These patterns have been accumulated by the predecessors bit by bit, have been improving, and have been optimizing, and these design patterns can solve some specific problems.

And in these modes, it can be said that the use of language is fully reflected.

Then we are going to learn   the intermediary model in the   behavioral model today     !

Intermediary model

Let’s start with an encyclopedia, what is the intermediary model?

Very good, but there is no gain, here I will talk about what is called the intermediary model

The intermediary model, as the name suggests, must first have an intermediary, that is, a third party, this third party is mainly used for "maintenance"

Example: It’s like looking for a house. We can’t go to a city to find a house, because we don’t know where there are vacant houses in this city. If we ask one by one, then when will we have to ask? At that time, there was an intermediary who could tell us that there are houses there, so that we can save trouble, and if the owner changes the price or something, we can all know the news directly.

The definition and characteristics of the intermediary model

The definition of the Mediator mode: define a mediator object to encapsulate the interaction between a series of objects, loosen the coupling between the original objects, and independently change the interaction between them. The intermediary model is also called the mediation model, which is a typical application of Dimit's law.

The intermediary model is an object behavior model, and its main advantages are as follows.

1. Reduce the coupling between objects, making the objects easy to be reused independently.

2. Change the one-to-many association between objects into one-to-one association, improve the flexibility of the system, and make the system easy to maintain and expand.

The main disadvantage is: when there are too many colleagues, the intermediary's responsibilities will be very large, it will become complex and large, so that the system is difficult to maintain.

 

Here I will talk about Dimit's law

Dimit's Law

The law of Dimit is defined as: if two software entities do not need to communicate directly, then there should be no direct mutual call, and the call can be forwarded through a third party. Its purpose is to reduce the degree of coupling between classes and improve the relative independence of modules.

In layman's terms, it is the relationship between me, WeChat and bank cards. I used to take a lot of bank cards when I went out. Now I only need to have a WeChat. Pay directly through WeChat to deduct my money.

For details, please see my blog: Java Object-Oriented Design Principle 6-Dimit's Law

The structure and realization of the intermediary model

1. Role

Let’s first take a look at those characters participating in this mode

1. Abstract intermediary role: it is the interface of the intermediary and provides abstract methods for colleague object registration and forwarding colleague object information.

2. Specific intermediary role: implement the intermediary interface, define a List to manage colleague objects, and coordinate the interaction between each colleague role, so it depends on the colleague role.

3. Abstract colleague class 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.

4. Specific colleague role: it is the implementer of the abstract colleague class. When it is necessary to interact with other colleague objects, the intermediary object is responsible for subsequent interactions.

 

After knowing the role, let’s write an example. This example is an example of this housing agency.

first step:

Let’s first define the abstract intermediary. This is actually the housing intermediary. We also need to define two methods in this abstract class, namely the user registration and forwarding methods.

 

The second step:

Let's get another implementer, here we need to implement these two methods

 

But let’s not realize it, let’s put it like this first

third step:

We define an abstract colleague class, which is the customer

Of course, this alone is not enough, we need to add some methods

Of course, we need to have an intermediary to send information, so here we also define the house intermediary and give a set method

 

 

the fourth step:

Create a specific implementation colleague class, here we are the specific user specified

Let's get customer A and customer B

 

But you will get an error when calling repay, here we go to modify the method of our house agency

OK

the fifth step:

Then get customer B yourself, and call the same code as customer A

The sixth step:

Here we begin to write the business code implemented by the specific intermediary

We first need a List collection to store these users

 

Then we started to write the specific business code. Before writing, we can see that neither the register method nor the repay method has our client object. Here, we need to modify the abstract intermediary method again.

Next, we store our customers in the list in register, and call the set method to pass the intermediary to the specific user implementation class

Then when we repay the notification, we judge whether it is the current object, if not, we call its receive method

test:

We first define our intermediary, then define two customers, and register to this intermediary

Then we let a send a price increase

Look at the result

Of course you can make a distinction here. For example, A says: B receives: so that you can distinguish clearly

 

OK, that's it, everyone take a good look. Practice a lot. If you have any questions, please contact me QQ: 2100363119

Welcome everyone to visit my personal website: lemon1234.com Thank you for leaving a message

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/109657682
Recommended