目录
生成器模式(Builder Pattern)
生成器模式是一种创建型设计模式,它允许你分步骤创建复杂对象。与其他创建型模式不同的是,生成器模式允许你在一个过程中创建一个对象,而无需等待所有部分都准备好。这种模式特别适用于需要构造不同表示或状态的复杂对象时。
实际应用
1. 需要构造的对象包含多个部分,且需要分步骤创建。
2. 希望使用相同的创建过程来构造不同表示的对象。
3. 构造过程需要能够被隔离和独立变化。
构建一辆汽车
假设我们有一个`Car`类,它有很多可选属性,例如车的颜色、发动机类型、轮胎类型等等。
#include <iostream>
#include <string>
class Car {
public:
std::string color;
std::string engine;
std::string tires;
std::string interior;
void show() {
std::cout << "Car Details:\n"
<< "Color: " << color << "\n"
<< "Engine: " << engine << "\n"
<< "Tires: " << tires << "\n"
<< "Interior: " << interior << "\n";
}
};
class CarBuilder {
private:
Car car;
public:
CarBuilder& setColor(const std::string& color) {
car.color = color;
return *this;
}
CarBuilder& setEngine(const std::string& engine) {
car.engine = engine;
return *this;
}
CarBuilder& setTires(const std::string& tires) {
car.tires = tires;
return *this;
}
CarBuilder& setInterior(const std::string& interior) {
car.interior = interior;
return *this;
}
Car build() {
return car;
}
};
int main() {
CarBuilder builder;
Car car = builder.setColor("Red")
.setEngine("V8")
.setTires("Sport")
.setInterior("Leather")
.build();
car.show();
return 0;
}
构建一台计算机
假设我们有一个`Computer`类,它有很多可选属性,例如CPU类型、RAM大小、存储类型和大小等等。
#include <iostream>
#include <string>
class Computer {
public:
std::string cpu;
int ram;
std::string storageType;
int storageSize;
void show() {
std::cout << "Computer Details:\n"
<< "CPU: " << cpu << "\n"
<< "RAM: " << ram << " GB\n"
<< "Storage Type: " << storageType << "\n"
<< "Storage Size: " << storageSize << " GB\n";
}
};
class ComputerBuilder {
private:
Computer computer;
public:
ComputerBuilder& setCPU(const std::string& cpu) {
computer.cpu = cpu;
return *this;
}
ComputerBuilder& setRAM(int ram) {
computer.ram = ram;
return *this;
}
ComputerBuilder& setStorageType(const std::string& storageType) {
computer.storageType = storageType;
return *this;
}
ComputerBuilder& setStorageSize(int storageSize) {
computer.storageSize = storageSize;
return *this;
}
Computer build() {
return computer;
}
};
int main() {
ComputerBuilder builder;
Computer computer = builder.setCPU("Intel i9")
.setRAM(32)
.setStorageType("SSD")
.setStorageSize(1024)
.build();
computer.show();
return 0;
}
构建一个房子
假设我们有一个`House`类,它有很多可选属性,例如房间数量、楼层数量、花园是否存在、是否有车库等等。
#include <iostream>
#include <string>
class House {
public:
int rooms;
int floors;
bool garden;
bool garage;
void show() {
std::cout << "House Details:\n"
<< "Rooms: " << rooms << "\n"
<< "Floors: " << floors << "\n"
<< "Garden: " << (garden ? "Yes" : "No") << "\n"
<< "Garage: " << (garage ? "Yes" : "No") << "\n";
}
};
class HouseBuilder {
private:
House house;
public:
HouseBuilder& setRooms(int rooms) {
house.rooms = rooms;
return *this;
}
HouseBuilder& setFloors(int floors) {
house.floors = floors;
return *this;
}
HouseBuilder& setGarden(bool garden) {
house.garden = garden;
return *this;
}
HouseBuilder& setGarage(bool garage) {
house.garage = garage;
return *this;
}
House build() {
return house;
}
};
int main() {
HouseBuilder builder;
House house = builder.setRooms(5)
.setFloors(2)
.setGarden(true)
.setGarage(true)
.build();
house.show();
return 0;
}
总结
每个生成器类负责设置对象的各个属性,并在最终构建出对象。