版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.04.29 |
前言
前面讲了23种设计模式中的前几个,下面我们继续,先看前几篇文章。
1. 23种设计模式(一)
2. 23种设计模式(二)
3. 23种设计模式(三)
4. 23种设计模式(四)
5. 23种设计模式(五)
6. 23种设计模式(六)
7. 23种设计模式(七)
8. 23种设计模式(八)
9. 23种设计模式(九)
详述
十九、职责链模式——Chain of Responsibility
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,将这个对象连成一条链传递该请求,直到有一个对象处理它为止。先看一下原理图。
看代码组织结构。
看代码。
1. ViewController.m
#import "ViewController.h"
#import "HCDMnager.h"
#import "HCDCommonManager.h"
#import "HCDMajorManager.h"
#import "HCDGenaralManager.h"
#import "HCDReuquest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDCommonManager *common = [[HCDCommonManager alloc]initWithName:@"猪经理"];
HCDMajorManager *majon = [[HCDMajorManager alloc]initWithName:@"狗总监"];
HCDGenaralManager *genaral = [[HCDGenaralManager alloc]initWithName:@"蛤蟆总经理"];
common.superior = majon;
majon.superior = genaral;
HCDReuquest *request = [[HCDReuquest alloc]init];
request.requestType = @"请假";
request.number = 5;
[common dealRequest:request];
request.number = 20;
[common dealRequest:request];
request.number = 220;
[common dealRequest:request];
request.number = 1200;
[common dealRequest:request];
}
@end
2. HCDMnager.h
#import
@class HCDMnager,HCDReuquest;
@interface HCDMnager : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) HCDMnager *superior;
- (instancetype)initWithName:(NSString *)name;
- (void)dealRequest:(HCDReuquest *)request;
@end
3. HCDMnager.m
#import "HCDMnager.h"
#import "HCDReuquest.h"
@implementation HCDMnager
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self) {
_name = name;
}
return self;
}
- (void)dealRequest:(HCDReuquest *)request
{
}
@end
4. HCDReuquest.h
#import
@interface HCDReuquest : NSObject
@property (nonatomic, copy) NSString *requestType;
@property (nonatomic, assign) NSInteger number;
@end
5. HCDReuquest.m
#import "HCDReuquest.h"
@implementation HCDReuquest
@end
6. HCDCommonManager.h
#import "HCDMnager.h"
@interface HCDCommonManager : HCDMnager
@end
7. HCDCommonManager.m
#import "HCDCommonManager.h"
#import "HCDReuquest.h"
@implementation HCDCommonManager
- (void)dealRequest:(HCDReuquest *)request
{
if ([request.requestType isEqualToString:@"请假"] && request.number < 10) {
NSLog(@"%@处理了%@,时间是%ld",self.name,request.requestType,request.number);
}
else{
[self.superior dealRequest:request];
}
}
@end
8. HCDMajorManager.h
#import "HCDMnager.h"
@interface HCDMajorManager : HCDMnager
@end
9. HCDMajorManager.m
#import "HCDMajorManager.h"
#import "HCDReuquest.h"
@implementation HCDMajorManager
- (void)dealRequest:(HCDReuquest *)request
{
if ([request.requestType isEqualToString:@"请假"] && request.number < 50) {
NSLog(@"%@处理了%@,时间是%ld",self.name,request.requestType,request.number);
}
else{
[self.superior dealRequest:request];
}
}
@end
10. HCDGenaralManager.h
#import "HCDMnager.h"
@interface HCDGenaralManager : HCDMnager
@end
11. HCDGenaralManager.m
#import "HCDGenaralManager.h"
#import "HCDReuquest.h"
@implementation HCDGenaralManager
- (void)dealRequest:(HCDReuquest *)request
{
if ([request.requestType isEqualToString:@"请假"] && request.number < 500) {
NSLog(@"%@处理了%@,时间是%ld",self.name,request.requestType,request.number);
}
else{
NSLog(@"%@不同意%@,时间是%ld",self.name,request.requestType,request.number);
}
}
@end
看结果。
2017-04-29 10:58:55.169 19职责链模式[1184:25440] 猪经理处理了请假,时间是5
2017-04-29 10:58:55.170 19职责链模式[1184:25440] 狗总监处理了请假,时间是20
2017-04-29 10:58:55.170 19职责链模式[1184:25440] 蛤蟆总经理处理了请假,时间是220
2017-04-29 10:58:55.170 19职责链模式[1184:25440] 蛤蟆总经理不同意请假,时间是1200
结论:思想。
二十、中介者模式——Mediator
用一个中介对象来封装一系列的对象交互,中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。先看一下原理示例图。
下面看一下代码结构图。
下面看代码。
1. ViewController.m
#import "ViewController.h"
#import "HCDColleagueA.h"
#import "HCDColleagueB.h"
#import "HCDAbstractMediator.h"
#import "HCDMediator.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDAbstractColleague *colleagueA = [[HCDColleagueA alloc]init];
HCDAbstractColleague *colleagueB = [[HCDColleagueB alloc]init];
HCDAbstractMediator *mediator = [[HCDMediator alloc]initWithColleagueA:colleagueA colleagueB:colleagueB];
colleagueA.mediator = mediator;
colleagueB.mediator = mediator;
colleagueA.number = 1450;
colleagueB.number = 1450;
[colleagueA notice];
NSLog(@"%f---%f",colleagueA.number,colleagueB.number);
colleagueB.number = 123;
[colleagueB notice];
NSLog(@"%f---%f",colleagueA.number,colleagueB.number);
}
@end
2. HCDAbstractColleague.h
#import
#import
@class HCDAbstractMediator;
@interface HCDAbstractColleague : NSObject
@property (nonatomic, assign) CGFloat number;
- (void)notice;
@property(nonatomic,strong) HCDAbstractMediator *mediator;
@end
3. HCDAbstractColleague.m
#import "HCDAbstractColleague.h"
#import "HCDAbstractMediator.h"
@implementation HCDAbstractColleague
@end
4. HCDMediator.h
#import
#import "HCDAbstractMediator.h"
@interface HCDMediator : HCDAbstractMediator
@end
5. HCDMediator.m
#import "HCDMediator.h"
@implementation HCDMediator
@synthesize colleagueB = _colleagueB;
@synthesize colleagueA = _colleagueA;
- (instancetype)initWithColleagueA:(HCDAbstractColleague *)colleagueA colleagueB:(HCDAbstractColleague *)colleagueB
{
self = [super init];
if (self) {
_colleagueA = colleagueA;
_colleagueB = colleagueB;
}
return self;
}
- (void)setValueToColleagueA:(CGFloat)value
{
self.colleagueA.number = value * 100;
}
- (void)setValueToColleagueB:(CGFloat)value
{
self.colleagueB.number = value * 0.01;
}
@end
6. HCDColleagueA.h
#import "HCDAbstractColleague.h"
@interface HCDColleagueA : HCDAbstractColleague
@end
7. HCDColleagueA.m
#import "HCDColleagueA.h"
#import "HCDAbstractMediator.h"
@implementation HCDColleagueA
- (void)notice
{
[self.mediator setValueToColleagueB:self.number];
}
@end
8. HCDColleagueB.h
#import "HCDAbstractColleague.h"
@interface HCDColleagueB : HCDAbstractColleague
@end
9. HCDColleagueB.m
#import "HCDColleagueB.h"
#import "HCDAbstractMediator.h"
@implementation HCDColleagueB
- (void)notice
{
[self.mediator setValueToColleagueA:self.number];
}
@end
10. HCDAbstractMediator.h
#import
#import "HCDColleagueB.h"
#import "HCDColleagueA.h"
@interface HCDAbstractMediator : NSObject
- (instancetype)initWithColleagueA:(HCDAbstractColleague *)colleagueA colleagueB:(HCDAbstractColleague *)colleagueB;
@property (nonatomic, weak) HCDAbstractColleague *colleagueA;
@property (nonatomic, weak) HCDAbstractColleague *colleagueB;
- (void)setValueToColleagueA:(CGFloat)value;
- (void)setValueToColleagueB:(CGFloat)value;
@end
11. HCDAbstractMediator.m
#import "HCDAbstractMediator.h"
@implementation HCDAbstractMediator
@end
看结果。
2017-04-29 11:23:14.332 20中介者模式[1615:39216] 1450.000000---14.500000
2017-04-29 11:23:14.333 20中介者模式[1615:39216] 12300.000000---123.000000
结论:思想。
后记
今天是五一放假的第一天,起来的有点晚,大约十点多才起床,起来了以后才想起来有一篇还没写,就接着写了,下午再写一篇,给23种设计模式一个了结,再次谢谢大家。