Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 397, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

设计模式之Builder模式

Builder模式和Factory模式的区别是:Builder模式是分步创建对象。在此模型中有个director类,通过它来把切片stream传递给builder,通过切片来创建对象。此中模式把部件组合成完整的产品和部件的实现分开,从而实现通过共同的接口的不同实现创建产品。
#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
#include ".../purge.h"
using std::size_t;

class BicyclePart{
public:
   enum BPart{FRAME,WHEEL,SEAT,DERAILLEUR,HANDLERBAR,SPROCKET,RACK,SHOCK,NPARTS};
private:
   BPart id;
   static std::string names[NPARTS];
public:
   BicyclePart(BPart bp) {id=bp;}
   friend std::ostream& operator<<(std::ostream& os,const BicyclePart& pb){
      return os<<bp.names[bp.id];}
};

class Bicycle{
   std::vector<BicyclePart*> parts;
public:
   ~Bicycle() {pruge(parts);}
   void addParts(BicyclePart* bp){parts.push_back(bp);}
   friend std::ostream& operator<<(std::ostream& os,const Bicycle& b){
      os<<"{";
      for(size_t i= 0; i < b.parts.size();++i)
         os<<*b.parts[i]<<' ';
      return os <<"}";}
};

class BicycleBuilder{
protected:
   Bicycle* product;
public:
   BicycleBuilder(){product = 0;}
   void CreateProcuct(){product = new Bicycle;}
   virtual void buildFrame()=0;
   virtual void buildWheel()=0;
   virtual void buildSeat()=0;
   virtual void buildDerailleur()=0;
   virtual void buildHandlebar()=0;
   virtual void buildSprocket()=0;
   virtual void buildRack()=0;
   virtual void buildShocK()=0;
   virtual std::string getBikeName() const =0;
   Bicycle* getProduct(){
      Bicycle* temp= product;
      product = 0; //Relinquish product
      return temp;}
};

class  MountainBikeBuilder:public BicycleBuilder{
public:
   void buildFrame();
   void buildWheel();
   void buildSeat();
   void buildDerailleur();
   void buildHandlebar();
   void buildSprocket();
   void buildRack();
   void buildShocK();
   std::string getBikeName() const {return "MountainBike"}
};

class  TouringBikeBuilder:public BicycleBuilder{
public:
   void buildFrame();
   void buildWheel();
   void buildSeat();
   void buildDerailleur();
   void buildHandlebar();
   void buildSprocket();
   void buildRack();
   void buildShocK();
   std::string getBikeName() const {return "TouringBike"}
};

class RacingBickBuilder:public BicycleBuilder{
public:
   void buildFrame();
   void buildWheel();
   void buildSeat();
   void buildDerailleur();
   void buildHandlebar();
   void buildSprocket();
   void buildRack();
   void buildShock();
   std::string getBickName()const {return "RacingBike";}
};

