视频教程-极客学院
ps:感觉打的一手好广告啊,因为自己不太爱看视频,但这类又必须看才能明白。粘贴源代码是为了以后查阅方便,也注释了自己的理解。
如同我们熟悉现实中的中介,多个顾客把要售卖的房子交给中介,然后中介进行管理,顾客说房子要涨一点,那么中介那里的信息也会要更新。同时,顾客也会去找其他中介。一个中介会有多个顾客。一个顾客也可能会有多个中介。
AbstractColleague(Mediator)
抽象的中介者
#import <Foundation/Foundation.h>
#import "AbstractColleague.h"
/**
抽象的中介类,因为要和顾客签订协议,所以在抽象类定义了顾客的协议
他说涨就涨,说降就降,从来不顾中介的感受
*/
@interface AbstractMediator : NSObject <ColleagueDelegate>
@end
#import "AbstractMediator.h"
@implementation AbstractMediator
//顾客的协议,空实现
- (void)colleagueEvent:(AbstractColleague *)event {
}
@end
AbstractColleague(Colleague)
抽象的同事类 ;顾客
#import <Foundation/Foundation.h>
@class AbstractColleague;
#顾客的协议
@protocol ColleagueDelegate <NSObject>
@optional
/**
* colleague对象的交互
*
* @param event Colleague对象
*/
- (void)colleagueEvent:(AbstractColleague *)event;
@end
@interface AbstractColleague : NSObject
@property (nonatomic, weak) id <ColleagueDelegate> delegate;
@end
#import "AbstractColleague.h"
@implementation AbstractColleague
@end
TypeOneMediator(ConcreteMediator)
具体的中介者
#import "AbstractMediator.h"
#import "Colleague.h"
@interface TypeOneMediator : AbstractMediator //这里继承父类的好处是:不必有很多类型中介写多次协议父类已经对通用的方法做了约束
//三位顾客
@property (nonatomic, strong) Colleague *colleagueA;
@property (nonatomic, strong) Colleague *colleagueB;
@property (nonatomic, strong) Colleague *colleagueC;
/**
* 查看信息
*
* @return Colleague相关的信息
*/
- (NSDictionary *)values;
@end
#import "TypeOneMediator.h"
@implementation TypeOneMediator
- (void)colleagueEvent:(AbstractColleague *)event {
//定义他们自己的价格
if ([event isEqual:self.colleagueA]) {
self.colleagueB.value = self.colleagueA.value * 2;
self.colleagueC.value = self.colleagueA.value * 4;
} else if ([event isEqual:self.colleagueB]) {
self.colleagueA.value = self.colleagueB.value / 2.f;
self.colleagueC.value = self.colleagueB.value * 2.f;
} else {
self.colleagueA.value = self.colleagueC.value / 4.f;
self.colleagueB.value = self.colleagueC.value / 2.f;
}
}
- (NSDictionary *)values {
return @{@"A" : @(self.colleagueA.value),
@"B" : @(self.colleagueB.value),
@"C" : @(self.colleagueC.value)};
}
@end
Colleague(ConcreteColleague)
具体的顾客
#import <UIKit/UIKit.h>
#import "AbstractColleague.h"
@interface Colleague : AbstractColleague
@property (nonatomic) CGFloat value;
/**
* 修改数值
*
* @param value 数值
*/
- (void)changeValue:(CGFloat)value;
@end
#import "Colleague.h"
@implementation Colleague
- (void)changeValue:(CGFloat)value {
self.value = value;
if (self.delegate && [self.delegate respondsToSelector:@selector(colleagueEvent:)]) {
[self.delegate colleagueEvent:self];
}
}
@end
实现
#import "ViewController.h"
#import "TypeOneMediator.h"
#import "TypeTwoMediator.h"
#import "Colleague.h"
@interface ViewController ()
@property (nonatomic, strong) TypeTwoMediator *mediator;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
self.mediator = [TypeTwoMediator new]; //类型二的中介者 可以随时修改
Colleague *colleagueA = [Colleague new]; //客户1
Colleague *colleagueB = [Colleague new]; //客户2
Colleague *colleagueC = [Colleague new]; //客户3
//3个客户进入中介
self.mediator.colleagueA = colleagueA;
self.mediator.colleagueB = colleagueB;
self.mediator.colleagueC = colleagueC;
//并和中介签订协议,由中介来完成
colleagueA.delegate = self.mediator;
colleagueB.delegate = self.mediator;
colleagueC.delegate = self.mediator;
//顾客修改了信息,中介也需要对信息更新
[colleagueA changeValue:2.f];
NSLog(@"%@", [self.mediator values]);
[colleagueA changeValue:1.f];
NSLog(@"%@", [self.mediator values]);
[colleagueA changeValue:3.f];
NSLog(@"%@", [self.mediator values]);
}
网友评论