Builder 模式和 AbstractFactory 模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:Builder 模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的结果对象,一般来说 Builder 模式中对象不是直接返回的。而在 AbstractFactory 模式中对象是直接返回的,AbstractFactory模式强调的是为创建多个相互依赖的对象提供一个同一的接口。
1 ////////////Product.h////////////////////// 2 #ifndef _PRODUCT_H_ 3 #define _PRODUCT_H_ 4 #include <vector> 5 #include <string> 6 class Product 7 { 8 public: 9 10 Product(); 11 ~Product(); 12 void Add( std::string part); 13 void Show(); 14 15 protected: 16 private: 17 std::vector<std::string> svec; 18 }; 19 20 #endif
1 ////////////Product.cpp////////////////////// 2 #include "Product.h" 3 #include <iostream> 4 using namespace std; 5 6 void Product::Add( string part) 7 { 8 svec.push_back(part); 9 } 10 11 void Product::Show() 12 { 13 vector<string>::iterator it = svec.begin(); 14 while (it != svec.end()) 15 { 16 cout<<*it<<" "; 17 it++; 18 } 19 cout<<endl; 20 } 21 22 Product::Product(){} 23 Product::~Product(){}
1 ////////////Builder.h////////////////////// 2 #ifndef _BUILDER_H 3 #define _BUILDER_H 4 #include <string> 5 using namespace std; 6 7 class Product ; 8 class Builder 9 { 10 public: 11 virtual ~Builder(); 12 virtual void BuildPartA() = 0 ; 13 virtual void BuildPartB() = 0 ; 14 virtual void BuildPartC() = 0 ; 15 virtual Product* GetProduct() = 0 ; 16 protected: 17 Builder(); 18 private: 19 20 }; 21 22 class ConcreteBuilder : public Builder 23 { 24 public: 25 ~ConcreteBuilder(); 26 ConcreteBuilder(); 27 28 void BuildPartA(); 29 void BuildPartB(); 30 void BuildPartC(); 31 Product* GetProduct(); 32 protected: 33 private: 34 Product* product; 35 }; 36 37 38 class ConcreteBuilder2 : public Builder 39 { 40 public: 41 ~ConcreteBuilder2(); 42 ConcreteBuilder2(); 43 44 void BuildPartA(); 45 void BuildPartB(); 46 void BuildPartC(); 47 Product* GetProduct(); 48 protected: 49 private: 50 Product* product; 51 }; 52 53 54 #endif
1 /////////Builder.cpp////////////////// 2 #include "Builder.h" 3 #include "Product.h" 4 #include <iostream> 5 using namespace std; 6 7 Builder::Builder(){} 8 Builder::~Builder(){} 9 10 ConcreteBuilder::ConcreteBuilder() 11 { 12 product = NULL; 13 } 14 ConcreteBuilder::~ConcreteBuilder() 15 { 16 17 } 18 void ConcreteBuilder::BuildPartA() 19 { 20 product = new Product(); 21 product->Add("A"); 22 cout<<"加入A"<<endl; 23 } 24 25 void ConcreteBuilder::BuildPartB() 26 { 27 product->Add("B"); 28 cout<<"加入B"<<endl; 29 } 30 31 void ConcreteBuilder::BuildPartC() 32 { 33 product->Add("C"); 34 cout<<"加入C"<<endl; 35 } 36 37 Product* ConcreteBuilder::GetProduct() 38 { 39 return product ; 40 } 41 42 43 44 ConcreteBuilder2::ConcreteBuilder2() 45 { 46 product = NULL; 47 } 48 ConcreteBuilder2::~ConcreteBuilder2() 49 { 50 51 } 52 void ConcreteBuilder2::BuildPartA() 53 { 54 product = new Product(); 55 product->Add("x"); 56 cout<<"加入x"<<endl; 57 } 58 59 void ConcreteBuilder2::BuildPartB() 60 { 61 product->Add("y"); 62 cout<<"加入y"<<endl; 63 } 64 65 void ConcreteBuilder2::BuildPartC() 66 { 67 product->Add("z"); 68 cout<<"加入z"<<endl; 69 } 70 71 Product* ConcreteBuilder2::GetProduct() 72 { 73 return product ; 74 }
1 ////////////////////////////Director.h////////////////////////////////////////////// 2 #ifndef _DIRECTOR_H 3 #define _DIRECTOR_H 4 5 class Builder; 6 class Director 7 { 8 public: 9 Director( Builder* bld); 10 11 ~Director(); 12 13 void Construct(); 14 protected: 15 private: 16 Builder* _bld ; 17 }; 18 19 #endif
1 //////////////////////////////Director.cpp//////////////////////////////////////////// 2 #include "Director.h" 3 #include "Builder.h" 4 using namespace std ; 5 6 Director::Director(Builder* bld) 7 { 8 _bld = bld ; 9 } 10 Director::~Director() 11 { 12 } 13 void Director::Construct() 14 { 15 _bld->BuildPartA(); 16 _bld->BuildPartB(); 17 _bld->BuildPartC(); 18 }
1 /////////////////////////////main.cpp///////////////////////////////////////////// 2 #include "Builder.h" 3 #include "Director.h" 4 #include "Product.h" 5 using namespace std; 6 7 int main() 8 { 9 Builder* b1 = new ConcreteBuilder(); 10 Director* d1 = new Director(b1); 11 d1->Construct(); 12 Product* p1 = b1->GetProduct(); 13 p1->Show(); 14 15 Builder* b2 = new ConcreteBuilder2(); 16 Director* d2 = new Director(b2); 17 d2->Construct(); 18 Product* p2 = b2->GetProduct(); 19 p2->Show(); 20 21 system("pause"); 22 return 0; 23 }
以上代码都是经过测试可以正常运行的哦···········