class BicycleTechnician{
   BicycleBuilder* builder;
public:
   BicycleTechnician(){builder=0;}
   void setBuilder(BicycleBuiler* b) {builder=b;}
   void construct();
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::string BicyclePart::names[NPARTS] ={"Frame","Wheel","Seat","Derailleur","Handlebar","Sprocket","Rack","Shock"};

//MountainBikeBuilder implementation
void MountainBikeBuilder::buildFrame(){product->addpart(new BicyclePart(Bicycle::FRAME));}
void MountainBikeBuilder::buildWheel(){product->addpart(new BicyclePart(Bicycle::WHEEL));}
void MountainBikeBuilder::buildSeat(){product->addpart(new BicyclePart(Bicycle::SEAT));}
void MountainBikeBuilder::buildDerailleur(){product->addpart(new BicyclePart(Bicycle::DERAILLEUR));}
void MountainBikeBuilder::buildHandlebar(){product->addpart(new BicyclePart(Bicycle::HANDLEBAR));}
void MountainBikeBuilder::buildSprocket(){product->addpart(new BicyclePart(Bicycle::SPROCKET));}
void MountainBikeBuilder::buildRack(){}
void MountainBikeBuilder::buildShock(){product->addpart(new BicyclePart(Bicycle::SHOCK));}
   
//TouringBikeBuilder implementation
void TouringBikeBllider::buildFrame(){product->addpart(new BicyclePart(Bicycle::FRAME));}
void TouringBikeBuilder::buildWheel(){product->addpart(new BicyclePart(Bicycle::WHEEL));}
void TouringBikeBuilder::buildSeat(){product->addpart(new BicyclePart(Bicycle::SEAT));}
void TouringBikeBuilder::buildDerailleur(){product->addpart(new BicyclePart(Bicycle::DERAILLEUR}
void TouringBikeBuilder::buildHandlebar(){product->addpart(new BicyclePart(Bicycle::HANDLEBAR));}
void TouringBikeBuilder::buildSprocket(){product->addpart(new BicyclePart(Bicycle::SPROCKET));}
void TouringBikeBuilder::buildRack(){product->addpart(new BicyclePart(Bicycle::RACK));}
void TouringBikeBuilder::buildShock(){}

//RacingBikeBulider implementation
void RacingBikeBuilder::buildFrame(){product->addpart(new BicyclePart(Bicycle::FRAME));}
void RacingBikeBuilder::buildWheel(){product->addpart(new BicyclePart(Bicycle::WHEEL));}
void RacingBikeBuilder::buildSeat(){product->addpart(new BicyclePart(Bicycle::SEAT));}
void RacingBikeBuilder::buildDerailleur(){}
void RacingBikeBuilder::buildHandlebar(){product->addpart(new BicyclePart(Bicycle::HANDLEBAR));}
void RacingBikeBuilder::buildSprocket(){product->addpart(new BicyclePart(Bicycle::SPROCKET));}
void RacingBikeBuilder::buildRack(){}
void RacingBikeBuilder::buildShock(){}

//BicycleTechnician implementation
void BicycleTechnician::construct(){
   assert(builder);
   builder->createProduct();
   builder->buildFrame();
   builder->buildWheel();
   builder->buildSeat();
   builder->buildDerailleur();
   builder->buildHandlbar();
   builder->buildSprocket();
   builder->buildRack();
   builder->buildShock();
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstddef>
#include <iostream>
#include <map>
#include <vector>
#inculde "../purge.h"
using namespace std;

//constructs a bike via a concrete builder
Bicycle* buildMeBike(BicycleTechnician& t,BicycleBuilder* builder){
   t.setBuilder(builder);
   t.construct();
   Bicycle* b = builder->getProduct();
   cout<<"Build a"<<builder->getBikeName()<<endl;
   return b;
}

int main(){
   //Create an order for some bicycle
   map<string,size_t> order;
   order["mountain"] = 2;
   order["toruing"] = 1;
   order["racing"] = 3;

   //build bikes
   vector<Bicycle*> bikes;
   BicycleBuilder* m = new MountainBikeBuilder;
   BicycleBuilder* t = new TouringBikeBuilder;
   BicycleBuilder* r = new RacingBikeBuilder;
   BicycleTechnician tech;
   map<string,size_t> ::iterator it = order.begin();
   while(it != order.end()){
      BicycleBuilder* builder;
      if(it->first == "Mountain")
         builder = m;
      else if(it->first == "touring")
         builder = t;
      else if(it->first == "racing")
         builder = r;
      for(size_t i=0; i < it->second; ++i)
         bikes.push_back(builderMeABike(tech,builder));
      ++ it;
   }
   delete m;
   delete t;
   delete r;
   
   //Display inventory
   for(size_t i = 0; i < bikes.size(); ++i)
      cout<<"Bicycle" <<*bikes[i] <<endl;
   pruge(bikes);
}

posted on 2009-04-23 22:34 Benjamin 阅读(397) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理