• Objective-C设计模式——生成器Builder(对象创建)


    生成器

    生成器,也成为建造者模式,同样是创建对象时的设计模式。该模式下有一个Director(指挥者),客户端知道该类引用用来创建产品。还有一个Builder(建造者),建造者知道具体创建对象的细节。

    指挥者只是起到协调的作用,它有一个Builder引用用于指挥Builder如何创建对象,Builder内就是为了构建对象的算法。

    其实生成器模式,就是为了定制某些特定对象而产生的模式。对象的过长往往非常的复杂,如果不是用该模式来创建对象,对象创建类将会非常的冗余且难以复用。使用生成器将表现层和创建层分离更加易于扩展和解耦。

    使用场景

    1.需要创建涉及各种部件的复杂对象。创建对象的算法应该独立于部件的装配方式。常见的例子是构建组合对象。

    2.构建过程需要以不同的方式(例如,部件或表现的不同组合)构建对象。

    生成器与抽象工厂对比

    生成器

    构建复杂对象

    以多个步骤构建对象

    在构建对象过程的最后一步返回产品

    专注于一个特定产品

    抽象工厂

    构建简单或复杂对象

    以单一步骤构建对象

    以单一方式构建对象

    立刻返回产品

    强调一套产品

    Demo

    比如汽车制造厂需要制造两种不同的汽车:

    首相创建Car类,有name和speed两个属性

    #import <Foundation/Foundation.h>
    
    @interface Car : NSObject
    
    @property(nonatomic, strong)NSString *name;
    
    @property(nonatomic ,assign)int speed;
    
    @end
    
    #import "Car.h"
    
    @implementation Car
    
    @end

    创建Builder,Builder封装了创建类的具体细节

    #import <Foundation/Foundation.h>
    #import "Car.h"
    
    @interface CarBuilder : NSObject
    
    -(CarBuilder *)buildCar;
    -(CarBuilder *)buildCarName;
    -(CarBuilder *)buildCarSpeed;
    -(Car *)getProduct;
    
    
    @end
    
    #import "CarBuilder.h"
    
    @implementation CarBuilder
    {
        Car *_product;
    
    }
    -(CarBuilder *)buildCar
    {
        _product =  nil;
        _product = [Car new];
        return self;
    }
    
    
    -(CarBuilder *)buildCarName
    {
        _product.name = @"benz";
        return self;
    }
    
    
    -(CarBuilder *)buildCarSpeed
    {
        _product.speed = 100;
        return self;
    }
    
    @end

    具体的Builder类:

    #import <Foundation/Foundation.h>
    #import "CarBuilder.h"
    
    @interface CarBuilderBenz : CarBuilder
    
    @end
    
    #import "CarBuilderBenz.h"
    
    @implementation CarBuilderBenz
    {
        Car *_product;
    }
    
    -(CarBuilder *)buildCar
    {
        _product =  nil;
        _product = [Car new];
        return self;
    }
    
    
    -(CarBuilder *)buildCarName
    {
        _product.name = @"benz";
        return self;
    }
    
    
    -(CarBuilder *)buildCarSpeed
    {
        _product.speed = 100;
        return self;
    }
    
    -(Car *)getProduct
    {
        return _product;
    }
    
    @end
    
    #import <Foundation/Foundation.h>
    #import "CarBuilder.h"
    @interface CarBuilderBMW : CarBuilder
    
    @end
    
    #import "CarBuilderBMW.h"
    
    @implementation CarBuilderBMW
    {
        Car *_product;
    }
    
    -(CarBuilder *)buildCar
    {
        _product =  nil;
        _product = [Car new];
        return self;
    }
    
    
    -(CarBuilder *)buildCarName
    {
        _product.name = @"BMW";
        return self;
    }
    
    
    -(CarBuilder *)buildCarSpeed
    {
        _product.speed = 120;
        return self;
    }
    
    -(Car *)getProduct
    {
        return _product;
    }
    @end

    Director类,Director知道生产产品,但并不知道具体生产细节。

    #import <Foundation/Foundation.h>
    #import "CarBuilder.h"
    #import "Car.h"
    
    @interface CarDirector : NSObject
    
    -(Car *)createCarBenz:(CarBuilder *)builder;
    -(Car *)createCarBMW:(CarBuilder *)builder;
    
    @end
    
    #import "CarDirector.h"
    
    @implementation CarDirector
    
    -(Car *)createCarBenz:(CarBuilder *)builder
    {
        [builder buildCar];
        [builder buildCarName];
        [builder buildCarSpeed];
        return [builder getProduct];
    }
    
    -(Car *)createCarBMW:(CarBuilder *)builder
    {
        [builder buildCar];
        [builder buildCarName];
        [builder buildCarSpeed];
        return [builder getProduct];
    }
    
    @end

    客户端代码:

            CarDirector *director = [CarDirector new];
            CarBuilderBenz *benzBuilder = [CarBuilderBenz new];
            CarBuilderBMW *BMWBuilder = [CarBuilderBMW new];
            Car *benz = [director createCarBenz:benzBuilder];
            Car *bmw = [director createCarBMW:BMWBuilder];
            NSLog(@"benz: name = %@ ,speed = %d",benz.name ,benz.speed);
            NSLog(@"BMW: name = %@ ,speed = %d",bmw.name ,bmw.speed);

    结果:

    2015-07-21 09:15:04.298 Builder[41745:10008190] benz: name = benz ,speed = 100
    2015-07-21 09:15:04.298 Builder[41745:10008190] BMW: name = BMW ,speed = 120

    可以看到,不同的builder封装不同的创建对象逻辑,产生不同的对象,同时分离了创建层和表示层。

  • 相关阅读:
    免费的视频、音频转文本
    Errors are values
    Codebase Refactoring (with help from Go)
    Golang中的坑二
    Cleaner, more elegant, and wrong(msdn blog)
    Cleaner, more elegant, and wrong(翻译)
    Cleaner, more elegant, and harder to recognize(翻译)
    vue控制父子组件渲染顺序
    computed 和 watch 组合使用,监听数据全局数据状态
    webstorm破解方法
  • 原文地址:https://www.cnblogs.com/madpanda/p/4663415.html
Copyright © 2020-2023  润新知