建造者模式(C++)

**定义:**建造者模式(Builder Pattern)是一种创建型设计模式,它主要用于构建一个复杂对象,并将其构建过程与表示分离,使得同样的构建过程可以创建不同的表示。该模式通过将复杂对象的构建过程分解为多个简单的步骤,每个步骤由独立的建造者对象负责,从而实现了构建过程的可控性和灵活性。

代码:

cpp 复制代码
// 产品类:计算机
class Computer {
private:
    std::string CPU;
    std::string RAM;
    std::string STORAGE;
    std::string GPU;
public:
    void setCPU(const std::string& cpu){CPU = cpu;}
    void setRAM(const std::string& ram){RAM = ram;}
    void setStorage(const std::string& storage){STORAGE = storage;}
    void setGPU(const std::string& gpu){GPU = gpu;}
    // 显示计算机配置
    void show() const {
        std::cout << "Computer [CPU=" << CPU << ", RAM=" << RAM << ", storage=" << STORAGE << ", GPU=" << GPU << "]" << std::endl;
    }
private:
     // 私有构造函数,避免直接创建对象
    Computer(){}

    // 友元类,允许Builder访问私有构造函数
    friend class ComputerBuilder;

};

// 抽象建造者类
class ComputerBuilder {
public:
    virtual ~ComputerBuilder() = default;
    virtual void setCPU(const std::string& cpu) = 0;
    virtual void setRAM(const std::string& ram) = 0;
    virtual void setStorage(const std::string& storage) = 0;
    virtual void setGPU(const std::string& gpu) = 0;
    virtual Computer* getResult() = 0;
protected:
    Computer* createComputer()
    {
        return  new Computer();
    }
protected:
    Computer* computer = nullptr;
};

// 具体建造者类
class ConcreteComputerBuilder : public ComputerBuilder {

public:
    ConcreteComputerBuilder() {
        computer = nullptr;
    }

    void setCPU(const std::string& cpu) override {
        if(computer == nullptr)
        {
            computer = createComputer();
        }
        computer->setCPU(cpu);

    }
    void setRAM(const std::string& ram) override {
        if(computer == nullptr)
        {
            computer = createComputer();
        }
        computer->setRAM(ram);
    }

    void setStorage(const std::string& storage) override {
        if(computer == nullptr)
        {
            computer = createComputer();
        }
        computer->setStorage(storage);
    }

    void setGPU(const std::string& gpu) override {
        if(computer == nullptr)
        {
            computer = createComputer();
        }
        computer->setGPU(gpu);
    }

    Computer* getResult() override {
        Computer* temp = computer;
        computer = nullptr; // 重置指针,避免内存泄漏(这里只是为了简化示例)
        return temp;
    }
};

// 指挥者类
class ComputerDirector {
private:
    ComputerBuilder* builder;

public:
    ComputerDirector(ComputerBuilder* builder) : builder(builder) {}
    ~ComputerDirector()
    {
        delete builder;
    }
    void setBuilder(ComputerBuilder* builder)
    {

        if(this->builder)
        {
            delete this->builder;
        }
        this->builder = builder;
    }

    void constructGamingComputer() {
        if(!builder)return;
        builder->setCPU("Intel Core i9");
        builder->setRAM("32GB");
        builder->setStorage("1TB SSD");
        builder->setGPU("NVIDIA GeForce RTX 3080");
    }

    void constructOfficeComputer() {
        if(!builder)return;
        builder->setCPU("Intel Core i5");
        builder->setRAM("16GB");
        builder->setStorage("512GB SSD");
        builder->setGPU("Integrated Graphics");
    }
};

// 客户端代码
int main() {
    ComputerBuilder* builder = new ConcreteComputerBuilder();
    ComputerDirector director(builder);

    // 构建游戏电脑
    director.constructGamingComputer();
    Computer* gamingComputer = builder->getResult();
    gamingComputer->show();
    delete gamingComputer;

    // 构建办公电脑
    builder = new ConcreteComputerBuilder();
    director.setBuilder(builder);
    director.constructOfficeComputer();
    Computer* officeComputer = builder->getResult();
    officeComputer->show();
    delete officeComputer;
    return 0;
}
相关推荐
王老师青少年编程3 小时前
gesp(C++五级)(14)洛谷:B4071:[GESP202412 五级] 武器强化
开发语言·c++·算法·gesp·csp·信奥赛
DogDaoDao3 小时前
leetcode 面试经典 150 题:有效的括号
c++·算法·leetcode·面试··stack·有效的括号
一只小bit4 小时前
C++之初识模版
开发语言·c++
CodeClimb5 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
apz_end6 小时前
埃氏算法C++实现: 快速输出质数( 素数 )
开发语言·c++·算法·埃氏算法
仟濹6 小时前
【贪心算法】洛谷P1106 - 删数问题
c语言·c++·算法·贪心算法
北顾南栀倾寒7 小时前
[Qt]系统相关-网络编程-TCP、UDP、HTTP协议
开发语言·网络·c++·qt·tcp/ip·http·udp
old_power8 小时前
【PCL】Segmentation 模块—— 基于图割算法的点云分割(Min-Cut Based Segmentation)
c++·算法·计算机视觉·3d
涛ing8 小时前
21. C语言 `typedef`:类型重命名
linux·c语言·开发语言·c++·vscode·算法·visual studio
PaLu-LI10 小时前
ORB-SLAM2源码学习:Initializer.cc⑧: Initializer::CheckRT检验三角化结果
c++·人工智能·opencv·学习·ubuntu·计算机视觉