iOS 生成器模式(簡(jiǎn)單使用)

  • 備忘錄模式
    建造過程的模塊處理,組合性好。
    指揮者(實(shí)施參數(shù)要求/協(xié)議),抽象實(shí)現(xiàn)類(承包),具體實(shí)現(xiàn)類(實(shí)施),產(chǎn)品。

  • 應(yīng)用,使用場(chǎng)景
    建造房子的承包商
    制造汽車的流程

生成器協(xié)議

//
//  BuilderProtocol.h
//  LearnBuilder
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol BuilderProtocol <NSObject>

@required
/**
 *  構(gòu)建對(duì)象
 *
 *  @return 返回構(gòu)建的對(duì)象
 */
- (id)build;

@end

抽象模塊1接口(協(xié)議)

//
//  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接口(協(xié)議)

//
//  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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容