注:为了更清楚的解释,类的成员函数均采用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");
}