前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS设计模式 (九)构建者模式

iOS设计模式 (九)构建者模式

作者头像
且行且珍惜_iOS
发布2021-09-07 16:47:19
6840
发布2021-09-07 16:47:19
举报
文章被收录于专栏:iOS开发攻城狮的集散地
概念

建造者模式是设计模式的一种,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

UML

9.png

构建者模式由三部分构成

  • 指挥者 (Director)
  • 构建者 (Builder)
  • 生成者 (ConcreteBuilder)
项目需求

客户想要店主冲一杯带糖,加珍珠,加牛奶,加椰果的奶茶

  • 指挥者:店主
  • 构建者:奶茶
  • 生成者:糖,珍珠,牛奶,椰果
demo实例
  • TeaBuilder (协议)
代码语言:javascript
复制
#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 类
代码语言:javascript
复制
#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 (协议)
代码语言:javascript
复制
#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
代码语言:javascript
复制
#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
代码语言:javascript
复制
#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 (客户端)
代码语言:javascript
复制
/**
 客户想要店主冲一杯带糖,加珍珠,加牛奶,加椰果的奶茶
 指挥者:店主
 构建者:奶茶
 生成者:糖,珍珠,牛奶,椰果
 */

#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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-5-12 1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概念
  • UML
  • 构建者模式由三部分构成
  • 项目需求
  • demo实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档