iOS設計模式 - 產生器

來源:互聯網
上載者:User

標籤:

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設計模式 - 產生器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.