zoukankan      html  css  js  c++  java
  • iOS设计模式

    iOS设计模式 - 生成器

    原理图

    说明

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

     复制代码

    //
    //  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
    复制代码

    细节

  • 相关阅读:
    C#设计模式——抽象工厂模式(原文转自:http://blog.jobbole.com/78059/)
    WebConfig配置文件详解(转载自逆心的博客)
    ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换(转自happymagic的专栏)
    ASP.NET RepeatLayout 属性
    牛顿迭代法
    汉诺塔(整理)
    游戏引擎---好牛(转)
    字符串相关面试题(整理)
    有关java调用批处理文件
    有关java 8
  • 原文地址:https://www.cnblogs.com/ming1025/p/6655884.html
Copyright © 2011-2022 走看看