建造者模式

定义

    将复杂对象的构建和其表示分离,使得同样的构建过程可以创建不同的表示

主要解决

    一个复杂对象的创建工作,由各个部分的子对象用一定的算法构成;由于需求变化,这个复杂对象的各个部分经常面临变化,但将它们组合在一起的算法却相对稳定。

如何解决

    将变与不变分开

关键代码

    建造者:创建和提供实例,Director:管理建造出来的实例的依赖关系

缺点

   1、产品必须有共同点,范围有限制。 

   2、如内部变化复杂,会有很多的建造类

typedef enum  
{  
    type1,  
    type2  
}ProductType;  
  
class Product   //产品  
{  
public:  
    void setNum(int num);  
    void setColor(string color);  
    void setType(ProductType type);  
  
    void showProduct();  
private:  
    int m_num;  
    string m_color;  
    ProductType m_type;  
  
};  
  
void Product::setNum(int num)  
{  
    m_num = num;  
}  
  
void Product::setColor(string color)  
{  
    m_color = color;  
}  
  
void Product::setType(ProductType type)  
{  
    m_type = type;  
}  
  
void Product::showProduct()  
{  
    cout << "Product: " << endl;  
    cout << "       num  : " << m_num << endl;  
    cout << "       color: " << m_color.data() << endl;  
    cout << "       type : " << m_type << endl;  
}  
  
//建造者父类,定义接口  
class Builder  
{  
public:  
    Builder(){}  
    virtual ~Builder(){}  
    virtual void buildNum(int num) = 0;  
    virtual void buildColor(string color) = 0;  
    virtual void buildType(ProductType type) = 0;  
    virtual void createProduct() = 0;  
    virtual Product* getProduct() = 0;  
    virtual void show() = 0;  
};  
  
//建造者A  
class BuilderA:public Builder  
{  
public:  
    BuilderA(){}  
     ~BuilderA(){}  
    void buildNum(int num) override;  
    void buildColor(string color) override;  
    void buildType(ProductType type) override;  
    void createProduct() override;  
    Product* getProduct() override;  
    void show() override;  
private:  
    Product* m_product;  
};  
  
void BuilderA::buildNum(int num)  
{  
    cout << "BuilderA build Num: " << num << endl;  
    m_product->setNum(num);  
}  
  
void BuilderA::buildColor(string color)  
{  
    cout << "BuilderA build color: " << color.data() << endl;  
    m_product->setColor(color);  
}  
  
void BuilderA::buildType(ProductType type)  
{  
    cout << "BuilderA build type: " << type << endl;  
    m_product->setType(type);  
}  
  
void BuilderA::createProduct()  
{  
    cout << "BuilderA CreateProduct: " << endl;  
    m_product = new Product();  
}  
  
Product* BuilderA::getProduct()  
{  
    return m_product;  
}  
void BuilderA::show()  
{  
    m_product->showProduct();  
}  
  
//建造者B  
class BuilderB:public Builder  
{  
public:  
    BuilderB(){}  
     ~BuilderB(){}  
    void buildNum(int num) override;  
    void buildColor(string color) override;  
    void buildType(ProductType type) override;  
    void createProduct() override;  
    Product* getProduct() override;  
    void show() override;  
private:  
    Product* m_product;  
};  
  
void BuilderB::buildNum(int num)  
{  
    cout << "BuilderB build Num: " << num << endl;  
    m_product->setNum(num);  
}  
  
void BuilderB::buildColor(string color)  
{  
    cout << "BuilderB build color: " << color.data() << endl;  
    m_product->setColor(color);  
}  
  
void BuilderB::buildType(ProductType type)  
{  
    cout << "BuilderB build type: " << type << endl;  
    m_product->setType(type);  
}  
  
void BuilderB::createProduct()  
{  
    cout << "BuilderB CreateProduct: " << endl;  
    m_product = new Product();  
}  
  
Product* BuilderB::getProduct()  
{  
    return m_product;  
}  
void BuilderB::show()  
{  
    m_product->showProduct();  
}  
  
//管理类,负责安排构造的具体过程  
class Director  
{  
public:  
    Director(Builder* builder):m_builder(builder)  
    {  
    }  
    void construct(int num, string color, ProductType type)  
    {  
        m_builder->createProduct();  
        m_builder->buildNum(num);  
        m_builder->buildColor(color);  
        m_builder->buildType(type);  
    }  
  
private:  
    Builder* m_builder;  
};  

参考:

https://www.yuque.com/docs/share/83ab9eb1-f8ed-45e3-9965-6157015bd2fc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值