iOS 生成器模式(简单使用)

2017-03-08  本文已影响389人  印林泉

生成器协议

//
//  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
上一篇 下一篇

猜你喜欢

热点阅读