• iOS设计模式


    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

    细节

  • 相关阅读:
    [转]fedora国内源常见配置
    [转]Fedora 添加国内源(sohu, 163)
    keepalived
    lvs详解
    ansible详解
    练习题:计算传入字符串中的【数字】、【字母】、【空格】和【其他】的个数
    面向对象(继承、重写、多态)以及异常处理
    面向对象详解以及封装案例解答
    进程、线程与GIL全局解释器锁详解
    python函数与模块(paramiko、socket模块、文件处理、装饰器、迭代器等)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4740407.html
Copyright © 2020-2023  润新知