Explain the use of mediator Mediator mode in the design pattern development of IOS application _ios

Source: Internet
Author: User

What is an intermediary pattern?

Object-oriented design encourages the spread of behavior across different objects, which can lead to the correlation of objects. In the worst-case scenario, all objects understand each other and manipulate each other.

Although the spread of behavior to different objects enhances reusability, the increased correlation 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 may be difficult to make any significant changes 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 interaction of a series of objects. Intermediaries do not need to refer explicitly to each other, so that they are loosely coupled and can independently alter their interactions.

When to use mediator mode?

1. The interaction between objects, though well-defined and complex, results in a group of objects that are dependent on one another and are difficult to understand.

2. Objects are difficult to reuse because they refer to many other objects and communicate with them.

3. You want to customize a logic or behavior that is distributed across multiple classes, and do not want to generate too many subclasses.

Example of an implementation of the broker pattern:
The following first gives the class structure diagram, then makes the simple explanation.

The mediator pattern is easily referenced in the system, but it is also easier to misuse. Therefore, when the system appears "Many-to-many" Interactive complex object group, do not rush to use intermediary mode, but first to reflect on the design of the system is not reasonable.

Here's what we say about the pros and cons of the broker model. The advantage of intermediaries first is that the presence of mediator reduces the coupling of each colleague, allowing the individual colleague classes and mediator to be changed and reused independently. Secondly, because of the abstraction of the object's collaboration, the intermediary is taken as an independent concept and encapsulated in an object, so that the objects of concern are transferred from the object's own behavior to the interaction between them, that is, standing in a more macroscopic angle to view the system.

Relatively speaking, the shortcomings are also obvious. Because Concretemediator controls centralization, the interaction complexity becomes the mediator's complexity, which makes the broker more complex than any concretecolleage. So once the concretemediator crashes, the whole system will be affected.

Or the old saying, there is no silver bullet, the right one is the best!

Below to give you a simple demonstration of concrete implementation.

Note: All of the code in this article is compiled in an arc environment.

Mediator class Interface

Copy Code code as follows:

#import <Foundation/Foundation.h>

@class colleague;
@interface Mediator:nsobject
-(void) Send: (nsstring*) message
:(colleague*) colleague;
@end


Mediator class implementation
Copy Code code as follows:

#import "Mediator.h"

@implementation Mediator
-(void) Send: (NSString *) Message:(colleague *) colleague{
Return
}
@end


Colleague Class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

@class Mediator;
@interface colleague:nsobject{
Mediator *mymediator;
}
-(colleague*) Myinit: (mediator*) mediator;
@end


Colleague Class implementation
Copy Code code as follows:

#import "Colleague.h"

@implementation Colleague
-(colleague*) Myinit: (Mediator *) mediator{
if (self = = [Super init]) {
Mymediator = Mediator;
}
return self;
}
@end


Concretemediator class Interface
Copy Code code as follows:

#import "Mediator.h"

@class ConcreteColleague1;
@class ConcreteColleague2;
@interface Concretemediator:mediator
@property concretecolleague1*colleague1;
@property concretecolleague2*colleague2;
@end


Concretemediator class implementation
Copy Code code as follows:

#import "ConcreteMediator.h"
#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"
#import "Colleague.h"

@implementation Concretemediator
@synthesize colleague1;
@synthesize colleague2;

-(void) Send: (NSString *) Message:(colleague *) colleague{
if ([colleague Iskindofclass:[concretecolleague1 class]]) {
[Colleague2 Notify:message];
}
else {
[Colleague1 Notify:message];
}
}
@end


ConcreteColleague1 class Interface
Copy Code code as follows:

#import "Colleague.h"

@class Mediator;
@interface Concretecolleague1:colleague
-(concretecolleague1*) Myinit: (mediator*) mediator;
-(void) Send: (nsstring*) message;
-(void) Notify: (nsstring*) message;
@end


ConcreteColleague1 class implementation
Copy Code code as follows:

#import "ConcreteColleague1.h"
#import "Mediator.h"

@implementation ConcreteColleague1
-(concretecolleague1*) Myinit: (mediator*) mediator{
if (self = = [Super init]) {
Mymediator = Mediator;
}
return self;
}
-(void) Send: (NSString *) message{
[Mymediator Send:message:self];
}
-(void) Notify: (NSString *) message{
NSLog (@ "ConcreteColleague1 got message:%@", message);
}
@end


ConcreteColleague2 class Interface
Copy Code code as follows:

#import "Colleague.h"

@class Mediator;
@interface Concretecolleague2:colleague
-(concretecolleague2*) Myinit: (mediator*) mediator;
-(void) Send: (nsstring*) message;
-(void) Notify: (nsstring*) message;
@end
ConcreteColleague2 class implementation

#import "ConcreteColleague2.h"
#import "Mediator.h"

@implementation ConcreteColleague2
-(concretecolleague2*) Myinit: (mediator*) mediator{
if (self = = [Super init]) {
Mymediator = Mediator;
}
return self;
}
-(void) Send: (NSString *) message{
[Mymediator Send:message:self];
}
-(void) Notify: (NSString *) message{
NSLog (@ "ConcreteColleague2 got message:%@", message);
}
@end


Main method call
Copy code code as follows:

#import <Foundation/Foundation.h>
#import "ConcreteMediator.h"
#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"

int main (int argc,const char * argv[])
{
@autoreleasepool {
Concretemediator *m = [[Concretemediator alloc]init];
ConcreteColleague1 *C1 = [[ConcreteColleague1 alloc]myinit:m];
ConcreteColleague2 *C2 = [[ConcreteColleague2 alloc]myinit:m];
[M SETCOLLEAGUE1:C1];
[M SETCOLLEAGUE2:C2];
[C1 send:@ "Good Morning"];
[C2 send:@ "Good Afternoon"];
}
return 0;
}


Completed!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.