/********************************* *设计模式--生成器实现 *C++语言 *Author:WangYong *Blog:http://www.cnblogs.com/newwy ********************************/ #include <iostream> using namespace std; class Product { public: Product(){cout<<"return a product"<<endl;} ~Product(){} void ProducePart(){cout<<"build part of product"<<endl;} }; class ProductPart { public: ProductPart(){cout<<"Build part of product"<<endl;} ~ProductPart(){} ProductPart* BuildPart(){return new ProductPart;} }; class Builder { public: virtual ~Builder(){}; virtual void BuilderPartA(const string & buildpara) = 0; virtual void BuilderPartB(const string & buildpara) = 0; virtual void BuilderPartC(const string & buildpara) = 0; virtual Product * GetProduct() = 0; }; class ConcreteBuilder:public Builder { public: ConcreteBuilder(){} ~ConcreteBuilder(){} void BuilderPartA(const string & buildpara){cout<<"step:buildparaA.."<<buildpara<<endl;} void BuilderPartB(const string & buildpara){cout<<"step:buildparaB.."<<buildpara<<endl;} void BuilderPartC(const string & buildpara){cout<<"step:buildparaC.."<<buildpara<<endl;} Product * GetProduct(); }; Product* ConcreteBuilder:: GetProduct() { BuilderPartA("pre defined"); BuilderPartB("pre defined"); BuilderPartC("pre defined"); return new Product(); } class Director { public: Director(Builder *bld){_bld = bld;} ~Director(){} void Constructor(); private: Builder *_bld; }; void Director::Constructor() { _bld->BuilderPartA("user defined"); _bld->BuilderPartB("user defined"); _bld->BuilderPartC("user defined"); } int main() { Director * d = new Director(new ConcreteBuilder()); d->Constructor(); return 0; }