C++实现设计模式:Builder Pattern


注:为了更清楚的解释,类的成员函数均采用inline。

#ifndef _BUILDER_H
#define _BUILDER_H

#include "Product.h"
#include <iostream>

using std::cout;
using std::endl;
using std::string;

class Product;
class Builder
{
public:
    virtual ~Builder(){}
    
    virtual Product getProduct()=0{}
protected:
    virtual void builderPartA(){}
    virtual void builderPartB(){}
    virtual void builderPartC(){}
};

class ConcreteBuilder_One:public Builder
{
public:
    ConcreteBuilder_One()
    {
        _product = NULL;
    }
    
    ~ConcreteBuilder_One()
    {
        if(_product)
        {
            delete _product;
            _product = NULL;
        } 
    }
    
    virtual Product getProduct()
    {
        if(_product)
        {
            delete _product;
            _product = NULL;
        }
        
        _product = new Product();
        builderPartA();
        builderPartB();
        builderPartC();
        return *_product;
    }

protected:
    void builderPartA()
    {
        _product->setPartA(string("One_A"));
        cout<<"Build product partA:"<<_product->getPartA()<<endl;
    }
    
    void builderPartB()
    {
        _product->setPartB(string("One_B"));
        cout<<"Build product partB:"<<_product->getPartB()<<endl;
    }
    
    void builderPartC()
    {
        _product->setPartC(string("One_C"));
        cout<<"Build product partC:"<<_product->getPartC()<<endl;
    }

private:
    Product* _product;
};

class ConcreteBuilder_Two:public Builder
{
public:
    ConcreteBuilder_Two()
    {
        _product = NULL;
    }

    ~ConcreteBuilder_Two()
    {
        if(_product)
        {
            delete _product;
        }
    }
    
    virtual Product getProduct()
    {
        if(_product)
        {
            delete _product;
        }
        
        _product = new Product();
        builderPartA();
        builderPartB();
        builderPartC();
        return *_product;
    }

protected:
    void builderPartA()
    {
        _product->setPartA(string("Two_A"));
        cout<<"Build product partA:"<<_product->getPartA()<<endl;
    }
    void builderPartB()
    {
        _product->setPartB(string("Two_B"));
        cout<<"Build product partB:"<<_product->getPartB()<<endl;
    }
    void builderPartC()
    {
        _product->setPartC(string("Two_C"));
        cout<<"Build product partC:"<<_product->getPartC()<<endl;
    }

private:
    Product* _product;
};

#endif


#ifndef _DIRECTOR_H
#define _DIRECTOR_H

#include "Builder.h"

class Product;
class Director
{
public:
    explicit Director(Builder* builder):_builder(builder)
    {
        //
    }
    
    virtual ~Director()
    {
        //
    }
    
    void setbuilder(Builder* builder)
    {
        _builder = builder;
    }

    Product ConstuctProduct()
    {
        return _builder->getProduct();
    }
    
private:
    Builder* _builder;
};

#endif


#ifndef _PRODUCT_H
#define _PRODUCT_H

#include <string>
#include <iostream>

using std::string;

class Product
{
public:
    void print()
    {
        std::cout<<"Product:"<<_partA<<","<<_partB<<","<<_partC<<std::endl;
    }
    void setPartA(std::string& part){_partA = part;}
    void setPartB(std::string& part){_partB = part;}
    void setPartC(std::string& part){_partC = part;}
    const string getPartA(){return _partA;}
    const string getPartB(){return _partB;}
    const string getPartC(){return _partC;}

private:
    std::string _partA;
    std::string _partB;
    std::string _partC;
};

#endif



#include "Director.h"
#include "Builder.h"
#include <iostream>

using std::cout;
using std::endl;

int main()
{
    Builder* p_builder_one = new ConcreteBuilder_One();
    Director* dirc = new Director(p_builder_one);
    Product one = dirc->ConstuctProduct();
    
    cout<<"The parts of produced product:"<<endl;
    cout<<one.getPartA()<<endl;
    cout<<one.getPartB()<<endl;
    cout<<one.getPartC()<<endl;
    
    Builder* p_builder_two = new ConcreteBuilder_Two();
    dirc->setbuilder(p_builder_two);
    Product two = dirc->ConstuctProduct();
    
    cout<<"The parts of produced product:"<<endl;
    cout<<two.getPartA()<<endl;
    cout<<two.getPartB()<<endl;
    cout<<two.getPartC()<<endl;

    delete p_builder_one;
    delete dirc;
    //
    system("PAUSE");
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值