23种设计模式(五)
2017-04-27 本文已影响21人
刀客传奇
版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.04.27 |
前言
前面讲了23种设计模式中的前几个,下面我们继续,先看前几篇文章。
1. 23种设计模式(一)
2. 23种设计模式(二)
3. 23种设计模式(三)
4. 23种设计模式(四)
详述
八、建造者模式——Builder
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
先看代码组织结构。
代码组织结构看代码。
1. ViewController.m
#import "ViewController.h"
#import "HCDPersonBuilderDirector.h"
#import "HCDPersonFatBuilder.h"
#import "HCDPersonThinBuilder.h"
#import "HCDPresionBuilder.h"
typedef id<HCDPresionBuilder> HCDPresionBuilder;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDPresionBuilder builder = [[HCDPersonFatBuilder alloc]init];
HCDPersonBuilderDirector *fatDirector = [[HCDPersonBuilderDirector alloc]initWithPersonBuilder:builder];
[fatDirector buildPerson];
}
@end
2. HCDPersonBuilderDirector.h
#import <Foundation/Foundation.h>
#import "HCDPresionBuilder.h"
@interface HCDPersonBuilderDirector : NSObject
- (instancetype)initWithPersonBuilder:(id<HCDPresionBuilder>)builder;
- (void)buildPerson;
@end
3. HCDPersonBuilderDirector.m
#import "HCDPersonBuilderDirector.h"
@interface HCDPersonBuilderDirector ()
@property (nonatomic, strong) id<HCDPresionBuilder> builder;
@end
@implementation HCDPersonBuilderDirector
- (instancetype)initWithPersonBuilder:(id<HCDPresionBuilder>)builder
{
self = [super init];
if (self) {
_builder = builder;
}
return self;
}
- (void)buildPerson
{
[self.builder buildHead];
[self.builder buildBody];
[self.builder buildArmLeft];
[self.builder buildArmRight];
[self.builder buildLegLeft];
[self.builder buildLegRight];
}
@end
4. HCDPersonFatBuilder.h
#import <Foundation/Foundation.h>
#import "HCDPresionBuilder.h"
@interface HCDPersonFatBuilder : NSObject <HCDPresionBuilder>
@end
5. HCDPersonFatBuilder.m
#import "HCDPersonFatBuilder.h"
@implementation HCDPersonFatBuilder
- (void)buildHead
{
NSLog(@"建造胖子的头部");
}
- (void)buildBody
{
NSLog(@"建造胖子的身体");
}
- (void)buildArmLeft
{
NSLog(@"建造胖子的左手");
}
- (void)buildArmRight
{
NSLog(@"建造胖子的右手");
}
- (void)buildLegLeft
{
NSLog(@"建造胖子的左脚");
}
- (void)buildLegRight
{
NSLog(@"建造胖子的右脚");
}
@end
6. HCDPersonThinBuilder.h
#import <Foundation/Foundation.h>
#import "HCDPresionBuilder.h"
@interface HCDPersonThinBuilder : NSObject<HCDPresionBuilder>
@end
7. HCDPersonThinBuilder.m
#import "HCDPersonThinBuilder.h"
@implementation HCDPersonThinBuilder
- (void)buildHead
{
NSLog(@"建造瘦子的头部");
}
- (void)buildBody
{
NSLog(@"建造瘦子的身体");
}
- (void)buildArmLeft
{
NSLog(@"建造瘦子的左手");
}
- (void)buildArmRight
{
NSLog(@"建造瘦子的右手");
}
- (void)buildLegLeft
{
NSLog(@"建造瘦子的左脚");
}
- (void)buildLegRight
{
NSLog(@"建造瘦子的右脚");
}
@end
8. HCDPresionBuilder.h
#import <Foundation/Foundation.h>
@protocol HCDPresionBuilder <NSObject>
- (void)buildHead;
- (void)buildBody;
- (void)buildArmLeft;
- (void)buildArmRight;
- (void)buildLegLeft;
- (void)buildLegRight;
@end
看结果。
2017-04-26 23:26:13.864 8建造者模式[847:13428] 建造胖子的头部
2017-04-26 23:26:13.864 8建造者模式[847:13428] 建造胖子的身体
2017-04-26 23:26:13.865 8建造者模式[847:13428] 建造胖子的左手
2017-04-26 23:26:13.866 8建造者模式[847:13428] 建造胖子的右手
2017-04-26 23:26:13.866 8建造者模式[847:13428] 建造胖子的左脚
2017-04-26 23:26:13.867 8建造者模式[847:13428] 建造胖子的右脚
结论:自己注意思想吧。
九、观察者模式——Observer
定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。
看代码结构。
代码结构看代码。
1. ViewController.m
#import "ViewController.h"
#import "HCDObserver.h"
#import "HCDNBAObserver.h"
#import "HCDStockObserver.h"
#import "HCDSecreteSubject.h"
typedef id<HCDObserver> HCDObserver;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDSubject *subject = [[HCDSecreteSubject alloc]init];
//添加三个通知对象
HCDObserver nbaobserver = [[HCDNBAObserver alloc]init];
[subject attach:nbaobserver];
HCDObserver stockobserver = [[HCDStockObserver alloc]init];
[subject attach:stockobserver];
HCDObserver stockobserver1 = [[HCDStockObserver alloc]init];
[subject attach:stockobserver1];
//删除一个通知对象
[subject detach:stockobserver];
[subject notify];
}
@end
2. HCDSubject.h
#import <Foundation/Foundation.h>
#import "HCDObserver.h"
@interface HCDSubject : NSObject
- (void)attach:(id<HCDObserver>)observer;
- (void)detach:(id<HCDObserver>)observer;
- (void)notify;
- (NSArray *)getobserverList;
@end
3. HCDSubject.m
#import "HCDSubject.h"
@interface HCDSubject ()
@property (nonatomic, readwrite, strong) NSMutableArray *observerList;
@end
@implementation HCDSubject
- (instancetype)init
{
self = [super init];
if (self) {
_observerList = [NSMutableArray array];
}
return self;
}
- (void)attach:(id<HCDObserver>)observer
{
[self.observerList addObject:observer];
}
- (void)detach:(id<HCDObserver>)observer
{
for (id<HCDObserver> currentObserver in [self getobserverList]) {
if (currentObserver == observer) {
[self.observerList removeObject:observer];
}
}
}
- (NSMutableArray *)getobserverList
{
return [NSMutableArray arrayWithArray:self.observerList];
}
- (void)notify
{
}
@end
4. HCDSecreteSubject.h
#import "HCDSubject.h"
@interface HCDSecreteSubject : HCDSubject
@end
5. HCDSecreteSubject.m
#import "HCDSecreteSubject.h"
#import "HCDObserver.h"
@implementation HCDSecreteSubject
- (void)notify
{
NSLog(@"秘书通知:老板回来了,大家赶紧撤");
for (id <HCDObserver> observer in [self getobserverList]) {
[observer update];
}
}
@end
6. HCDObserver.h
#import <Foundation/Foundation.h>
@protocol HCDObserver <NSObject>
@optional
- (void)update;
@end
7. HCDStockObserver.h
#import <Foundation/Foundation.h>
#import "HCDObserver.h"
@interface HCDStockObserver : NSObject <HCDObserver>
@end
8. HCDStockObserver.m
#import "HCDStockObserver.h"
@implementation HCDStockObserver
- (void)update
{
NSLog(@"操。。老板回来了,带会儿再看");
}
@end
9. HCDNBAObserver.h
#import <Foundation/Foundation.h>
#import "HCDObserver.h"
@interface HCDNBAObserver : NSObject <HCDObserver>
@end
10. HCDNBAObserver.m
#import "HCDNBAObserver.h"
@implementation HCDNBAObserver
- (void)update
{
NSLog(@"擦,老板回来了,待会儿继续看NBA");
}
@end
看答案。
2017-04-26 23:39:50.296 9观察者模式[1086:21539] 秘书通知:老板回来了,大家赶紧撤
2017-04-26 23:39:50.296 9观察者模式[1086:21539] 擦,老板回来了,待会儿继续看NBA
2017-04-26 23:39:50.296 9观察者模式[1086:21539] 操。。老板回来了,带会儿再看
结论:自己看思想。
后记
未完,待续,谢谢大家对我的支持,希望我写的这些东西能对大家有好处,拿一张风景图养养眼吧。
沙滩风景图