Objective-C生成器模式

假如有以下实体类

实体类声明(.h)
@interface ZDWCar : NSObject

@property (nonatomic, copy) NSString *brand;
@property (nonatomic, copy) NSString *country;
@property (nonatomic, strong) UIColor *color;
@property (nonatomic, assign) CGFloat price;
此处省略n多property...

通常可以有以下创建方法:

方法1:
ZDWCar *myCar = [[ZDWCar alloc] init];
    myCar.brand = @"wzd";
    myCar.country = @"China";
    myCar.color = [UIColor redColor];
    myCar.price = 111000;
    其它property赋值...

这种方法的缺点是时间久了会记不住这个类到底有哪些property需要在创建时初始化,尤其是这个类是有他人提供时,如果没有注释将会变得十分痛苦😳。

方法2:

类创建者在实体类中添加多个init方法,比如:

- (instancetype)initWithBrand:(NSString *)brand;
- (instancetype)initWithBrand:(NSString *)brand country:(NSString *)country;
- (instancetype)initWithBrand:(NSString *)brand country:(NSString *)country color:(UIColor *)color;
- (instancetype)initWithBrand:(NSString *)brand country:(NSString *)country color:(UIColor *)color price:(CGFloat)price;
...

这种创建方法清晰地告诉调用者到底有哪些property需要在创建时初始化。但是,当类的提供者有一天告诉你他增加了一个属性,init方法也变了。这时,你是不是想死?还有如果当需要初始化的property比较多时,一排排的init岂不是很壮观😡?

当某实体类的属性值很多时,特别是当类将来是要提供给其他人用时,可以使用生成器模式:

在声明文件(.h)中添加

typedef void(^ZDWCarBulderBlock)(ZDWCarBuilder *builder);


+(instancetype)createWithBuilder:(ZDWCarBulderBlock)block;

在定义(.m)文件中添加

+(instancetype)createWithBuilder:(ZDWCarBulderBlock)block {
    NSParameterAssert(block);
    ZDWCarBuilder *builder = [[ZDWCarBuilder alloc] init];
    block(builder);
    return [builder build];
}
生成器类声明(.h)
@interface ZDWCarBuilder : NSObject

@property (nonatomic, copy) NSString *brand;
@property (nonatomic, copy) NSString *country;
@property (nonatomic, strong) UIColor *color;
@property (nonatomic, assign) CGFloat price;

- (ZDWCar *)build;
生成器类定义(.m)
- (ZDWCar *)build {
    //设置brand和country为必须初始化的属性,不传会整段垮掉,😝
    NSParameterAssert(self.brand);
    NSParameterAssert(self.country);
    
    ZDWCar *car = [[ZDWCar alloc] init];
    car.brand = self.brand;
    car.country = self.country;
    car.color = self.color;
    car.price = self.price;
    return car;
}

调用方实例

ZDWCar *myCar = [ZDWCar createWithBuilder:^(ZDWCarBuilder *builder) {
        builder.brand = @"wzd";
        builder.country = @"China";
        builder.color = [UIColor redColor];
        builder.price = 111000;
    }];
    原文作者:杭州凤梨
    原文地址: https://www.jianshu.com/p/28fc9c3566a9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