標籤:
iOS設計模式 - 產生器
原理圖
說明
產生器模式可以理解為零組件組裝工廠,與Factory 方法是非常相似的!
源碼
https://github.com/YouXianMing/BuilderPattern
//// 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
細節
iOS設計模式 - 產生器