What is the intermediary mode?
Object-oriented design encourages the dispersion of behaviors into different objects, which can lead to the correlation between objects. In the worst case, all objects understand each other and manipulate each other.
While the spread of behavior to different objects enhances reusability, the increased interconnectedness reduces the benefits gained. An increased association makes it difficult or impossible for an object to work without relying on other objects. The overall behavior of the application can be difficult to make any significant modifications, because the behavior is distributed across many objects. The result may be that more and more subclasses are created to support any new behavior in the application.
Mediator mode: An object that encapsulates the way a series of objects interact. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them.
When to use the broker mode?
@: Interactions between objects are well defined and complex, resulting in a set of objects that depend on each other and are difficult to understand.
@: Objects are difficult to reuse because they reference many other objects and communicate with them.
@: You want to customize a logic or behavior that is distributed across multiple classes, and you don't want to generate too many subclasses.
Examples of application of the mediator pattern:
If pilots are scrambling to take off or land at the airport without traffic control, there will be immeasurable danger. To avoid this situation, pilots need to know each other's intentions and location. Here's how to know each other's location to illustrate the application of the broker pattern. As shown, the relationship between them is indicated.
Abstract Abstractradarmediator defines the general behavior used to interact with plane. Concreteradarmediator defines a more specific behavior for plane, so it is possible to subclass Abstractradarmediator and apply various plane interaction algorithms to the same or different plane types. Sometimes abstract mediator can be omitted if the application requires only one mediator.
An instance of plane has a reference to an mediator instance, and the instance of Abstractradarmediator knows every object that participates. The code for plane is as follows:
#import <Foundation/Foundation.h> @class abstractradarmediator;//import "AbstractRadarMediator.h", can't write like this, Otherwise there will be mutual import, resulting in error prompts, header file cannot find @interface Plane:nsobject@property (nonatomic, assign) float distance; Distance from Airport @property (nonatomic, assign) float otherplanedistance; Location of other aircraft @property (nonatomic, strong) Abstractradarmediator *radarmediator; Keep the reference of the radar Mediator-(void) noticelocation; @end
#import "Plane.h" @implementation plane-(void) noticelocation {NSLog (@ "notifies other aircraft of their own location");} @end
The Noticelocation method in the plane class is augmented by its subclasses, which allows you to define multiple behaviors. The code for Planea and Planeb is as follows:
#import "Plane.h" @interface planea:plane-(void) noticelocation; Override this method @end
#import "PlaneA.h" #import "AbstractRadarMediator.h" @implementation planea-(void) Noticelocation {[ Self.radarmediator noticeLocationToPlaneBWithDistance:self.distance];} @end
#import "Plane.h" @interface planeb:plane-(void) noticelocation; Override this method @end
#import "PlaneB.h" #import "AbstractRadarMediator.h" @implementation planeb-(void) Noticelocation {[ Self.radarmediator noticeLocationToPlaneAWithDistance:self.distance];} @end
Here we can see the implementation of Noticelocation, passing the message to another object in the way of the mediator, the advantage is that the objects do not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them. In Abstractradarmediator defines the general behavior of interacting with plane objects, the code is as follows:
#import <Foundation/Foundation.h> #import "Plane.h" @interface AbstractRadarMediator : NSObject@property (nonatomic, Strong) Plane *planeA; @property (nonatomic, strong) Plane *planeB;- ( Instancetype) Initwithplanea: (plane *) Planea planeb: (plane *) planeb;- (void) Noticelocationtoplaneawithdistance: (float) distance;- (void) Noticelocationtoplanebwithdistance: (float) Distance; @end
#import "AbstractRadarMediator.h" @implementation AbstractRadarMediator- (Instancetype) Initwithplanea: (plane *) Planea planeb: (plane *) planeb { self = [super init]; if (self) { _planeA = planeA; _planeb = planeb; } return self;} - (void) Noticelocationtoplaneawithdistance: (float) Distance { nslog (@ "subclass to implement") ;} - (void) Noticelocationtoplanebwithdistance: (float) Distance { nslog (@ "subclass to implement") ;} @end
here noticelocationtoplaneawithdistance is defined: (float) distance and noticelocationtoplanebwithdistance two ways of interacting, but specifically how to interact, is completed by its corresponding subclass, which makes it more flexible and convenient to provide various interactive algorithms for plane objects. The code in the Concreteradarmediator class is as follows:
#import "AbstractRadarMediator.h" @interface concreteradarmediator:abstractradarmediator//Override these two methods-(void) Noticelocationtoplaneawithdistance: (float) distance;-(void) Noticelocationtoplanebwithdistance: (float) distance;@ End
#import "ConcreteRadarMediator.h" @implementation concreteradarmediator-(void) noticelocationtoplaneawithdistance :(float) Distance {self.planeA.otherPlaneDistance = distance;} -(void) Noticelocationtoplanebwithdistance: (float) Distance {self.planeB.otherPlaneDistance = distance;} @end
So far, the broker pattern has been described above, and below we look at how the client uses the broker pattern and the convenience of the broker pattern, the code is as follows:
#import "ViewController.h" #import "Plane.h" #import "PlaneA.h" #import "PlaneB.h" #import "AbstractRadarMediator.h" #import "ConcreteRadarMediator.h" @interface ViewController () @ end@implementation viewcontroller- (void) viewdidload { [super viewdidload]; plane *planea = [[planea alloc] init]; planeb *planeb = [[planeb alloc] init]; AbstractRadarMediator *radarMediator = [[ConcreteRadarMediator alloc] initwithplanea:planea planeb:planeb]; planea.radarmediator = radarmediator; //to keep Abstractradarmediator references planeb.radarmediator = radarMediator; planeA.distance = 800.0f; planeb.distance = 1000.0f; [planea noticelocation]; nslog (@ "Airplane B receives a position notification:%f", planeb.otherplanedistance); [planeb noticelocation]; nslog (@ "Airplane A receives B's position notice:%f", planea.otherplanedistance); }- (void) didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated.} @end
the output ends as follows:
2015-09-01 09:44:42.236 mediatorpatter[12407:407240] Airplane B receives a position notification: 800.0000002015-09-01 09:44:42.237 MediatorPatter[ 12,407:407,240] Aircraft a receives B's position notice: 1000.000000
Here we can see that there is no mutual reference between the Planea and Planeb two objects, but the communication between the two objects is achieved, thus reducing the association between the two, thus making it loosely coupled, which is the convenience of using the mediator pattern.
Demo Link Address: https://github.com/guoshimeihua/MediatorPatter
iOS design mode-Mediator mode