1.Builder模式解决这样的问题:当要创建的对象很复杂的时候(通常是由很多其他的对象组合而成),把复杂对象的创建过程和这个对象的表示(展示)分离开来,这样做的好处就是通过一步步的进行复杂对象的构建,由于在每一步的构造过程中可以引入参数,使得经过相同的步骤创建最后得到的对象的展示不一样。
2.Builder模式结构图如下
3.实现
1 //main.cpp 2 #include "Builder.h" 3 #include "Product.h" 4 #include "Director.h" 5 6 #include <iostream> 7 using namespace std; 8 9 int main(int argc,char* argv[]) 10 { 11 Director* d = new Director(new ConcreteBuilder()); 12 13 d->Construct(); 14 15 return 0; 16 }
1 #ifndef _PRODUCT_H_ 2 #define _PRODUCT_H_ 3 class Product 4 { 5 public: 6 Product(); 7 ~Product(); 8 void ProducePart(); 9 protected: 10 private: 11 }; 12 13 class ProductPart 14 { 15 public: 16 ProductPart(); 17 ~ProductPart(); 18 ProductPart* BuildPart(); 19 protected: 20 private: 21 }; 22 23 #endif
1 #include "Product.h" 2 #include <iostream> 3 using namespace std; 4 5 Product::Product() 6 { 7 ProducePart(); 8 cout<<"return a product"<<endl; 9 } 10 11 Product::~Product() 12 { 13 14 } 15 void Product::ProducePart() 16 { 17 cout<<"build part of product.."<<endl; 18 } 19 20 ProductPart::ProductPart() 21 { 22 //cout<<"build productpart.."<<endl; 23 } 24 ProductPart::~ProductPart() 25 { 26 27 } 28 ProductPart* ProductPart::BuildPart() 29 { 30 return new ProductPart; 31 }
1 #ifndef _BUILDER_H_ 2 #define _BUILDER_H_ 3 4 #include <string> 5 using namespace std; 6 class Product; 7 8 class Builder 9 { 10 public: 11 virtual ~Builder(); 12 virtual void BuildPartA(const string& buildPara) = 0; 13 virtual void BuildPartB(const string& buildPara) = 0; 14 virtual void BuildPartC(const string& buildPara) = 0; 15 virtual Product* GetProduct() = 0; 16 protected: 17 Builder(); 18 private: 19 }; 20 21 class ConcreteBuilder:public Builder 22 { 23 public: 24 ConcreteBuilder(); 25 ~ConcreteBuilder(); 26 void BuildPartA(const string& buildPara); 27 void BuildPartB(const string& buildPara); 28 void BuildPartC(const string& buildPara); 29 Product* GetProduct(); 30 protected: 31 private: 32 }; 33 34 #endif
1 #include "Builder.h" 2 #include "Product.h" 3 #include <iostream> 4 using namespace std; 5 6 Builder::Builder() 7 { 8 9 } 10 Builder::~Builder() 11 { 12 13 } 14 ConcreteBuilder::ConcreteBuilder() 15 { 16 17 } 18 ConcreteBuilder::~ConcreteBuilder() 19 { 20 21 } 22 void ConcreteBuilder::BuildPartA(const string& buildPara) 23 { 24 cout<<"Step1:Build PartA..."<<buildPara<<endl; 25 } 26 void ConcreteBuilder::BuildPartB(const string& buildPara) 27 { 28 cout<<"Step1:Build PartB..."<<buildPara<<endl; 29 } 30 void ConcreteBuilder::BuildPartC(const string& buildPara) 31 { 32 cout<<"Step1:Build PartC..."<<buildPara<<endl; 33 } 34 Product* ConcreteBuilder::GetProduct() 35 { 36 BuildPartA("pre-defined"); 37 BuildPartB("pre-defined"); 38 BuildPartC("pre-defined"); 39 return new Product(); 40 }
1 #ifndef _DIRECTOR_H_ 2 #define _DIRECTOR_H_ 3 class Builder; 4 5 class Director 6 { 7 public: 8 Director(Builder* bld); 9 ~Director(); 10 void Construct(); 11 protected: 12 private: 13 Builder* _bld; 14 }; 15 #endif
1 #include "director.h" 2 #include "Builder.h" 3 Director::Director(Builder* bld) 4 { 5 _bld = bld; 6 } 7 Director::~Director() 8 { 9 10 } 11 void Director::Construct() 12 { 13 _bld->BuildPartA("user-defined"); 14 _bld->BuildPartB("user-defined"); 15 _bld->BuildPartC("user-defined"); 16 }