In the past two years, China’s housing price has been extremely crazy, and the profit of buying and selling real estate is very substantial, so the real estate agency industry is booming. Street “XX real estate” can be seen everywhere, in the street to send leaflets chasing you a street to sell the house intermediary is common, of course, there is no less received from the intermediary harassment phone. We are not unfamiliar to the intermediary role estimate, so today’s design mode to the real estate intermediary as the story background!





No intermediaries. JPG

As can be seen from the unmediated. JPG, if there is no intermediary, the communication between buyers and sellers is so difficult and complicated. Buyers have to run to find sellers to buy houses, and sellers have to run to find buyers. If the reality is really so, estimates that the seller would have said: “Lao TZE XX not to buy (sell)!” With all this going on, is it possible for real estate to prop up the national economy as it does now?





Have a mediation. JPG

Well, with intermediary access, everything becomes so easy! Buyers want to buy a house, directly to the intermediary to see the house, the seller sells the house, directly entrust the intermediary to promote, buyers and sellers through the intermediary contact each other, communicate, and finally reach a consensus on buying and selling. Uh.. No more nonsense, now to the subject !!!!


I. Basic definitions

Definition: Encapsulates a set of object interactions with a mediator object that loosens the coupling by allowing objects to interact without explicitly, and can change their interactions independently.

In the definition of the mediator pattern, each class is considered as two broad categories, namely the mediator class and the colleague class.

This official definition may seem abstract, but try to put it into context. This definition can be divided into four sections (broker = broker, colleague = seller) :

① Encapsulate a series of object interactions with an intermediary object — it is like the intermediary has the contact information of the buyer and the seller, and provides the latest news and effective help to the buyer and seller in the process of buying and selling

(2) Intermediaries enable the objects to interact without the need for explicit interaction — both buyers and sellers will communicate their wishes to the intermediaries, through the intermediaries to communicate their wishes to each other, without the need for direct telephone contact between buyers and sellers

③ So that the coupling is loose — the link between buyers and sellers from “buyers < — > buyers” into “buyers < — > intermediary < — > buyers”, buyers and sellers of direct contact is disconnected, intermediaries become the bridge between buyers and sellers

(4) The interaction between them can be independently changed — the agent tells the seller that the house price has risen, and the seller decides to increase the price. Seeing here, some children’s shoes should have evolved from meng to “Lian such as (Meng) to this (BI)”. Ahem… Let go!!


Second, the UML

Now for the second part, please take a look at the UML diagram * 3 (say important things three times!!).





UML diagrams


Three, example code

AbstractClient.h

@class AbstractMediator;

AbstractClient is an abstract base class that encapsulates the common properties and methods of a client class

@interface AbstractClient : NSObject

@property (nonatomic,strong) AbstractMediator *mediator;

@property (nonatomic,copy)  NSString *name;

// Entrust an intermediary

– (void) entrustMediator:(AbstractMediator *)mediator;

@end

AbstractMediator.h

@class AbstractClient;

//AbstractMediator is an abstract base class that encapsulates the general properties and methods of the mediator class

@interface AbstractMediator : NSObject

@property (nonatomic,copy) NSString *name; // Agent name

@property (nonatomic,strong) NSMutableArray *clients; // Save the client

// Retain customer information

– (void) saveClintProfile:(AbstractClient *)client

MediatorLJ.h

@interface MediatorLJ : AbstractMediator

// Screen listings for buyers and match them with suitable sellers

– (void) screenHouseForBuyer:(Buyer *)buyer;

@end

The AbstractMediator and MediatorLJ classes fulfill the first point in the definition “encapsulating a set of object interactions”!

Buyer.h

@interface Buyer : AbstractClient

@property (nonatomic,assign) NSInteger money; // The buyer becomes a pair of assets

– (void) buyHouse;

@end

Seller.h

@interface Seller : AbstractClient

@property (nonatomic,assign) NSInteger price; // The value of the seller’s house

@end

main.m

// Once upon a time, there was a real estate agency called Weiheng Real Estate

MediatorLJ *weihengMdr = [[MediatorLJ alloc] init];

Name = @” 中 国 “;

// One day, Xiaomin wanted to sell a villa worth 5 million yuan, and entrusted Weiheng Real Estate to help sell it

Seller *xiaoMin = [[Seller alloc] init];

Name = @xiaomin. name;

xiaoMin.price = 5000000; // See if there are any missing 0’s, if there are, the buyer will make a lot of money

[xiaoMin entrustMediator:weihengMdr];

// Weiheng Real Estate left xiaomin information

[weihengMdr saveClintProfile:xiaoMin];

// The next day, Xiao Yan wanted to sell a staircase house worth 1 million yuan, and entrusted Weiheng Real Estate to help sell it

Seller *xiaoYan = [[Seller alloc] init];

Xiaoyan. name = @” xiaoYan “;

xiaoYan.price = 1000000;

[xiaoYan entrustMediator:weihengMdr];

[weihengMdr saveClintProfile:xiaoYan];

// On the third day, Xiao Thought about buying a house and went to Weiheng Real Estate to look for a house

Buyer *xiaoSi = [[Buyer alloc] init];

xiaoSi.money = 1500000;

XiaoSi. Name = @” xiasi “;

[xiaoSi entrustMediator:weihengMdr];

// Weiheng Real Estate left xiaosi’s information

[weihengMdr saveClintProfile:xiaoSi];

// Weiheng Real Estate matched Xiaosi’s house

[xiaoSi buyHouse];

As can be seen from the example code main.m, there is no direct connection between Seller and buyer, the original relationship of mutual influence and interdependent reference is eliminated, only communication with intermediary, and all interaction logic is concentrated in intermediary class, isn’t it nice to think about it!!

The complete demo is on GitHub, welcome to watch the learning ~ mediator mode demo


4. Model analysis

The mediator model is a relatively easy model to understand and we can easily combine it with real life. IOS developers must have used the MVC pattern, which is a manifestation of the mediator pattern. Comtroller (mediator) assumes the role of mediator and mediator between two colleague classes (View and Modle).

Advantages of the mediator pattern

(1), decoupling

In the pattern, the interaction logic of multiple colleague classes is encapsulated in the mediation class, and the colleague classes are no longer dependent on each other, and each colleague class can change independently

② Centralized management of interaction logic

When interactions between peer classes change, only the logic needs to be modified at the mediator, improving development efficiency and reducing maintenance costs

Disadvantages of the mediator pattern

The strength of the mediator pattern can become a weakness if not used well, as the mediator becomes bloated and difficult to manage as the business logic increases due to the highly centralized interaction logic.


Five, the summary

First time to write technology blog, I am so excited!! Thank you very much for your support!