备忘录模式
建造过程的模块处理,组合性好。
指挥者(实施参数要求/协议),抽象实现类(承包),具体实现类(实施),产品。应用,使用场景
建造房子的承包商
制造汽车的流程
生成器协议
//
// BuilderProtocol.h
// LearnBuilder
//
// Created by 印林泉 on 2017/3/8.
// Copyright © 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol BuilderProtocol <NSObject>
@required
/**
* 构建对象
*
* @return 返回构建的对象
*/
- (id)build;
@end
抽象模块1接口(协议)
//
// AbstractPartOne.h
// LearnBuilder
//
// Created by 印林泉 on 2017/3/8.
// Copyright © 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol AbstractPartOne<NSObject>
@required
- (void)partOneBuilder;
@end
抽象模块2接口(协议)
//
// AbstractPartTwo.h
// LearnBuilder
//
// Created by 印林泉 on 2017/3/8.
// Copyright © 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol AbstractPartTwo <NSObject>
@required
- (void)buildTree;
- (void)buildSoureWithNumber;
@end
生成器
//
// Builder.h
// LearnBuilder
//
// Created by 印林泉 on 2017/3/8.
// Copyright © 2017年 ylq. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BuilderProtocol.h"
#import "AbstractPartOne.h"
#import "AbstractPartTwo.h"
@interface Builder : NSObject
@property (nonatomic, strong) id <BuilderProtocol, AbstractPartOne> partOne;
@property (nonatomic, strong) id <BuilderProtocol, AbstractPartTwo> partTwo;
- (id)builderAll;
@end
//
// Builder.m
// LearnBuilder
//
// Created by 印林泉 on 2017/3/8.
// Copyright © 2017年 ylq. All rights reserved.
//
#import "Builder.h"
@implementation Builder
- (id)builderAll {
Builder *builder = [[[self class] alloc] init];
[builder.partOne build];
[builder.partTwo build];
return builder;
}
@end