009 --- 第13章 建造者模式
简述:
建造者模式将一个复杂的对象构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式包括:指挥者类、抽象建造者类、具体建造者类、具体产品类。
指挥者类:构建一个使用抽象建造者接口的对象。
抽象建造者类:为创建一个具体产品类对象的各个部件指定的抽象接口。
具体建造者类:实现抽象建造者类接口,构造和装配各个部件。
具体产品类:具体产品。
应用场景:主要用于创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但对象内部的构建通常面临着复杂的变化
开发环境调整为VS2017,操作系统win11
建造者模式代码:
1 #include <iostream> 2 #include <list> 3 #include <string> 4 using namespace std; 5 6 7 // 建造者模式 8 // 具体产品类 9 class CProduct 10 { 11 private: 12 list<string> m_listParts; 13 14 public: 15 void Add(string szPart) 16 { 17 m_listParts.push_back(szPart); 18 } 19 20 void Show() 21 { 22 cout << "产品创建-----" << endl; 23 for (auto szPart : m_listParts) 24 cout << szPart << endl; 25 } 26 }; 27 28 // 抽象建造者类 29 class CBuilder 30 { 31 public: 32 virtual void BuildPartA() {}; 33 virtual void BuildPartB() {}; 34 CProduct* GetResult() {}; 35 }; 36 37 // 具体建造者类 38 class CConcreteBuilder1 : public CBuilder 39 { 40 private: 41 CProduct* m_pProduct; 42 43 public: 44 CConcreteBuilder1() 45 { 46 m_pProduct = new CProduct(); 47 } 48 49 ~CConcreteBuilder1() 50 { 51 if (m_pProduct) 52 { 53 delete m_pProduct; 54 m_pProduct = NULL; 55 } 56 } 57 58 void BuildPartA() 59 { 60 m_pProduct->Add("部件A"); 61 } 62 63 void BuildPartB() 64 { 65 m_pProduct->Add("部件B"); 66 } 67 68 CProduct* GetResult() 69 { 70 return m_pProduct; 71 } 72 }; 73 74 // 具体建造者类 75 class CConcreteBuilder2 : public CBuilder 76 { 77 private: 78 CProduct* m_pProduct; 79 80 public: 81 CConcreteBuilder2() 82 { 83 m_pProduct = new CProduct(); 84 } 85 86 ~CConcreteBuilder2() 87 { 88 if (m_pProduct) 89 { 90 delete m_pProduct; 91 m_pProduct = NULL; 92 } 93 } 94 void BuildPartA() 95 { 96 m_pProduct->Add("部件X"); 97 } 98 99 void BuildPartB() 100 { 101 m_pProduct->Add("部件Y"); 102 } 103 104 CProduct* GetResult() 105 { 106 return m_pProduct; 107 } 108 }; 109 110 // 指挥者类 111 class CDirector 112 { 113 public: 114 void Construct(CBuilder* pBuilder) 115 { 116 pBuilder->BuildPartA(); 117 pBuilder->BuildPartB(); 118 } 119 }; 120 121 int main() 122 { 123 CDirector Director; 124 CConcreteBuilder1 ConcreteBuilder1; 125 CConcreteBuilder2 ConcreteBuilder2; 126 127 Director.Construct(&ConcreteBuilder1); 128 ConcreteBuilder1.GetResult()->Show(); 129 130 Director.Construct(&ConcreteBuilder2); 131 ConcreteBuilder2.GetResult()->Show(); 132 133 system("pause"); 134 return 0; 135 }
输出结果:
例:画小人
代码如下:
1 #include <iostream> 2 #include <list> 3 #include <string> 4 using namespace std; 5 6 // 抽象建造者类 7 class CPersonBuilder 8 { 9 public: 10 virtual void BuildHead() {}; 11 virtual void BuildBody() {}; 12 virtual void BuildArmLeft() {}; 13 virtual void BuildArmRight() {}; 14 virtual void BuildLegLeft() {}; 15 virtual void BuildLegRight() {}; 16 }; 17 18 // 具体建造者类 19 class CPersonThinBuilder : public CPersonBuilder 20 { 21 public: 22 void BuildHead() 23 { 24 cout << "画瘦小人头" << endl; 25 } 26 27 void BuildBody() 28 { 29 cout << "画瘦小人身体" << endl; 30 } 31 32 void BuildArmLeft() 33 { 34 cout << "画瘦小人左臂" << endl; 35 } 36 37 void BuildArmRight() 38 { 39 cout << "画瘦小人右臂" << endl; 40 } 41 42 void BuildLegLeft() 43 { 44 cout << "画瘦小人左腿" << endl; 45 } 46 47 void BuildLegRight() 48 { 49 cout << "画瘦小人右腿" << endl; 50 } 51 }; 52 53 // 具体建造者类 54 class CPersonFatBuilder : public CPersonBuilder 55 { 56 public: 57 void BuildHead() 58 { 59 cout << "画胖小人头" << endl; 60 } 61 62 void BuildBody() 63 { 64 cout << "画胖小人身体" << endl; 65 } 66 67 void BuildArmLeft() 68 { 69 cout << "画胖小人左臂" << endl; 70 } 71 72 void BuildArmRight() 73 { 74 cout << "画胖小人右臂" << endl; 75 } 76 77 void BuildLegLeft() 78 { 79 cout << "画胖小人左腿" << endl; 80 } 81 82 void BuildLegRight() 83 { 84 cout << "画胖小人右腿" << endl; 85 } 86 }; 87 88 // 指挥者类 89 class CPersonDirector 90 { 91 private: 92 CPersonBuilder* m_pPerson; 93 94 public: 95 CPersonDirector(CPersonBuilder* pPerson) 96 { 97 m_pPerson = pPerson; 98 } 99 100 void CreatePerson() 101 { 102 m_pPerson->BuildHead(); 103 m_pPerson->BuildBody(); 104 m_pPerson->BuildArmLeft(); 105 m_pPerson->BuildArmRight(); 106 m_pPerson->BuildLegLeft(); 107 m_pPerson->BuildLegRight(); 108 } 109 }; 110 111 int main() 112 { 113 CPersonThinBuilder Thin; 114 CPersonDirector dThin(&Thin); 115 dThin.CreatePerson(); 116 117 cout << "\n华丽的分割线\n" << endl; 118 CPersonFatBuilder Fat; 119 CPersonDirector dFat(&Fat); 120 dFat.CreatePerson(); 121 122 system("pause"); 123 return 0; 124 }
输出结果: