博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS设计模式 - 生成器
阅读量:4537 次
发布时间:2019-06-08

本文共 3139 字,大约阅读时间需要 10 分钟。

iOS设计模式 - 生成器

 

原理图

 

说明

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

 

源码

////  VehicleBuilder.h//  BuilderPattern////  Created by YouXianMing on 15/8/18.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import 
#import "VehicleBuilderProtocol.h"@interface VehicleBuilder : NSObject
/** * 车辆信息 */@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 
@protocol VehicleBuilderProtocol
@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 
#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

你可能感兴趣的文章
[工具] BurpSuite--XssValidator插件
查看>>
LPC1788系统时钟初始化
查看>>
channel vs mutex
查看>>
页面布局(--FlowLayout,--BorderLayout,--GridLayout)
查看>>
实验吧--web--你真的会php吗
查看>>
vue组件化学习第二天
查看>>
网络枚举工具推荐
查看>>
003LeetCode--LongestSubstring
查看>>
quarzt(官方)---给自己看的文档(SchedulerListeners)-8
查看>>
Linux-慕课网学习笔记-3-1命令格式
查看>>
AJAX入门介绍
查看>>
[算法竞赛入门]第一章_算法概述
查看>>
SQL反模式笔记3——主键规范
查看>>
简单粗暴,微生物生态研究中常用数据库简介--转载
查看>>
Oracle -操作数据库
查看>>
c - 给分数分级别
查看>>
chrome 调试
查看>>
luoguP2774 方格取数问题
查看>>
tcp/ip协议各层的理解与
查看>>
python中的setdefault()方法
查看>>