Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Behavioral model: intermediary model

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The article begins with:

Behavioral model: intermediary model

The second of the eleven behavioral models: the intermediary model.

Brief introduction

Name: intermediary mode

English name: Mediator Pattern

Values: let you experience that intermediary is omnipotent.

Personal introduction:

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.

A series of object interactions are encapsulated by a mediation object, and the mediator makes the objects do not need to interact with each other explicitly, so that they are loosely coupled, and the interaction between them can be changed independently.

(from the Zen of Design patterns)

The story you want.

After reading the name of this young man, we will intuitively think of those rental agents who have taken our rent for half a month. If we don't talk about rent agents here, we won't have a chance in the future. People are now looking for a house, no matter whether they are buying or renting, as soon as they log on to Anjuke and 58.com, are 80% brokers? they say that 80% is still relatively conservative. After 4 times of looking for a house, they find that there are fewer and fewer individual houses. Every website has an option: broker housing. As shown below:

(photo taken from Anjuke website)

The broker plays the role of intermediary, which is completely consistent with the intermediary model discussed in this article. What role does the agent play when we are looking for a house? We briefly describe the role of the broker through the rental cases of individual housing and broker housing.

Personal housing source

The way we find a house through personal housing is like this: find the landlord of the personal housing source online, and then contact one by one and make an appointment with the landlord to see the house. Our relationship with the landlord is an one-to-many relationship. Xiaoming looked at the personal housing source on the Internet, contacted the landlord, and went to see the peasant house and the small area room respectively, as indicated by the code below.

Public class PersonalTest {public static void main (String [] args) {Tenant xiaoMing = new Tenant ("Xiaoming"); xiaoMing.lookAtHouse ();}} class Tenant {private String name; private XiaoQuFangLandlord xiaoQuFangLandlord2 = new XiaoQuFangLandlord (); private NongMinFangLandlord nongMinFangLandlord2 = new NongMinFangLandlord (); public Tenant (String name) {this.name = name } public void lookAtHouse () {System.out.println (this.name + "want to see the farmer's house"); nongMinFangLandlord2.supply (); System.out.println (this.name + "want to see the community house"); xiaoQuFangLandlord2.supply ();}} / * * landlord * / abstract class Landlord {/ / provide the house public abstract void supply () } class XiaoQuFangLandlord extends Landlord {@ Override public void supply () {System.out.println ("the landlord of the residential room provides a residential room");}} class NongMinFangLandlord extends Landlord {@ Override public void supply () {System.out.println ("the landlord of the farmer's house provides a residential room") }} the printed results are as follows: Xiaoming wants to see the farmer's room, the landlord of the farmer's room provides a residential room, Xiaoming wants to see the small room, the landlord of the small room provides a small room.

Xiaoming contacted the landlord of the community house and the landlord of the farmer house respectively, and then went to see the farmer house and the small area room in turn. A disadvantage of this is that Xiaoming has a strong relationship with the landlord. In fact, Xiaoming just goes to take a look at the house, but it has nothing to do with the landlord if he doesn't want to rent it. At this time, the broker came in handy. The broker's main task was to rent out the house, so he should have a strong relationship with the landlord. It was not until he successfully rented out the house that he broke away from the landlord. And Xiaoming did not have to go to the landlord to look at the house. This responsibility was transferred to the broker. Xiaoming only needed to contact one person, that is, the broker, and told him that I wanted to see the community house and the farmer's house. The agent took him to see it. The following is to introduce the way of broker housing, that is, the intermediary model that this article will talk about.

Broker housing supply

Looking for a house with a broker, Xiaoming had a lot of worries. Xiao Ming only contacted one broker and described to him the housing source he wanted: both the small area room and the farmer's room. The broker agreed with him for an afternoon. Let him see all the rooms Xiaoming wanted to see, and finally Xiaoming decided to rent a room. Look at the code.

Public class MediatorTest {public static void main (String [] args) {System.out.println ("Xiaoming wants to see community houses and farmers' houses"); Tenant2 xiaoMing = new Tenant2 ("Xiaoming", Arrays.asList ("XiaoQuFang", "NongMinFang"); xiaoMing.lookAtHouse ();}} / * * tenant * / class Tenant2 {private String name; private List wantTypes; private RentingMediator rentingMediator = new RentingMediator () Public Tenant2 (String name, List wantTypes) {this.name = name; this.wantTypes = wantTypes;} public void lookAtHouse () {rentingMediator.supplyHouse (wantTypes);} / * * intermediary Abstract Class * / abstract class Mediator {/ / public abstract void supplyHouse (List types);} / * * Rental intermediary * / class RentingMediator extends Mediator {private XiaoQuFangLandlord xiaoQuFangLandlord; private NongMinFangLandlord nongMinFangLandlord Public RentingMediator () {xiaoQuFangLandlord = new XiaoQuFangLandlord (); nongMinFangLandlord = new NongMinFangLandlord ();} @ Override public void supplyHouse (List types) {System.out.println ("the broker provided the following housing"); if (types.contains ("XiaoQuFang")) {xiaoQuFangLandlord.supply () } if (types.contains ("NongMinFang")) {nongMinFangLandlord.supply ();} print result: Xiao Ming wants to see the residential room and the farmer's house broker provides the following housing source: the landlord of the residential room provides a residential room, and the landlord of the farmer's room provides a residential room.

In the code, we can see that there is an one-to-one relationship between Xiaoming and the broker, and an one-to-many relationship between the broker and the landlord. Xiaoming's experience of looking for a house is also much easier. It only took him one afternoon to look at the house and take a fancy to it. This is also the advantage of the intermediary model, which reduces unnecessary dependencies and reduces the coupling between classes.

Code:

Mediator Pattern

Summary

By adding a layer between interdependent objects, the intermediary pattern turns previously strongly dependent objects into weak dependencies. In software programming, there is a typical example of intermediary mode, which is the MVC framework, also known as three-tier architecture, which separates the dependencies of Model (business logic layer) and View (view layer) through Controller (control layer), and coordinates the interaction between data and interface in Model and View. Look at the code in your work, think about whether there are any objects that are particularly tight and confusing, and consider whether you can split the dependencies through the intermediary pattern to make the code clearer.

Recommended reading:

Single responsibility principle (method: change the name or password? Interface: washing dishes, buying vegetables or taking out the trash? Classes: register, log in, and log out)

Richter substitution principle (my son is from New Oriental Cooking)

Rely on the principle of inversion (a stingy restaurant owner)

Interface isolation principle (young man's workshop)

Dimitt's rule (reading e-books on mobile phones)

The principle of opening and closing (social security)

Creative mode: singleton mode (Xiaoming has only one car)

Creative model: factory method (Xiaoming's garage)

Creative model: abstract factory (BMW cars have to use BMW tires and BMW steering wheels)

Creative model: builder mode (soup is so hot)

Creative model: prototype model (photocopying books)

Behavioral model: template method (sneaker manufacturing process)

The official account replied "Gift package" to get tutorials such as Java, Python, IOS, etc.

Add personal Wechat comments "tutorials" to get tutorials for architects, machine learning, etc.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report