建造者模式(C++)
定义:建造者模式(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;
}