1//------------------------三个组成该汽车的基类----------------------------- 2//abstract part 3//其实属性可以有很多,这里就不一一列出来。 4class wheel 5{ 6public: 7 wheel(std::string n,int l) 8 :name(n),loadCapacity(l) 9 {} 10virtual std::string getName( )=0; 11virtualint getLG( )=0; 12 ... 13protected: 14 std::string name; 15int loadCapacity; //负载能力 16 ... 17}; 18 19class engine 20{ 21public: 22 engine(std::string n,int s) 23 :name(n),speed(s) 24 {} 25virtual std::string getName( )=0; 26virtualint getSpeed( )=0; 27 ... 28protected: 29 std::string name; 30int speed ; //转速 31 ... 32}; 33 34class shell 35{ 36public: 37 shell(std::string n,std::string c) 38 :name(n),color(c) 39 {} 40virtual std::string getName( )=0; 41virtual std::string getColor( )=0; 42 ... 43protected: 44 std::string name; 45 std::string color //颜色 46 ... 47}; 48 49//------------------------继承上面基类wheel 的轮胎类----------------------------- 50//concerte part 51//米其林轮胎 52class wheelMichelin:public wheel 53{ 54public: 55 wheelMichelin(std::string n,int l) 56 :wheel(n,l) 57 {} 58 std::string getName( ); 59int getLG( ); 60}; 61 std::string wheelMichelin::getName( ) 62{ 63return name; 64} 65int wheelMichelin::getLG( ) 66{ 67return loadCapacity; 68} 69 70//普利司通轮胎 71class wheelBridgestone:public wheel 72{ 73public: 74 wheelBridgestone(std::string n,int l) 75 :wheel(n,l) 76 {} 77 std::string getName( ); 78int getLG( ); 79}; 80 std::string wheelBridgestone::getName( ) 81{ 82return name; 83} 84int wheelBridgestone::getLG( ) 85{ 86return loadCapacity; 87} 88//--------------------------继承上面基类engine的引擎类----------------------------------------- 89//中国制造 90class engineChina:public engine 91{ 92public: 93 engineChina(std::string n,int s) 94 :engine(n,s) 95 {} 96 std::string getName( ); 97int getSpeed( ); 98}; 99 std::string engineChina::getName( )100{101return name;102}103int engineChina::getSpeed( )104{105return speed;106}107108//美国制造109class engineAmerica:public engine110{111public:112 engineAmerica(std::string n,int s)113 :engine(n,s)114 {}115 std::string getName( );116int getSpeed( );117};118 std::string engineAmerica::getName( )119{120return name;121}122int engineAmerica::getSpeed( )123{124return speed;125}126//----------------------------继承基类shell的外壳类--------------------------------------127//普通128class shellNormal:public shell129{130public:131 shellNormal(std::string n,std::string c)132 :shell(n,c)133 {}134 std::string getName( );135int getColor( );136};137 std::string shellNormal::getName( )138{139return name;140}141 std::string shellNormal::getColor( )142{143return color;144}145146class shellSpecial:public shell147{148public:149 shellSpecial(std::string n,std::string c)150 :name(n),color(c):shell(n,c)151 {}152 std::string getName( );153int getColor( );154};155 std::string shellSpecial::getName( )156{157return name;158}159 std::string shellSpecial::getColor( )160{161return color;162}163//---------------------------产品的基类即车-------------------------------164//abstract product165//车由wheel,engine,shell组成166class car167{168public:169virtualvoid setwh(std::auto_ptr<wheel> w)=0;170virtualvoid seten(std::auto_ptr<engine> e)=0;171 virutal void setsh(std::auto_ptr<shell> s)=0;172virtual std::auto_ptr<wheel> getwh( )=0;173virtual std::auto_ptr<engine> geten( )=0;174virtual std::auto_ptr<shell> getsh( )=0;175protected:176 std::auto_ptr<wheel> pw; //轮子177 std::auto_ptr<engine> pe; //发动机178 std::auto_ptr<shell> ps; //外壳 179};180//------------------------------继承上面基类的车类---------------------------181//concerte product182class AudiCar:public car183{184public: 185void setwh(std::auto_ptr<wheel> w);186void seten(std::auto_ptr<engine> e);187void 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};192void AudiCar::setwh(std::auto_ptr<wheel> w)193{194 pw=w;195}196void AudiCar::seten(std::auto_ptr<engine> e)197{198 pe=e;199}200void AudiCar::setps(std::auto_ptr<shell> s)201{202 ps=s;203}204 wheel* AudiCar::getwh( )205{206if(!pw.get( ))207 {208throw std::invalid_argument("Error: pointer is null!!");209 }210return wh.get( );211}212 engine* AudiCar::geten( )213{214if(!pe.get( ))215 {216throw std::invalid_argument("Error: pointer is null!!");217 }218return pe.get( );219}220 shell* AudiCar::getsh( )221{222if(!ps.get( ))223 {224throw std::invalid_argument("Error:pointer is null!!");225 }226return ps.get( );227}228229//奔驰230class BenzCar:public car231{232public:233void setwh(std::auto_ptr<wheel> w);234void seten(std::auto_ptr<engine> e);235void 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{254if(!pw.get( ))255 {256throw std::invalid_argument("Error: pointer is null!!");257 }258return pw.get( );259}260 engine* BenzCar::geten( )261{262if(!pe.get( ))263 {264throw std::invalid_argument("Error: pointer is null!!");265 }266return pe.get( );267}268 shell* BenzCar::getsh( )269{270if(!ps.get( ))271 {272throw std::invalid_argument("Error:pointer is null!!");273 }274return ps.get( );275}276 ------------------------------------------------------------------277class builder278{279public:280virtualvoid buildWheel( )=0;281virtualvoid buildEngine( )=0;282virtualvoid buildShell( )=0;283virtual car* getCar( )=0;284};285286class BenzBuilder:public builder287{288public:289 BenzBuilder()290 :c(new BenzCar())291 {}292void buildWheel();293void buildEngine();294void buildShell();295 car* getCar( );296private:297 std::auto_ptr<car> c;298};299300//生产奔驰的轮胎301void BenzBuilder::buildWheel( )302{303//采用米其林轮胎304 std::auto_ptr<wheel> w(new wheelMichelin("Michelin",270 ));305 c->setwh(); 306}307308//生产奔驰的引擎309void BenzBuilder::buildEngine( )310{311//美国产引擎312 std::auto_ptr<engine> w(new engineAmercia("Amercia",300));313 c->seten();314}315316//生产奔驰的外壳317void BenzBuilder::buildShell( )318{319 std::auto_ptr<sheel> w(new shellNormol("Normal","Red"));320 c->setsh();321}322323 std::auto_ptr<car> BenzBuilder::getCar( )324{325return c.get( );326}327328class AudiBuilder:public builder329{330public:331 AudiBuilder( )332 :c(new AudiCar( ))333 {}334void buildWheel( );335void buildEngine( );336void buildShell( );337 std::auto_ptr<car> getCar( );338private:339 std::auto_ptr<car> c;340};341342void AudiBuilder::buildWheel( )343{344//采用高通345 std::auto_ptr<wheel> w(new wheelBridgestone("Bridgestone",250));346 c->setwh( );347}348349void AudiBuilder::buildEngine( )350{351//中国产引擎352 std::auto_ptr<engine> w(new EngineChina("China",320));353 c->seten( );354}355356void AudiBuilder::buildShell( )357{358 std::auto_ptr<shell> w(new ShellSpecial("Special","white"));359 c-setsh(w);360}361362 car* AudiBuilder::getCar( )363{364return c.get( );365}366//------------------------------组装汽车--------------------------------------------367class director368{369public:370static 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( );377return b->getCar( );378}379380int 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;398return0;399 }