iOS设计模式 (九)构建者模式
2018-05-12 本文已影响28人
handsome5
概念
建造者模式是设计模式的一种,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
UML 9.png
构建者模式由三部分构成
- 指挥者 (Director)
- 构建者 (Builder)
- 生成者 (ConcreteBuilder)
项目需求
客户想要店主冲一杯带糖,加珍珠,加牛奶,加椰果的奶茶
- 指挥者:店主
- 构建者:奶茶
- 生成者:糖,珍珠,牛奶,椰果
demo实例
- TeaBuilder (协议)
#import <Foundation/Foundation.h>
#import "MilkGenerateProtocol.h"
@protocol TeaBuilder <NSObject>
/**
构建糖
*/
- (id <TeaBuilder>)buildSugar:(NSString *)sugar;
/**
构建牛奶
*/
- (id <TeaBuilder>)buildMilk:(NSString *)milk;
/**
构建珍珠
*/
- (id <TeaBuilder>)buildPearl:(NSString *)milk;
/**
构建椰果
*/
- (id <TeaBuilder>)buildNatadecoco:(NSString *)milk;
/**
构建喝
*/
- (id<MilkGenerateProtocol>)drink;
@end
- Director 类
#import <Foundation/Foundation.h>
#import "TeaBuilder.h"
@interface Director : NSObject
- (instancetype)initWithDirector:(id <TeaBuilder>)builder;
- (void)contractWithDirector:(NSString *)suger
milk:(NSString *)milk
peal:(NSString *)peal
natadecoco:(NSString *)natadecoco;
@end
#import "Director.h"
@interface Director ()
@property (nonatomic, strong) id <TeaBuilder> Tmilk;
@end
@implementation Director
- (instancetype)initWithDirector:(id <TeaBuilder>)builder
{
self = [super init];
if (self) {
self.Tmilk = builder;
}
return self;
}
- (void)contractWithDirector:(NSString *)suger milk:(NSString *)milk peal:(NSString *)peal natadecoco:(NSString *)natadecoco
{
[[[[[self.Tmilk buildSugar:suger] buildMilk:milk] buildPearl:peal] buildNatadecoco:natadecoco] drink];
}
@end
- MilkGenerateProtocol (协议)
#import <Foundation/Foundation.h>
@protocol MilkGenerateProtocol <NSObject>
- (void)addSuger:(NSString *)suger;
- (void)addMilk:(NSString *)milk;
- (void)addPeal:(NSString *)peal;
- (void)addNatadecoco:(NSString *)natadecoco;
- (void)addDrink;
@end
- BuildMilkyTea
#import <Foundation/Foundation.h>
#import "TeaBuilder.h"
@interface BuildMilkyTea : NSObject <TeaBuilder>
@end
#import "BuildMilkyTea.h"
#import "MilkGenerateBuilder.h"
@interface BuildMilkyTea ()
@property (nonatomic, strong)id <MilkGenerateProtocol> milk;
@end
@implementation BuildMilkyTea
- (instancetype)init
{
self = [super init];
if (self) {
self.milk = [[MilkGenerateBuilder alloc] init];
}
return self;
}
/**
构建糖
*/
- (id <TeaBuilder>)buildSugar:(NSString *)sugar
{
[_milk addSuger:sugar];
return self;
}
/**
构建牛奶
*/
- (id <TeaBuilder>)buildMilk:(NSString *)milk
{
[_milk addMilk:milk];
return self;
}
/**
构建珍珠
*/
- (id <TeaBuilder>)buildPearl:(NSString *)milk
{
[_milk addPeal:milk];
return self;
}
/**
构建糖
*/
- (id <TeaBuilder>)buildNatadecoco:(NSString *)milk
{
[_milk addNatadecoco:milk];
return self;
}
/**
构建喝
*/
- (id<MilkGenerateProtocol>)drink
{
[_milk addDrink];
return _milk;
}
@end
- MilkGenerateBuilder
#import <Foundation/Foundation.h>
#import "MilkGenerateProtocol.h"
@interface MilkGenerateBuilder : NSObject <MilkGenerateProtocol>
@end
#import "MilkGenerateBuilder.h"
@implementation MilkGenerateBuilder
- (void)addSuger:(NSString *)suger
{
NSLog(@"奶茶添加了:%@",suger);
}
- (void)addMilk:(NSString *)milk
{
NSLog(@"奶茶添加了:%@",milk);
}
- (void)addPeal:(NSString *)peal
{
NSLog(@"奶茶添加了:%@",peal);
}
- (void)addNatadecoco:(NSString *)natadecoco
{
NSLog(@"奶茶添加了:%@",natadecoco);
}
- (void)addDrink
{
NSLog(@"这是一杯超级棒的奶茶");
}
@end
- ViewController (客户端)
/**
客户想要店主冲一杯带糖,加珍珠,加牛奶,加椰果的奶茶
指挥者:店主
构建者:奶茶
生成者:糖,珍珠,牛奶,椰果
*/
#import "ViewController.h"
#import "Director.h"
#import "BuildMilkyTea.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//第一种方式
id <TeaBuilder> builder = [[BuildMilkyTea alloc] init];
//构建者
Director *director = [[Director alloc] initWithDirector:builder];
[director contractWithDirector:@"5分糖" milk:@"纯牛奶" peal:@"黑珍珠" natadecoco:@"台湾椰果"];
//第二种方式
id <TeaBuilder> builder2 = [[BuildMilkyTea alloc] init];
//构建者
id <MilkGenerateProtocol> milk = [[[[[builder2 buildSugar:@"7分糖"] buildMilk:@"酸牛奶"] buildPearl:@"大黑珍珠"] buildNatadecoco:@"台湾椰果11"] drink];
NSLog(@"milk======:%@",milk);
}
@end