1. 程式人生 > >設計模式--建立型模式--建造者模式

設計模式--建立型模式--建造者模式

//Creational Patterns--builder //建立型模式--建造者模式

//1.Builder    抽象建造者 //2.ConcreteBuilder    具體建造者 //3.Director    指揮者 //4.Product 產品

//產品:電腦 class Computer { public:     void SetCpu(string cpu){m_strCpu = cpu;}     void SetMainBoard(string mainboard){m_strMainBoard = mainboard;}     void SetRam(string ram){m_strRam = ram;}     void SetVideoCard(string videocard){m_strVideoCard = videocard;}

    string GetCpu(){return m_strCpu;}     string GetMainBoard(){return m_strMainBoard;}     string GetRam(){return m_strRam;}     string GetVideoCard(){return m_strVideoCard;}

private:     string m_strCpu;     string m_strMainBoard;     string m_strRam;     string m_strVideoCard;

}; //抽象建造者 class IBuilder { public:     virtual void BuildCpu() = 0;     virtual void BuildMainBoard() = 0;     virtual void BuildRam() = 0;     virtual void BuildVideoCard() = 0;     virtual Computer* GetResult() = 0; };

//具體建造者:thinkpad class ThinkPadBuilder : public IBuilder { public:     ThinkPadBuilder(){m_pComputer = new Computer();}     ~ThinkPadBuilder(){if(m_pComputer) delete m_pComputer;}     void BuildCpu(){m_pComputer->SetCpu("i5-6200u");}     void BuildMainBoard(){m_pComputer->SetMainBoard("Intel DH5700");}     void BuildRam(){m_pComputer->SetRam("DDR4");}     void BuildVideoCard(){m_pComputer->SetVideoCard("NVIDIA Geforce 920MX");}     Computer* GetResult(){return m_pComputer;}

private:     Computer *m_pComputer; };

//具體建造者:yoga class YogaBuilder : public IBuilder { public:     YogaBuilder(){m_pComputer = new Computer();}     ~YogaBuilder(){if(m_pComputer) delete m_pComputer;}     void BuildCpu(){m_pComputer->SetCpu("i7-7500u");}     void BuildMainBoard(){m_pComputer->SetMainBoard("Intel DP55KG");}     void BuildRam(){m_pComputer->SetRam("DDR5");}     void BuildVideoCard(){m_pComputer->SetVideoCard("NVIDIA Geforce 950MX");}     Computer* GetResult(){return m_pComputer;}

private:     Computer *m_pComputer; };

//指揮者 class Director { public:     void Create(IBuilder *pbuilder)     {         pbuilder->BuildCpu();         pbuilder->BuildMainBoard();         pbuilder->BuildRam();         pbuilder->BuildVideoCard();     } };

//--------------------------------------------------------------- //測試 void dpBuilderMain() {     Director* pDirector = new Director();     ThinkPadBuilder *pThinkPadBuilder = new ThinkPadBuilder();     YogaBuilder* pYogaBuilder = new YogaBuilder();

    //組裝兩種電腦     pDirector->Create(pThinkPadBuilder);     pDirector->Create(pYogaBuilder);

    //獲取組裝後的電腦     Computer *pThinkPadComputer = pThinkPadBuilder->GetResult();     Computer *pYogaComputer = pYogaBuilder->GetResult();

    //分別測試     cout << "-----ThinkPad-----" << endl;     cout << "CPU: " << pThinkPadComputer->GetCpu() << endl;     cout << "MainBoard: " << pThinkPadComputer->GetMainBoard() << endl;     cout << "RAM: " << pThinkPadComputer->GetRam() << endl;     cout << "VideoCard: " << pThinkPadComputer->GetVideoCard() << endl;

    cout << "-----Yoga-----" << endl;     cout << "CPU: " << pYogaComputer->GetCpu() << endl;     cout << "MainBoard: " << pYogaComputer->GetMainBoard() << endl;     cout << "RAM: " << pYogaComputer->GetRam() << endl;     cout << "VideoCard: " << pYogaComputer->GetVideoCard() << endl;

    if(pDirector) delete pDirector;     if(pThinkPadBuilder) delete pThinkPadBuilder;     if(pYogaBuilder) delete pYogaBuilder;

    return; }