来源:爱编程的大丙
复杂的问题化繁为简,逐个击破。也就是分步骤创建复杂的对象,并允许使用相同代码生成不同类型和形式的对象,这种模式叫做生成器模式(也叫建造者模式)。
生成器模式建议将造船工序的代码从产品类抽取出来并将其放在一个名为生成器的独立类。
海贼船:桑尼类,梅利类,通过字符串代表某个零件
船生成器:每艘船的零部件由对应的生成器类构造完成,抽象类中定义建造海贼船所有零部件的方法,在这个类的子类中需要重写这些虚函数,分别完成桑尼号 和梅利号零件的建造。
在这个生成器类中只要调用build方法,对应的零件就会加载到SunnyShip类的对象m_sunny中,当当船被造好之后就可以通过SunnyShip* getsunny()方法得到桑尼的实例对象
主管类:桑尼号和梅利号分别有三个规格,简约型、标准型、豪华型根据不同的规格,有选择的调用生成器中不同的建造函数,就可以得到最终的成品了。
cpp
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
class SunnyShip {
public:
//添加零件
void addParts(string name) {
m_parts.push_back(name);
}
void showParts() {
for (const auto& item : m_parts) {
cout << item << " ";
}
cout << endl;
}
private:
vector<string>m_parts;
};
class MerryShip {
public:
//组装
void assemble(string name, string parts) {
m_parts.insert(make_pair(name, parts));
}
void showParts() {
for (const auto& item : m_parts) {
cout << item.first << ": " << item.second << " ";
}
cout << endl;
}
private:
map<string, string>m_parts;
};
//生成器类 定义建造海贼船所有零件的方法,在这个类中需要重写这些虚函数
class ShipBuilder {
public:
virtual void reset() = 0;
virtual void buildBody() = 0;
virtual void buildWeapon() = 0;
virtual void buildEngine() = 0;
virtual void buildInterior() = 0;
virtual ~ShipBuilder() {}
};
//桑尼号生产器
class SunnyBuilder :public ShipBuilder {
public:
SunnyBuilder() {
reset();
}
~SunnyBuilder() {
if (m_sunny != nullptr) {
delete m_sunny;
m_sunny = nullptr;//删除后将指针置为nullptr
}
}
//提供重置函数,目的是能够使用生成器对象生产多个产品
void reset() override {
if (m_sunny != nullptr) {
delete m_sunny;
}
m_sunny = new SunnyShip;
}
void buildBody() override {
m_sunny->addParts("神树亚当的树干");
}
void buildWeapon() override {
m_sunny->addParts("狮子吼");
}
void buildEngine() override {
m_sunny->addParts("可乐驱动");
}
void buildInterior() override {
m_sunny->addParts("豪华内室精装");
}
SunnyShip* getSunny() {
SunnyShip* ship = m_sunny;
m_sunny = nullptr;
return ship;
}
private:
SunnyShip* m_sunny = nullptr;
};
//梅利号生产器
class MerryBuilder :public ShipBuilder {
public:
MerryBuilder() {
reset();
}
~MerryBuilder() {
if (m_merry != nullptr) {
delete m_merry;
m_merry = nullptr;//删除后将指针置为nullptr
}
}
//提供重置函数,目的是能够使用生成器对象生产多个产品
void reset() override {
m_merry = new MerryShip;
}
void buildBody() override {
m_merry->assemble("船体","优质木材");
}
void buildWeapon() override {
m_merry->assemble("武器","四门大炮");
}
void buildEngine() override {
m_merry->assemble("动力","蒸汽机");
}
void buildInterior() override {
m_merry->assemble("内室","精修");
}
MerryShip* getMerry() {
MerryShip* ship = m_merry;
m_merry = nullptr;
return ship;
}
private:
MerryShip* m_merry = nullptr;
};
//主管类
class Director {
public:
void setBuilder(ShipBuilder* builder) {
m_builder = builder;
}
//简约型
void buildSimpleShip() {
m_builder->buildBody();
m_builder->buildEngine();
}
//标准型
void buildStandardShip() {
buildSimpleShip();
m_builder->buildWeapon();
}
//豪华型
void buildRegalShip() {
buildStandardShip();
m_builder->buildInterior();
}
private:
ShipBuilder* m_builder = nullptr;
};
//建造桑尼号
void BuilderSunny() {
Director* director = new Director;
SunnyBuilder* builder = new SunnyBuilder;
//简约型
director->setBuilder(builder);
director->buildSimpleShip();
SunnyShip* sunny = builder->getSunny();
sunny->showParts();
delete sunny;
//标准型
builder->reset();
director->setBuilder(builder);
director->buildStandardShip();
sunny = builder->getSunny();
sunny->showParts();
delete sunny;
//豪华型
builder->reset();
director->setBuilder(builder);
director->buildRegalShip();
sunny = builder->getSunny();
sunny->showParts();
delete sunny;
delete builder;
delete director;
}
int main() {
BuilderSunny();
return 0;
}