builder pattern--建造者模式
buildre pattern称建造者模式
Builder设计模式,提供一种封装机制来隔离出构成复杂对象的各个子对象的变化,从而保持系统中的相对稳定的将这些子对象组合在一起的算法不随着需求的改变而改变。
Separate the construction of a complex object from its representation so that the same construction process can create different representations. - GoF
建造者(Builder):
给出一个抽象接口,以规范产品对象的各个组成成分的建造。一般而言,此接口独立于应用程序的商业逻辑。模式中直接创建产品对象的是具体建造者(ConcreteBuilder)角色。具体建造者类必须实现这个接口所要求的方法:一个是建造方法,另一个是结果返还方法。
具体建造者(Concrete Builder):
担任这个角色的是于应用程序紧密相关的类,它们在应用程序调用下创建产品实例。这个角色主要完成的任务包括:
- 实现Builder角色提供的接口,一步一步完成创建产品实例的过程。
- 在建造过程完成后,提供产品的实例。
指导者(Director):
担任这个角色的类调用具体建造者角色以创建产品对象。导演者并没有产品类的具体知识,真正拥有产品类的具体知识的是具体建造者对象。
产品(Product):
产品便是需要建造的复杂对象。
实例:
例如所有的汽车的构建其实是有轮子(Michelin--米其林,Bridgestone--普利司通)、发动机(China,America)、外壳(Normal,Special)三大部分组成。
1 //------------------------三个组成该汽车的基类----------------------------- 2 //abstract part 3 //其实属性可以有很多,这里就不一一列出来。 4 class wheel 5 { 6 public: 7 wheel(std::string n,int l) 8 :name(n),loadCapacity(l) 9 {} 10 virtual std::string getName( )=0; 11 virtual int getLG( )=0; 12 ... 13 protected: 14 std::string name; 15 int loadCapacity; //负载能力 16 ... 17 }; 18 19 class engine 20 { 21 public: 22 engine(std::string n,int s) 23 :name(n),speed(s) 24 {} 25 virtual std::string getName( )=0; 26 virtual int getSpeed( )=0; 27 ... 28 protected: 29 std::string name; 30 int speed ; //转速 31 ... 32 }; 33 34 class shell 35 { 36 public: 37 shell(std::string n,std::string c) 38 :name(n),color(c) 39 {} 40 virtual std::string getName( )=0; 41 virtual std::string getColor( )=0; 42 ... 43 protected: 44 std::string name; 45 std::string color //颜色 46 ... 47 }; 48 49 //------------------------继承上面基类wheel 的轮胎类----------------------------- 50 //concerte part 51 //米其林轮胎 52 class wheelMichelin:public wheel 53 { 54 public: 55 wheelMichelin(std::string n,int l) 56 :wheel(n,l) 57 {} 58 std::string getName( ); 59 int getLG( ); 60 }; 61 std::string wheelMichelin::getName( ) 62 { 63 return name; 64 } 65 int wheelMichelin::getLG( ) 66 { 67 return loadCapacity; 68 } 69 70 //普利司通轮胎 71 class wheelBridgestone:public wheel 72 { 73 public: 74 wheelBridgestone(std::string n,int l) 75 :wheel(n,l) 76 {} 77 std::string getName( ); 78 int getLG( ); 79 }; 80 std::string wheelBridgestone::getName( ) 81 { 82 return name; 83 } 84 int wheelBridgestone::getLG( ) 85 { 86 return loadCapacity; 87 } 88 //--------------------------继承上面基类engine的引擎类----------------------------------------- 89 //中国制造 90 class engineChina:public engine 91 { 92 public: 93 engineChina(std::string n,int s) 94 :engine(n,s) 95 {} 96 std::string getName( ); 97 int getSpeed( ); 98 }; 99 std::string engineChina::getName( ) 100 { 101 return name; 102 } 103 int engineChina::getSpeed( ) 104 { 105 return speed; 106 } 107 108 //美国制造 109 class engineAmerica:public engine 110 { 111 public: 112 engineAmerica(std::string n,int s) 113 :engine(n,s) 114 {} 115 std::string getName( ); 116 int getSpeed( ); 117 }; 118 std::string engineAmerica::getName( ) 119 { 120 return name; 121 } 122 int engineAmerica::getSpeed( ) 123 { 124 return speed; 125 } 126 //----------------------------继承基类shell的外壳类-------------------------------------- 127 //普通 128 class shellNormal:public shell 129 { 130 public: 131 shellNormal(std::string n,std::string c) 132 :shell(n,c) 133 {} 134 std::string getName( ); 135 int getColor( ); 136 }; 137 std::string shellNormal::getName( ) 138 { 139 return name; 140 } 141 std::string shellNormal::getColor( ) 142 { 143 return color; 144 } 145 146 class shellSpecial:public shell 147 { 148 public: 149 shellSpecial(std::string n,std::string c) 150 :name(n),color(c):shell(n,c) 151 {} 152 std::string getName( ); 153 int getColor( ); 154 }; 155 std::string shellSpecial::getName( ) 156 { 157 return name; 158 } 159 std::string shellSpecial::getColor( ) 160 { 161 return color; 162 } 163 //---------------------------产品的基类即车------------------------------- 164 //abstract product 165 //车由wheel,engine,shell组成 166 class car 167 { 168 public: 169 virtual void setwh(std::auto_ptr<wheel> w)=0; 170 virtual void seten(std::auto_ptr<engine> e)=0; 171 virutal void setsh(std::auto_ptr<shell> s)=0; 172 virtual std::auto_ptr<wheel> getwh( )=0; 173 virtual std::auto_ptr<engine> geten( )=0; 174 virtual std::auto_ptr<shell> getsh( )=0; 175 protected: 176 std::auto_ptr<wheel> pw; //轮子 177 std::auto_ptr<engine> pe; //发动机 178 std::auto_ptr<shell> ps; //外壳 179 }; 180 //------------------------------继承上面基类的车类--------------------------- 181 //concerte product 182 class AudiCar:public car 183 { 184 public: 185 void setwh(std::auto_ptr<wheel> w); 186 void seten(std::auto_ptr<engine> e); 187 void setsh(std::auto_ptr<shell> s); 188 std::auto_ptr<wheel> getwh( ); 189 std::auto_ptr<engine> geten( ); 190 std::auto_ptr<shell> getsh( ); 191 }; 192 void AudiCar::setwh(std::auto_ptr<wheel> w) 193 { 194 pw=w; 195 } 196 void AudiCar::seten(std::auto_ptr<engine> e) 197 { 198 pe=e; 199 } 200 void AudiCar::setps(std::auto_ptr<shell> s) 201 { 202 ps=s; 203 } 204 wheel* AudiCar::getwh( ) 205 { 206 if(!pw.get( )) 207 { 208 throw std::invalid_argument("Error: pointer is null!!"); 209 } 210 return wh.get( ); 211 } 212 engine* AudiCar::geten( ) 213 { 214 if(!pe.get( )) 215 { 216 throw std::invalid_argument("Error: pointer is null!!"); 217 } 218 return pe.get( ); 219 } 220 shell* AudiCar::getsh( ) 221 { 222 if(!ps.get( )) 223 { 224 throw std::invalid_argument("Error:pointer is null!!"); 225 } 226 return ps.get( ); 227 } 228 229 //奔驰 230 class BenzCar:public car 231 { 232 public: 233 void setwh(std::auto_ptr<wheel> w); 234 void seten(std::auto_ptr<engine> e); 235 void setsh(std::auto_ptr<shell> s); 236 std::auto_ptr<wheel> getwh( ); 237 std::auto_ptr<engine> geten( ); 238 std::auto_ptr<shell> getsh( ); 239 }; 240 BenzCar::setwh(std::auto_ptr<wheel> w) 241 { 242 pw=w; 243 } 244 BenzCar::seten(std::auto_ptr<engine> e) 245 { 246 pe=e; 247 } 248 BenzCar::setsh(std::auto_ptr<shell> s) 249 { 250 ps=s; 251 } 252 wheel* BenzCar::getwh( ) 253 { 254 if(!pw.get( )) 255 { 256 throw std::invalid_argument("Error: pointer is null!!"); 257 } 258 return pw.get( ); 259 } 260 engine* BenzCar::geten( ) 261 { 262 if(!pe.get( )) 263 { 264 throw std::invalid_argument("Error: pointer is null!!"); 265 } 266 return pe.get( ); 267 } 268 shell* BenzCar::getsh( ) 269 { 270 if(!ps.get( )) 271 { 272 throw std::invalid_argument("Error:pointer is null!!"); 273 } 274 return ps.get( ); 275 } 276 ------------------------------------------------------------------ 277 class builder 278 { 279 public: 280 virtual void buildWheel( )=0; 281 virtual void buildEngine( )=0; 282 virtual void buildShell( )=0; 283 virtual car* getCar( )=0; 284 }; 285 286 class BenzBuilder:public builder 287 { 288 public: 289 BenzBuilder() 290 :c(new BenzCar()) 291 {} 292 void buildWheel(); 293 void buildEngine(); 294 void buildShell(); 295 car* getCar( ); 296 private: 297 std::auto_ptr<car> c; 298 }; 299 300 //生产奔驰的轮胎 301 void BenzBuilder::buildWheel( ) 302 { 303 //采用米其林轮胎 304 std::auto_ptr<wheel> w(new wheelMichelin("Michelin",270 )); 305 c->setwh(); 306 } 307 308 //生产奔驰的引擎 309 void BenzBuilder::buildEngine( ) 310 { 311 //美国产引擎 312 std::auto_ptr<engine> w(new engineAmercia("Amercia",300)); 313 c->seten(); 314 } 315 316 //生产奔驰的外壳 317 void BenzBuilder::buildShell( ) 318 { 319 std::auto_ptr<sheel> w(new shellNormol("Normal","Red")); 320 c->setsh(); 321 } 322 323 std::auto_ptr<car> BenzBuilder::getCar( ) 324 { 325 return c.get( ); 326 } 327 328 class AudiBuilder:public builder 329 { 330 public: 331 AudiBuilder( ) 332 :c(new AudiCar( )) 333 {} 334 void buildWheel( ); 335 void buildEngine( ); 336 void buildShell( ); 337 std::auto_ptr<car> getCar( ); 338 private: 339 std::auto_ptr<car> c; 340 }; 341 342 void AudiBuilder::buildWheel( ) 343 { 344 //采用高通 345 std::auto_ptr<wheel> w(new wheelBridgestone("Bridgestone",250)); 346 c->setwh( ); 347 } 348 349 void AudiBuilder::buildEngine( ) 350 { 351 //中国产引擎 352 std::auto_ptr<engine> w(new EngineChina("China",320)); 353 c->seten( ); 354 } 355 356 void AudiBuilder::buildShell( ) 357 { 358 std::auto_ptr<shell> w(new ShellSpecial("Special","white")); 359 c-setsh(w); 360 } 361 362 car* AudiBuilder::getCar( ) 363 { 364 return c.get( ); 365 } 366 //------------------------------组装汽车-------------------------------------------- 367 class director 368 { 369 public: 370 static std::auto_ptr<car> construct(std::auto_ptr<builder> b); 371 } 372 std::auto_ptr<car> director::construct( std::auto_ptr<builder> b) 373 { 374 b->buildWheel( ); 375 b->buildEngine( ); 376 b->buildShell( ); 377 return b->getCar( ); 378 } 379 380 int main(int argc,char **argv) 381 { 382 std::auto_ptr<builder> b(new BenzBuilder( ); 383 std::auto_ptr<car> c=director::construct(b); 384 std::cout<<"The BenzCar's wheel is "<<c->getwh( )->getName( )<<std::endl; 385 std::cout<<"Its loadCapacity is "<<c->getwh( )->getLG( )<<std::endl; 386 std::cout<<"The BenzCar's engine is made in "<<c->geten()->getName( )<<std::endl; 387 std::cout<<"Its speed is "<<c->geten()->getSpeed()<<std::endl; 388 std::cout<<"The BenzCar's shell is "<<c->getsh()->getName( )<<std::endl; 389 std::cout<<"Its color is "<<c->getsh()->getColor()<<std::endl; 390 std::auto_ptr<builder> b2(new AudiBuilder( ); 391 std::auto_ptr<car> c2=director::construct(b2); 392 std::cout<<"The AudiCar's wheel is "<<c2->getwh( )->getName( )<<std::endl; 393 std::cout<<"Its loadCapacity is "<<c2->getwh( )->getLG( )<<std::endl; 394 std::cout<<"The AudiCar's engine is made in "<<c2->geten()->getName( )<<std::endl; 395 std::cout<<"Its speed is "<<c2->geten()->getSpeed()<<std::endl; 396 std::cout<<"The AudiCar's shell is "<<c2->getsh()->getName( )<<std::endl; 397 std::cout<<"Its color is "<<c2->getsh()->getColor()<<std::endl; 398 return 0; 399 }
结果