iOS设计模式 - 生成器

本文介绍了iOS开发中的生成器模式,通过VehicleBuilder类实现不同车辆组件的构建过程,并使用VehicleAssemblyPlant类完成整个车辆的组装。该模式适用于需要逐步构建复杂对象的场景。

iOS设计模式 - 生成器

 

原理图

 

说明

生成器模式可以理解为零部件组装工厂,与工厂方法是非常相似的!

 

源码

https://github.com/YouXianMing/iOS-Design-Patterns

//
//  VehicleBuilder.h
//  BuilderPattern
//
//  Created by YouXianMing on 15/8/18.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "VehicleBuilderProtocol.h"

@interface VehicleBuilder : NSObject <VehicleBuilderProtocol>

/**
 *  车辆信息
 */
@property (nonatomic, strong) NSMutableDictionary *vehicleInfo;

@end
//
//  VehicleBuilder.m
//  BuilderPattern
//
//  Created by YouXianMing on 15/8/18.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "VehicleBuilder.h"

@implementation VehicleBuilder

- (instancetype)init {
    
    self = [super init];
    if (self) {
    
        self.vehicleInfo = [NSMutableDictionary dictionary];
    }
    
    return self;
}

- (void)buildVehicleChassis {

    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

- (void)buildVehicleEngine {

    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

- (void)buildVehicleWheels {

    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

- (void)buildVehicleDoors {

    [NSException raise:NSInternalInconsistencyException
                format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
     [NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}

@end
//
//  VehicleBuilderProtocol.h
//  BuilderPattern
//
//  Created by YouXianMing on 15/8/18.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol VehicleBuilderProtocol <NSObject>

@required
/**
 *  制造汽车底盘
 */
- (void)buildVehicleChassis;

/**
 *  制造汽车引擎
 */
- (void)buildVehicleEngine;

/**
 *  制造汽车轮子
 */
- (void)buildVehicleWheels;

/**
 *  制造汽车车门
 */
- (void)buildVehicleDoors;

@end
//
//  VehicleAssemblyPlant.h
//  BuilderPattern
//
//  Created by YouXianMing on 15/8/18.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "VehicleBuilder.h"

/**
 *  车辆装配工厂
 */
@interface VehicleAssemblyPlant : NSObject

/**
 *  组装车辆
 *
 *  @param vehicleBuilder 组装方案
 *
 *  @return 组装好的车辆
 */
+ (VehicleBuilder *)vehicleAssembly:(VehicleBuilder *)vehicleBuilder;

@end
//
//  VehicleAssemblyPlant.m
//  BuilderPattern
//
//  Created by YouXianMing on 15/8/18.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "VehicleAssemblyPlant.h"

@implementation VehicleAssemblyPlant

+ (VehicleBuilder *)vehicleAssembly:(VehicleBuilder *)vehicleBuilder {

    [vehicleBuilder buildVehicleChassis];
    [vehicleBuilder buildVehicleDoors];
    [vehicleBuilder buildVehicleEngine];
    [vehicleBuilder buildVehicleWheels];
    
    return vehicleBuilder;
}

@end

 

细节

 

转载于:https://www.cnblogs.com/YouXianMing/p/4740407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值