设计模式的 C++ 实现---建造者模式

前文回顾
单例模式(一)
单例模式(二)
观察者模式
简单工厂模式
工厂方法模式(一)
工厂方法模式(二)
抽象工厂模式(一)
抽象工厂模式(二)
原型模式
外观模式
中介者模式
代理模式
装饰者模式
策略模式
状态模式
命令模式

前言

建造者模式的本质是一个对象的构造过程是相对不变的、客户端只是对象的消费者,不关心对象的创建过程,仅提出需求获取对象即可,每一种需求即为一张配方,根据配方进行对象的创建。

实现举例

  1. 场景描述
    以电脑组装过程为例:电脑的构成是不变的,但根据配置需求可以有不同的组装方式。
  2. 对象类
#include 
#include 
using namespace std;

class Computer
{
     
public:
    Computer(){
     }
    void setCpu(const string &m_cpu)
    {
     
        cpu = m_cpu;
    }
    void setDisk(const string &m_disk)
    {
     
        disk = m_disk;
    }
    void setMainBoard(const string &m_mainboard)
    {
     
        mainBoard = m_mainboard;
    }
    void setDisplay(const string &m_display)
    {
     
        display = m_display;
    }
    void setRam(const string &m_ram)
    {
     
        ram = m_ram;
    }
    void setSystem(const string &m_system)
    {
     
        system = m_system;
    }
    void showCopmtuter()
    {
     
        cout<<"主板:"<<mainBoard<<",CPU:"<<cpu<<",内存:"<<ram<<",硬盘:"<<disk<<",显示器:"<<display<<",操作系统:"<<system<<endl;
    }
private:
    string mainBoard;
    string cpu;
    string disk;
    string display;
    string ram;
    string system;
};

  1. 创建者基类
#include 
#include "computer.h"
using namespace std;

class ComputerBuilder
{
     
public:
    ComputerBuilder():m_computer(new  Computer()){
     }
    virtual ~ComputerBuilder(){
     }
    virtual void buildCpu() = 0;
    virtual void buildDisk() = 0;
    virtual void buildMainBoard() = 0;
    virtual void buildDisplay() = 0;
    virtual void buildRam() = 0;
    virtual void buildSystem() = 0;
    virtual Computer *getComputer() = 0;
protected:
    Computer *m_computer;
};
  1. 创建者派生类
#include "computerbuilder.h"
//配方1
class ComputerBuilderOne : public ComputerBuilder
{
     
public:
    using ComputerBuilder::ComputerBuilder;
    void buildMainBoard() override
    {
     
        cout <<"建造主板"<<endl;
        m_computer->setMainBoard("华硕");
    }
    void buildCpu() override
    {
     
         cout <<"组装CPU"<<endl;
        m_computer->setCpu("intel");
    }
    void buildRam() override
    {
     
         cout <<"组装内层"<<endl;
        m_computer->setRam("16G");
    }
    void buildDisk() override
    {
     
         cout <<"组装硬盘"<<endl;
        m_computer->setDisk("500G固态硬盘");
    }
    void buildDisplay() override
    {
     
         cout <<"组装显示器"<<endl;
        m_computer->setDisplay("三星");
    }
    void buildSystem() override
    {
     
         cout <<"组装操作系统"<<endl;
        m_computer->setSystem("win10");
    }
    Computer * getComputer() override
    {
     
         cout <<"组装完成"<<endl;
        return  m_computer;
    }
};
  1. 指挥者类
#include "computerbuilder.h"
#include 

class ComputerBuilderDirector
{
     
public:
    ComputerBuilderDirector(shared_ptr<ComputerBuilder> m_builder):builder(m_builder){
     }
    Computer * constructComputer()
    {
     
        builder->buildMainBoard();
        builder->buildCpu();
        builder->buildRam();
        builder->buildDisk();
        builder->buildDisplay();
        builder->buildSystem();
        return builder->getComputer();
    }
    void setBuilder(shared_ptr<ComputerBuilder> m_builder)
    {
     
        builder = m_builder;
    }
private:
    shared_ptr<ComputerBuilder> builder;
};
  1. 客户端调用
    设计模式的 C++ 实现---建造者模式_第1张图片
#include "computer.h"
#include "computerbuilderone.h"
#include "computerbuilderdirector.h"

int main(int argc, char *argv[])
{
     
    QCoreApplication a(argc, argv);

    shared_ptr<ComputerBuilder> builder(new ComputerBuilderOne());
    ComputerBuilderDirector * director = new  ComputerBuilderDirector(builder);
    Computer * myComputer = director->constructComputer();
    myComputer->showCopmtuter();

    delete myComputer;
    delete director;

    return a.exec();
}

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