您的位置:首页 > 科技 > IT业 > 全国互联网平台_百度网站管理_培训推广 seo_南京百度快照优化排名

全国互联网平台_百度网站管理_培训推广 seo_南京百度快照优化排名

2024/12/26 11:58:08 来源:https://blog.csdn.net/menger3388/article/details/142892114  浏览:    关键词:全国互联网平台_百度网站管理_培训推广 seo_南京百度快照优化排名
全国互联网平台_百度网站管理_培训推广 seo_南京百度快照优化排名

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

        代码:

// 产品类:计算机
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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com