C++设计模式——建造者模式(高屋建瓴)

原网站:https://blog.csdn.net/u012581604/article/details/72758110
修改后的代码如下:

#include 
using namespace std;
#define DELETE_OBJECT(Obj) \
        {\
        if (Obj) \
            {\
            delete (Obj); \
            (Obj)=NULL;\
            }\
        }
class IdiotDressing {
 public:
  void SetUnderWare(std::string strUnderWare){
    m_strUnderWare = strUnderWare;
  }
  void SetBriefs(std::string strBriefs) {
    m_strBriefs = strBriefs;
  }
  void SetPants(std::string strPants) {
    m_strPants = strPants;
  }
  void SetCoat(std::string strCoat){
    m_strCoat = strCoat;
  }
  std::string GetUnderWare() {
    return m_strUnderWare;
  }
  std::string GetBriefs() {
    return m_strBriefs;
  }
  std::string GetPants() {
    return m_strPants;
  }
  std::string GetCoat() {
    return m_strCoat;
  }
 private:
  std::string m_strUnderWare;
  std::string m_strBriefs;
  std::string m_strPants;
  std::string m_strCoat;
};
class IBuilder {
 public:
  virtual void BuildUnderware() = 0;
  virtual void BuildBriefs() = 0;
  virtual void BuildPants() = 0;
  virtual void BuildCoat() = 0;
  virtual IdiotDressing* GetIdiotDressing() = 0;
};

class BulidBeggerDressing : public IBuilder {
 public:
  BulidBeggerDressing () {m_idiotDressing = new IdiotDressing();}
  virtual void BuildUnderware(){ m_idiotDressing->SetUnderWare("ZangDeQiuYi"); }
  virtual void BuildBriefs() { m_idiotDressing->SetBriefs("PoDeQiuKu"); }
  virtual void BuildPants() { m_idiotDressing->SetPants("BuHeShenDeKuZi"); }
  virtual void BuildCoat() { m_idiotDressing->SetCoat("ZangDeWaiTao"); }
  virtual IdiotDressing* GetIdiotDressing(){ return m_idiotDressing; }
 private:
  IdiotDressing* m_idiotDressing;
};
class BuildBossDressing : public IBuilder {
 public:
  BuildBossDressing () {m_idiotDressing = new IdiotDressing();}
  virtual void BuildUnderware() { m_idiotDressing->SetUnderWare("GanJinDeQiuYi"); }
  virtual void BuildBriefs() { m_idiotDressing->SetBriefs("HeShenDeQiuKu"); }
  virtual void BuildPants() { m_idiotDressing->SetPants("MingPaiDeQiuKu"); }
  virtual void BuildCoat() { m_idiotDressing->SetCoat("MingPaiDeXiZhuang"); }
  virtual IdiotDressing* GetIdiotDressing(){ return m_idiotDressing; }
 private:
  IdiotDressing* m_idiotDressing;
};
class Director {
 public:
  //实现建造流程
  void Dressing(IBuilder* builder){
    builder->BuildUnderware();
    builder->BuildBriefs();
    builder->BuildPants();
     builder->BuildCoat();
  }
};
int main(){
  //创建两个建造者
  IBuilder* pBeggerBuilder = new BulidBeggerDressing();
  IBuilder* pBossBuilder = new BuildBossDressing();
  //创建一个指挥者,并且创建相应的产品表现。
  Director* pDirector = new Director();
  pDirector->Dressing(pBeggerBuilder);
  pDirector->Dressing(pBossBuilder);
  
  IdiotDressing* pBeggerDress = pBeggerBuilder->GetIdiotDressing();
  IdiotDressing* pBossDress = pBossBuilder->GetIdiotDressing();

  cout << "Begger Dressing" << endl;
  cout << "Underware:" << pBeggerDress->GetUnderWare().c_str() << endl;
  cout << "Briefs:" << pBeggerDress->GetBriefs().c_str() << endl;
  cout << "Pants:" << pBeggerDress->GetPants().c_str() << endl;
  cout << "Coat:" << pBeggerDress->GetCoat().c_str() << endl<<endl<<endl;

  cout << "Boss Dressing" << endl;
  cout << "Underware:" << pBossDress->GetUnderWare().c_str() << endl;
  cout << "Briefs:" << pBossDress->GetBriefs().c_str() << endl;
  cout << "Pants:" << pBossDress->GetPants().c_str() << endl;
  cout << "Coat:" << pBossDress->GetCoat().c_str() << endl;

  DELETE_OBJECT(pBeggerBuilder);
  DELETE_OBJECT(pBossBuilder);
  DELETE_OBJECT(pDirector);
  DELETE_OBJECT(pBeggerDress);
  DELETE_OBJECT(pBossDress);
  
  return 0;
}

输出结果:
Begger Dressing
Underware:ZangDeQiuYi
Briefs:PoDeQiuKu
Pants:BuHeShenDeKuZi
Coat:ZangDeWaiTao

Boss Dressing
Underware:GanJinDeQiuYi
Briefs:HeShenDeQiuKu
Pants:MingPaiDeQiuKu
Coat:MingPaiDeXiZhuang

你可能感兴趣的:(C++设计模式,c++,设计模式)