首页 > 代码库 > 建造者模式
建造者模式
建造者模式定义
建造者模式(Builder),将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。如果我们使用了建造者模式,那么用户就只需指定需要建造的类型就可以得到他们,而具体建造的过程和细节就不许知道了。
建造者模式结构图
建造者模式结构图如下所示:
图 01 建造者模式结构图
建造者模式套用代码
1 #include <list> 2 #include <string> 3 #include "iostream" 4 using namespace std; 5 // 最终的产品 6 class Product 7 { 8 private: 9 list<string> parts; 10 public: 11 void Add(string part) 12 { 13 parts.push_back(part); 14 } 15 16 void Show() 17 { 18 cout << "产品创建 -- " << endl; 19 list<string>::iterator it; 20 for(it = parts.begin(); it != parts.end(); ++it) 21 { 22 cout << *it << " "; 23 } 24 } 25 }; 26 27 // 封闭建造产品的方法 28 class Builder 29 { 30 public: 31 virtual void BuildPartA() = 0; 32 virtual void BuildPartB() = 0; 33 virtual Product* GetResult() = 0; 34 }; 35 36 // 具体如何生产产品1 37 class ConcreteBuilder1 : public Builder 38 { 39 private: 40 Product* product; 41 public: 42 ConcreteBuilder1() 43 { 44 product = new Product(); 45 } 46 47 virtual void BuildPartA() 48 { 49 product->Add("部件A"); 50 } 51 52 virtual void BuildPartB() 53 { 54 product->Add("部件B"); 55 } 56 57 virtual Product* GetResult() 58 { 59 return product; 60 } 61 }; 62 63 // 具体如何生产产品2 64 class ConcreteBuilder2 : public Builder 65 { 66 private: 67 Product* product; 68 public: 69 ConcreteBuilder2() 70 { 71 product = new Product(); 72 } 73 74 virtual void BuildPartA() 75 { 76 product->Add("部件x"); 77 } 78 79 virtual void BuildPartB() 80 { 81 product->Add("部件y"); 82 } 83 84 virtual Product* GetResult() 85 { 86 return product; 87 } 88 }; 89 90 // 指挥者,与客户端交互用于判断要生产哪种产品 91 class Director 92 { 93 public: 94 void Construct(Builder* builder) 95 { 96 builder->BuildPartA(); 97 builder->BuildPartB(); 98 } 99 };100 101 void main()102 {103 Director* director = new Director();104 Builder* b1 = new ConcreteBuilder1();105 Builder* b2 = new ConcreteBuilder2();106 107 director->Construct(b1);108 Product* p1 = b1->GetResult();109 p1->Show();110 111 director->Construct(b2);112 Product* p2 = b2->GetResult();113 p2->Show();114 115 if(director != NULL)116 {117 delete director;118 director = NULL;119 }120 121 if(b1 != NULL)122 {123 delete b1;124 b1 = NULL;125 }126 127 if(b2 != NULL)128 {129 delete b2;130 b2 = NULL;131 }132 133 if(p1 != NULL)134 {135 delete p1;136 p1 = NULL;137 }138 139 if(p2 != NULL)140 {141 delete p2;142 p2 = NULL;143 }144 }145 146 #include <list>147 #include <string>148 #include "iostream"149 using namespace std;150 // 最终的产品151 class Product152 {153 private:154 list<string> parts;155 public:156 void Add(string part)157 {158 parts.push_back(part);159 }160 161 void Show()162 {163 cout << "产品创建 -- " << endl;164 list<string>::iterator it;165 for(it = parts.begin(); it != parts.end(); ++it) 166 {167 cout << *it << " ";168 }169 }170 };171 172 // 封闭建造产品的方法173 class Builder174 {175 public:176 virtual void BuildPartA() = 0;177 virtual void BuildPartB() = 0;178 virtual Product* GetResult() = 0;179 };180 181 // 具体如何生产产品1182 class ConcreteBuilder1 : public Builder183 {184 private:185 Product* product;186 public:187 ConcreteBuilder1()188 {189 product = new Product();190 }191 192 virtual void BuildPartA()193 {194 product->Add("部件A");195 }196 197 virtual void BuildPartB()198 {199 product->Add("部件B");200 }201 202 virtual Product* GetResult()203 {204 return product;205 }206 };207 208 // 具体如何生产产品2209 class ConcreteBuilder2 : public Builder210 {211 private:212 Product* product;213 public:214 ConcreteBuilder2()215 {216 product = new Product();217 }218 219 virtual void BuildPartA()220 {221 product->Add("部件x");222 }223 224 virtual void BuildPartB()225 {226 product->Add("部件y");227 }228 229 virtual Product* GetResult()230 {231 return product;232 }233 };234 235 // 指挥者,与客户端交互用于判断要生产哪种产品236 class Director237 {238 public:239 void Construct(Builder* builder)240 {241 builder->BuildPartA();242 builder->BuildPartB();243 }244 };245 246 void main()247 {248 Director* director = new Director();249 Builder* b1 = new ConcreteBuilder1();250 Builder* b2 = new ConcreteBuilder2();251 252 director->Construct(b1);253 Product* p1 = b1->GetResult();254 p1->Show();255 256 director->Construct(b2);257 Product* p2 = b2->GetResult();258 p2->Show();259 260 if(director != NULL)261 {262 delete director;263 director = NULL;264 }265 266 if(b1 != NULL)267 {268 delete b1;269 b1 = NULL;270 }271 272 if(b2 != NULL)273 {274 delete b2;275 b2 = NULL;276 }277 278 if(p1 != NULL)279 {280 delete p1;281 p1 = NULL;282 }283 284 if(p2 != NULL)285 {286 delete p2;287 p2 = NULL;288 }289 }
建造者模式特点
① 建造者模式是组建建造产品的,所以建造者的Builder类里的建造方法必须要足够普遍,才便为各种类型的具体建造者构造。
② 建造者模式主要用于创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但对象内部的构建通常面临着复杂的变化
③ 建造者模式额好处就是使得建造代码与表示代码分离,由于建造者隐藏了该产品如何组装的,所以若需要改变一个产品的内部表示,只需要再定义一个具体的建造者就可以了。
建造者模式实例应用
建造者模式实例应用类图
图 02 建造者模式实例应用类图
建造者模式实例应用代码
1 #include "iostream" 2 using namespace std; 3 #include <string> 4 #include <list> 5 6 class CPerson 7 { 8 private: 9 list<string> person; 10 public: 11 void Add(string part) 12 { 13 person.push_back(part); 14 } 15 16 void Show() 17 { 18 list<string>::iterator it; 19 for(it = person.begin(); it != person.end(); it++) 20 { 21 cout << " " << *it << " " << endl; 22 } 23 } 24 }; 25 26 class CPersonBuilder 27 { 28 public: 29 virtual void BuildHead() = 0; 30 virtual void BuildBody() = 0; 31 virtual void BuildArmLeft() = 0; 32 virtual void BuildArmRight() = 0; 33 virtual void BuildLegLeft() = 0; 34 virtual void BuildLegRight() = 0; 35 virtual CPerson* GetPerson() = 0; 36 }; 37 38 39 class CPersonThinBuilder : public CPersonBuilder 40 { 41 private: 42 CPerson* person; 43 public: 44 CPersonThinBuilder() 45 { 46 person = new CPerson(); 47 } 48 49 virtual void BuildHead() 50 { 51 person->Add("Сͷ"); 52 } 53 54 virtual void BuildBody() 55 { 56 person->Add("СÉíÌå"); 57 } 58 59 virtual void BuildArmLeft() 60 { 61 person->Add("С×óÊÖ"); 62 } 63 64 virtual void BuildArmRight() 65 { 66 person->Add("СÓÒÊÖ"); 67 } 68 69 virtual void BuildLegLeft() 70 { 71 person->Add("С×ó½Å"); 72 } 73 74 virtual void BuildLegRight() 75 { 76 person->Add("СÓÒ½Å"); 77 } 78 79 virtual CPerson* GetPerson() 80 { 81 return person; 82 } 83 }; 84 85 class CPersonFatBuilder : public CPersonBuilder 86 { 87 private: 88 CPerson* person; 89 public: 90 CPersonFatBuilder() 91 { 92 person = new CPerson(); 93 } 94 95 virtual void BuildHead() 96 { 97 person->Add("´óÍ·"); 98 } 99 100 virtual void BuildBody()101 {102 person->Add("´óÉíÌå");103 }104 105 virtual void BuildArmLeft()106 {107 person->Add("´ó×óÊÖ");108 }109 110 virtual void BuildArmRight()111 {112 person->Add("´óÓÒÊÖ");113 }114 115 virtual void BuildLegLeft()116 {117 person->Add("´ó×ó½Å");118 }119 120 virtual void BuildLegRight()121 {122 person->Add("´óÓÒ½Å");123 }124 125 virtual CPerson* GetPerson()126 {127 return person;128 }129 };130 131 class CPersonDirector132 {133 public:134 CPersonBuilder* pb;135 public:136 CPersonDirector(CPersonBuilder* pb)137 {138 this->pb = pb;139 }140 void CreatePerson()141 {142 pb->BuildHead();143 pb->BuildBody();144 pb->BuildArmRight();145 pb->BuildLegLeft();146 pb->BuildLegRight();147 pb->BuildLegRight();148 }149 };150 151 void main()152 {153 CPersonThinBuilder* ptb1 = new CPersonThinBuilder();154 CPersonDirector* pdThin = new CPersonDirector(ptb1);155 pdThin->CreatePerson();156 CPerson* person1 = pdThin->pb->GetPerson();157 person1->Show();158 if(ptb1 != NULL)159 {160 delete ptb1;161 ptb1 = NULL;162 }163 164 if(pdThin != NULL)165 {166 delete pdThin;167 pdThin = NULL;168 }169 170 if(person1 != NULL)171 {172 delete person1;173 person1 = NULL;174 }175 cout << endl;176 177 CPersonFatBuilder* ptb2 = new CPersonFatBuilder();178 CPersonDirector* pdFat = new CPersonDirector(ptb2);179 pdFat->CreatePerson();180 CPerson* person2 = pdFat->pb->GetPerson();181 person2->Show();182 183 if(ptb2 != NULL)184 {185 delete ptb2;186 ptb2 = NULL;187 }188 189 if(pdFat != NULL)190 {191 delete pdFat;192 pdFat = NULL;193 }194 195 if(person2 != NULL)196 {197 delete person2;198 person2 = NULL;199 }200 }
2014-11-29 17:10:42
建造者模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。