《C++新经典设计模式》之第3章 工厂模式、原型模式、建造者模式
2023-12-13 05:53:08
《C++新经典设计模式》之第3章 工厂模式、原型模式、建造者模式
简单工厂模式.cpp
#include <iostream>
#include <memory>
using namespace std;
namespace ns1
{
class Monster // 怪物父类
{
protected: // 怪物属性
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
};
class M_Undead : public Monster // 亡灵类怪物
{
public:
M_Undead(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A ghost monster came to this world" << endl; }
};
class M_Element : public Monster // 元素类怪物
{
public:
M_Element(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "An elemental monster came to this world" << endl; }
};
class M_Mechanic : public Monster // 机械类怪物
{
public:
M_Mechanic(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster came to this world" << endl; }
};
shared_ptr<Monster> createMonster(const string &strmontype)
{
if (strmontype == "udd") // udd代表要创建亡灵类怪物
return make_shared<M_Undead>(300, 50, 80);
else if (strmontype == "elm") // ele代表要创建元素类怪物
return make_shared<M_Element>(200, 80, 100);
else if (strmontype == "mec") // mec代表要创建机械类怪物
return make_shared<M_Mechanic>(400, 0, 110);
else
return nullptr;
}
class MonsterFactory // 怪物工厂类
{
public:
static shared_ptr<Monster> createMonster(const string &strmontype)
{
if (strmontype == "udd") // udd代表要创建亡灵类怪物
return make_shared<M_Undead>(300, 50, 80);
else if (strmontype == "elm") // ele代表要创建元素类怪物
return make_shared<M_Element>(200, 80, 100);
else if (strmontype == "mec") // mec代表要创建机械类怪物
return make_shared<M_Mechanic>(400, 0, 110);
else
return nullptr;
}
};
}
namespace ns2
{
class Shape
{
public:
virtual ~Shape() = default;
virtual void draw() const = 0;
};
class Rectangle : public Shape
{
public:
void draw() const override { cout << "Inside Rectangle::draw() method." << endl; }
};
class Square : public Shape
{
public:
void draw() const override { cout << "Inside Square::draw() method." << endl; }
};
class Circle : public Shape
{
public:
void draw() const override { cout << "Inside Circle::draw() method." << endl; }
};
class ShapeFactory
{
public:
static shared_ptr<Shape> getShape(const string &shapeType)
{
if (shapeType == "CIRCLE")
return make_shared<Circle>();
else if (shapeType == "RECTANGLE")
return make_shared<Rectangle>();
else if (shapeType == "SQUARE")
return make_shared<Square>();
else
return nullptr;
}
};
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<Monster> pM1(new M_Undead(300, 50, 80)); // 产生了一只亡灵类怪物
shared_ptr<Monster> pM2(new M_Element(200, 80, 100)); // 产生了一只元素类怪物
shared_ptr<Monster> pM3(new M_Mechanic(400, 0, 110)); // 产生了一只机械类怪物
#endif
#if 0
using namespace ns1;
shared_ptr<Monster> pM1 = createMonster("udd"); // 产生了一只亡灵类怪物,当然这里必须知道"udd"代表的是创建亡灵类怪物
shared_ptr<Monster> pM2 = createMonster("elm"); // 产生了一只元素类怪物
shared_ptr<Monster> pM3 = createMonster("mec"); // 产生了一只机械类怪物
#endif
#if 0
using namespace ns1;
shared_ptr<Monster> pM1 = MonsterFactory::createMonster("udd"); // 产生了一只亡灵类怪物,当然这里必须知道"udd"代表的是创建亡灵类怪物
shared_ptr<Monster> pM2 = MonsterFactory::createMonster("elm"); // 产生了一只元素类怪物
shared_ptr<Monster> pM3 = MonsterFactory::createMonster("mec"); // 产生了一只机械类怪物
#endif
#if 1
using namespace ns2;
shared_ptr<Shape> shape1 = ShapeFactory::getShape("CIRCLE");
shape1->draw();
shared_ptr<Shape> shape2 = ShapeFactory::getShape("RECTANGLE");
shape2->draw();
shared_ptr<Shape> shape3 = ShapeFactory::getShape("SQUARE");
shape3->draw();
#endif
cout << "Over!\n";
return 0;
}
抽象工厂模式.cpp
#include <iostream>
#include <memory>
using namespace std;
// 一个工厂,多个产品
// 产品类别稳定(元素、亡灵、机械类怪物固定),场景变换(沼泽、山脉、城镇等增加)
namespace ns1
{
class Monster // 怪物父类
{
protected:
// 怪物属性
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
};
class M_Undead_Swamp : public Monster // 沼泽亡灵类怪物
{
public:
M_Undead_Swamp(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A dead monster from the swamp came to this world" << endl; }
};
class M_Element_Swamp : public Monster // 沼泽元素类怪物
{
public:
M_Element_Swamp(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A swamp elemental monster came to this world" << endl; }
};
class M_Mechanic_Swamp : public Monster // 沼泽机械类怪物
{
public:
M_Mechanic_Swamp(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster of swamp came to this world" << endl; }
};
class M_Undead_Mountain : public Monster // 山脉亡灵类怪物
{
public:
M_Undead_Mountain(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mountain necromancer came to this world" << endl; }
};
class M_Element_Mountain : public Monster // 山脉元素类怪物
{
public:
M_Element_Mountain(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mountain elemental monster came to this world" << endl; }
};
class M_Mechanic_Mountain : public Monster // 山脉机械类怪物
{
public:
M_Mechanic_Mountain(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster from the mountains came to this world" << endl; }
};
class M_Undead_Town : public Monster // 城镇亡灵类怪物
{
public:
M_Undead_Town(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A town's undead monster came to this world" << endl; }
};
class M_Element_Town : public Monster // 城镇元素类怪物
{
public:
M_Element_Town(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A town's elemental monster came to this world" << endl; }
};
class M_Mechanic_Town : public Monster // 城镇机械类怪物
{
public:
M_Mechanic_Town(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A town's mechanical monster came to this world" << endl; }
};
class M_ParFactory // 所有工厂类的父类
{
public:
virtual ~M_ParFactory() {}
virtual shared_ptr<Monster> createMonster_Undead() const = 0; // 创建亡灵类怪物
virtual shared_ptr<Monster> createMonster_Element() const = 0; // 创建元素类怪物
virtual shared_ptr<Monster> createMonster_Mechanic() const = 0; // 创建机械类怪物
};
class M_Factory_Swamp : public M_ParFactory // 沼泽地区的工厂
{
public:
shared_ptr<Monster> createMonster_Undead() const override
{
return make_shared<M_Undead_Swamp>(300, 50, 120); // 创建沼泽亡灵类怪物
}
shared_ptr<Monster> createMonster_Element() const override
{
return make_shared<M_Element_Swamp>(200, 80, 110); // 创建沼泽元素类怪物
}
shared_ptr<Monster> createMonster_Mechanic() const override
{
return make_shared<M_Mechanic_Swamp>(400, 0, 90); // 创建沼泽机械类怪物
}
};
class M_Factory_Mountain : public M_ParFactory // 山脉地区的工厂
{
public:
shared_ptr<Monster> createMonster_Undead() const override
{
return make_shared<M_Undead_Mountain>(300, 50, 80); // 创建山脉亡灵类怪物
}
shared_ptr<Monster> createMonster_Element() const override
{
return make_shared<M_Element_Mountain>(200, 80, 100); // 创建山脉元素类怪物
}
shared_ptr<Monster> createMonster_Mechanic() const override
{
return make_shared<M_Mechanic_Mountain>(600, 0, 110); // 创建山脉机械类怪物
}
};
class M_Factory_Town : public M_ParFactory // 城镇的工厂
{
public:
shared_ptr<Monster> createMonster_Undead() const override
{
return make_shared<M_Undead_Town>(300, 50, 80); // 创建城镇亡灵类怪物
}
shared_ptr<Monster> createMonster_Element() const override
{
return make_shared<M_Element_Town>(200, 80, 100); // 创建城镇元素类怪物
}
shared_ptr<Monster> createMonster_Mechanic() const override
{
return make_shared<M_Mechanic_Town>(400, 0, 110); // 创建城镇机械类怪物
}
};
}
// 类模板实现
namespace ns2
{
class Monster // 怪物父类
{
protected: // 怪物属性
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
};
class M_Undead_Swamp : public Monster // 沼泽亡灵类怪物
{
public:
M_Undead_Swamp(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A dead monster from the swamp came to this world" << endl; }
};
class M_Element_Swamp : public Monster // 沼泽元素类怪物
{
public:
M_Element_Swamp(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A swamp elemental monster came to this world" << endl; }
};
class M_Mechanic_Swamp : public Monster // 沼泽机械类怪物
{
public:
M_Mechanic_Swamp(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster of swamp came to this world" << endl; }
};
class M_Undead_Mountain : public Monster // 山脉亡灵类怪物
{
public:
M_Undead_Mountain(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mountain necromancer came to this world" << endl; }
};
class M_Element_Mountain : public Monster // 山脉元素类怪物
{
public:
M_Element_Mountain(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mountain elemental monster came to this world" << endl; }
};
class M_Mechanic_Mountain : public Monster // 山脉机械类怪物
{
public:
M_Mechanic_Mountain(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster from the mountains came to this world" << endl; }
};
class M_Undead_Town : public Monster // 城镇亡灵类怪物
{
public:
M_Undead_Town(int life = 30, int magic = 50, int attack = 80) : Monster(life, magic, attack) { cout << "A town's undead monster came to this world" << endl; }
};
class M_Element_Town : public Monster // 城镇元素类怪物
{
public:
M_Element_Town(int life = 100, int magic = 0, int attack = 44) : Monster(life, magic, attack) { cout << "A town's elemental monster came to this world" << endl; }
};
class M_Mechanic_Town : public Monster // 城镇机械类怪物
{
public:
M_Mechanic_Town(int life = 400, int magic = 0, int attack = 110) : Monster(life, magic, attack) { cout << "A town's mechanical monster came to this world" << endl; }
};
// 创建怪物工厂类模板
template <typename T>
class M_Factory
{
public:
static shared_ptr<Monster> createMonster() { return make_shared<T>(); }
static shared_ptr<Monster> createMonster(int life, int magic, int attack) { return make_shared<T>(life, magic, attack); }
};
template <typename T>
shared_ptr<T> getMonster()
{
return make_shared<T>();
}
template <typename T>
shared_ptr<T> getMonster(int life, int magic, int attack)
{
return make_shared<T>(life, magic, attack);
}
}
namespace ns3
{
class Body // 身体抽象类
{
public:
virtual void getName() const = 0;
virtual ~Body() {}
};
class Clothes // 衣服抽象类
{
public:
virtual void getName() const = 0;
virtual ~Clothes() {}
};
class Shoes // 鞋子抽象类
{
public:
virtual void getName() const = 0;
virtual ~Shoes() {}
};
class AbstractFactory // 抽象工厂类
{
public: // 所创建的部件应该稳定的保持这三个部件,才适合抽象工厂模式
virtual shared_ptr<Body> createBody() const = 0; // 创建身体
virtual shared_ptr<Clothes> createClothes() const = 0; // 创建衣服
virtual shared_ptr<Shoes> createShoes() const = 0; // 创建鞋子
virtual ~AbstractFactory() {}
};
class BarbieDoll // 芭比娃娃类
{
shared_ptr<Body> body;
shared_ptr<Clothes> clothes;
shared_ptr<Shoes> shoes;
public:
BarbieDoll(const shared_ptr<Body> &tmpbody, const shared_ptr<Clothes> &tmpclothes, const shared_ptr<Shoes> &tmpshoes) : body(tmpbody), clothes(tmpclothes), shoes(tmpshoes) {}
void Assemble() const // 组装芭比娃娃
{
cout << "Successfully assembled a Barbie doll: " << endl;
body->getName();
clothes->getName();
shoes->getName();
}
};
// 中国厂商实现的三个部件
class China_Body : public Body
{
public:
void getName() const override { cout << "Made by Chinese manufacturers_Body parts" << endl; }
};
class China_Clothes : public Clothes
{
public:
void getName() const override { cout << "Made by Chinese manufacturers_Clothes parts" << endl; }
};
class China_Shoes : public Shoes
{
public:
void getName() const override { cout << "Made by Chinese manufacturers_Shoes parts" << endl; }
};
class ChinaFactory : public AbstractFactory // 创建一个中国工厂
{
public:
shared_ptr<Body> createBody() const override { return make_shared<China_Body>(); }
shared_ptr<Clothes> createClothes() const override { return make_shared<China_Clothes>(); }
shared_ptr<Shoes> createShoes() const override { return make_shared<China_Shoes>(); }
};
// 日本厂商实现的三个部件
class Japan_Body : public Body
{
public:
void getName() const override { cout << "Made by Japanese manufacturers_Body parts" << endl; }
};
class Japan_Clothes : public Clothes
{
public:
void getName() const override { cout << "Made by Japanese manufacturers_Clothes parts" << endl; }
};
class Japan_Shoes : public Shoes
{
public:
void getName() const override { cout << "Made by Japanese manufacturers_Shoes parts" << endl; }
};
class JapanFactory : public AbstractFactory // 创建一个日本工厂
{
public:
shared_ptr<Body> createBody() const override { return make_shared<Japan_Body>(); }
shared_ptr<Clothes> createClothes() const override { return make_shared<Japan_Clothes>(); }
shared_ptr<Shoes> createShoes() const override { return make_shared<Japan_Shoes>(); }
};
// 美国厂商实现的三个部件
class America_Body : public Body
{
public:
void getName() const override { cout << "Made by American manufacturers_Body parts" << endl; }
};
class America_Clothes : public Clothes
{
public:
void getName() const override { cout << "Made by American manufacturers_Clothes parts" << endl; }
};
class America_Shoes : public Shoes
{
public:
void getName() const override { cout << "Made by American manufacturers_Shoes parts" << endl; }
};
class AmericaFactory : public AbstractFactory // 创建一个美国工厂
{
public:
shared_ptr<Body> createBody() const override { return make_shared<America_Body>(); }
shared_ptr<Clothes> createClothes() const override { return make_shared<America_Clothes>(); }
shared_ptr<Shoes> createShoes() const override { return make_shared<America_Shoes>(); }
};
}
namespace ns4
{
class Shape
{
public:
virtual ~Shape() = default;
virtual void draw() const = 0;
};
class Rectangle : public Shape
{
public:
void draw() const override { cout << "Inside Rectangle::draw() method." << endl; }
};
class Square : public Shape
{
public:
void draw() const override { cout << "Inside Square::draw() method." << endl; }
};
class Circle : public Shape
{
public:
void draw() const override { cout << "Inside Circle::draw() method." << endl; }
};
class Color
{
public:
virtual ~Color() = default;
virtual void fill() const = 0;
};
class Red : public Color
{
public:
void fill() const override { cout << "Inside Red::fill() method." << endl; }
};
class Green : public Color
{
public:
void fill() const override { cout << "Inside Green::fill() method." << endl; }
};
class Blue : public Color
{
public:
void fill() const override { cout << "Inside Blue::fill() method." << endl; }
};
class AbstractFactory
{
public:
virtual ~AbstractFactory() = default;
virtual shared_ptr<Shape> getShape(const string &shape) const = 0;
virtual shared_ptr<Color> getColor(const string &color) const = 0;
};
class ShapeFactory : public AbstractFactory
{
public:
shared_ptr<Shape> getShape(const string &shapeType) const override
{
if (shapeType == "CIRCLE")
return make_shared<Circle>();
else if (shapeType == "RECTANGLE")
return make_shared<Rectangle>();
else if (shapeType == "SQUARE")
return make_shared<Square>();
else
return nullptr;
}
shared_ptr<Color> getColor(const string &color) const override { return nullptr; }
};
class ColorFactory : public AbstractFactory
{
public:
shared_ptr<Shape> getShape(const string &shapeType) const override { return nullptr; }
shared_ptr<Color> getColor(const string &color) const override
{
if (color == "RED")
return make_shared<Red>();
else if (color == "GREEN")
return make_shared<Green>();
else if (color == "BLUE")
return make_shared<Blue>();
else
return nullptr;
}
};
shared_ptr<AbstractFactory> getFactory(const string &choice)
{
if (choice == "SHAPE")
return make_shared<ShapeFactory>();
else if (choice == "COLOR")
return make_shared<ColorFactory>();
else
return nullptr;
}
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<M_ParFactory> p_mou_fy(new M_Factory_Mountain()); // 多态工厂,山脉地区的工厂
shared_ptr<Monster> pM1 = p_mou_fy->createMonster_Element(); // 创建山脉地区的元素类怪物
shared_ptr<M_ParFactory> p_twn_fy(new M_Factory_Town()); // 多态工厂,城镇的工厂
shared_ptr<Monster> pM2 = p_twn_fy->createMonster_Undead(); // 创建城镇的亡灵类怪物
shared_ptr<Monster> pM3 = p_twn_fy->createMonster_Mechanic(); // 创建城镇的机械类怪物
#endif
#if 0
using namespace ns2;
shared_ptr<Monster> pM1 = M_Factory<M_Element_Town>::createMonster(); // 创建山脉地区的元素类怪物
shared_ptr<Monster> pM2 = M_Factory<M_Undead_Town>::createMonster(); // 创建城镇的亡灵类怪物
shared_ptr<Monster> pM3 = M_Factory<M_Mechanic_Town>::createMonster(400, 0, 110); // 创建城镇的机械类怪物
shared_ptr<Monster> pM = getMonster<M_Mechanic_Town>(400, 0, 110);
#endif
#if 1
using namespace ns3;
// 创建第一个芭比娃娃------------------------------------
//(1)创建一个中国工厂
shared_ptr<AbstractFactory> pChinaFactory(new ChinaFactory());
//(2)创建中国产的各种部件
shared_ptr<Body> pChinaBody = pChinaFactory->createBody();
shared_ptr<Clothes> pChinaClothes = pChinaFactory->createClothes();
shared_ptr<Shoes> pChinaShoes = pChinaFactory->createShoes();
//(3)创建芭比娃娃
shared_ptr<BarbieDoll> pbd1obj(new BarbieDoll(pChinaBody, pChinaClothes, pChinaShoes));
pbd1obj->Assemble(); // 组装芭比娃娃
// 创建第二个芭比娃娃------------------------------------
//(1)创建另外两个工厂:日本工厂,美国工厂
shared_ptr<AbstractFactory> pJapanFactory(new JapanFactory());
shared_ptr<AbstractFactory> pAmericaFactory(new AmericaFactory());
//(2)创建中国产的身体部件,日本产的衣服部件,美国产的鞋子部件
shared_ptr<Body> pChinaBody2 = pChinaFactory->createBody();
shared_ptr<Clothes> pJapanClothes = pJapanFactory->createClothes();
shared_ptr<Shoes> pAmericaShoes = pAmericaFactory->createShoes();
//(3)创建芭比娃娃
shared_ptr<BarbieDoll> pbd2obj(new BarbieDoll(pChinaBody2, pJapanClothes, pAmericaShoes));
pbd2obj->Assemble(); // 组装芭比娃娃
#endif
#if 0
using namespace ns4;
shared_ptr<AbstractFactory> shapeFactory = getFactory("SHAPE");
shared_ptr<Shape> shape1 = shapeFactory->getShape("CIRCLE");
shape1->draw();
shared_ptr<Shape> shape2 = shapeFactory->getShape("RECTANGLE");
shape2->draw();
shared_ptr<Shape> shape3 = shapeFactory->getShape("SQUARE");
shape3->draw();
shared_ptr<AbstractFactory> colorFactory = getFactory("COLOR");
shared_ptr<Color> color1 = colorFactory->getColor("RED");
color1->fill();
shared_ptr<Color> color2 = colorFactory->getColor("GREEN");
color2->fill();
shared_ptr<Color> color3 = colorFactory->getColor("BLUE");
color3->fill();
#endif
cout << "Over!\n";
return 0;
}
工厂方法模式.cpp
#include <iostream>
#include <memory>
using namespace std;
// 开闭原则(Open Close Principle,OCP)
// 对扩展开放,对修改关闭(封闭)
// 增加新功能时,不修改已有代码,而应该扩展代码(增加新类、新成员函数)。
// 一个工厂,一个产品
// 增加新子类时,添加子类工厂,已有代码不改变
namespace ns1
{
class Monster // 怪物父类
{
protected: // 怪物属性
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
};
class M_Undead : public Monster // 亡灵类怪物
{
public:
M_Undead(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A ghost monster came to this world" << endl; }
};
class M_Element : public Monster // 元素类怪物
{
public:
M_Element(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "An elemental monster came to this world" << endl; }
};
class M_Mechanic : public Monster // 机械类怪物
{
public:
M_Mechanic(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster came to this world" << endl; }
};
class M_ParFactory // 所有工厂类的父类
{
public:
virtual ~M_ParFactory() {}
virtual shared_ptr<Monster> createMonster() const = 0;
};
class M_UndeadFactory : public M_ParFactory // M_Undead怪物类型的工厂,生产M_Undead类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Undead>(300, 50, 80); } // 创建亡灵类怪物
};
class M_ElementFactory : public M_ParFactory // M_Element怪物类型的工厂,生产M_Element类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Element>(200, 80, 100); } // 创建元素类怪物
};
class M_MechanicFactory : public M_ParFactory // M_Mechanic怪物类型的工厂,生产M_Mechanic类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Mechanic>(400, 0, 110); } // 创建机械类怪物
};
}
namespace ns2
{
class Monster // 怪物父类
{
protected: // 怪物属性
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
};
class M_Undead : public Monster // 亡灵类怪物
{
public:
M_Undead(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A ghost monster came to this world" << endl; }
};
class M_Element : public Monster // 元素类怪物
{
public:
M_Element(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "An elemental monster came to this world" << endl; }
};
class M_Mechanic : public Monster // 机械类怪物
{
public:
M_Mechanic(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster came to this world" << endl; }
};
class M_ParFactory // 所有工厂类的父类
{
public:
virtual ~M_ParFactory() {}
virtual shared_ptr<Monster> createMonster() const = 0;
};
class M_UndeadFactory : public M_ParFactory // M_Undead怪物类型的工厂,生产M_Undead类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Undead>(300, 50, 80); } // 创建亡灵类怪物
};
class M_ElementFactory : public M_ParFactory // M_Element怪物类型的工厂,生产M_Element类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Element>(200, 80, 100); } // 创建元素类怪物
};
class M_MechanicFactory : public M_ParFactory // M_Mechanic怪物类型的工厂,生产M_Mechanic类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Mechanic>(400, 0, 110); } // 创建机械类怪物
};
// 模板实现
template <typename T>
class M_ChildFactory1 : public M_ParFactory
{
public:
shared_ptr<Monster> createMonster() const { return make_shared<T>(300, 50, 80); }
};
template <typename T>
class M_ChildFactory2
{
public:
static shared_ptr<Monster> createMonster(int life, int magic, int attack) { return make_shared<T>(life, magic, attack); }
};
class M_ChildFactory3
{
public:
template <typename T>
static shared_ptr<Monster> createMonster(int life, int magic, int attack) { return make_shared<T>(life, magic, attack); }
};
template <typename T>
shared_ptr<T> getMonster(int life, int magic, int attack) { return make_shared<T>(life, magic, attack); }
}
namespace ns3
{
class Monster // 怪物父类
{
protected: // 怪物属性
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
};
class M_Undead : public Monster // 亡灵类怪物
{
public:
M_Undead(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A ghost monster came to this world" << endl; }
};
class M_Element : public Monster // 元素类怪物
{
public:
M_Element(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "An elemental monster came to this world" << endl; }
};
class M_Mechanic : public Monster // 机械类怪物
{
public:
M_Mechanic(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "A mechanical monster came to this world" << endl; }
};
class M_ParFactory // 所有工厂类的父类
{
public:
virtual ~M_ParFactory() {}
virtual shared_ptr<Monster> createMonster() const = 0;
};
class M_UndeadFactory : public M_ParFactory // M_Undead怪物类型的工厂,生产M_Undead类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Undead>(300, 50, 80); } // 创建亡灵类怪物
};
class M_ElementFactory : public M_ParFactory // M_Element怪物类型的工厂,生产M_Element类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Element>(200, 80, 100); } // 创建元素类怪物
};
class M_MechanicFactory : public M_ParFactory // M_Mechanic怪物类型的工厂,生产M_Mechanic类型怪物
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Mechanic>(400, 0, 110); } // 创建机械类怪物
};
// 全局函数实现
shared_ptr<Monster> Gbl_CreateMonster(shared_ptr<M_ParFactory> &factory)
{
return factory->createMonster(); // createMonster虚函数扮演了多态new的行为,factory指向的具体怪物工厂类不同,创建的怪物对象也不同
}
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<M_ParFactory> p_ud_fy(new M_UndeadFactory()); // 多态工厂,注意指针类型
shared_ptr<Monster> pM1 = p_ud_fy->createMonster();
shared_ptr<M_ParFactory> p_elm_fy(new M_ElementFactory());
shared_ptr<Monster> pM2 = p_elm_fy->createMonster(); // 产生了一只元素类怪物
shared_ptr<M_ParFactory> p_mec_fy(new M_MechanicFactory());
shared_ptr<Monster> pM3 = p_mec_fy->createMonster(); // 产生了一只机械类怪物
#endif
#if 0
using namespace ns2;
M_ChildFactory1<M_Undead> myFactory;
shared_ptr<Monster> pM1 = myFactory.createMonster();
shared_ptr<Monster> pM2 = M_ChildFactory2<M_Element>::createMonster(400, 0, 100);
shared_ptr<Monster> pM3 = M_ChildFactory3::createMonster<M_Mechanic>(400, 0, 100);
shared_ptr<Monster> pM = getMonster<M_Mechanic>(400, 0, 100);
#endif
#if 1
using namespace ns3;
shared_ptr<M_ParFactory> p_ud_fy(new M_UndeadFactory()); // 多态工厂,注意指针类型
shared_ptr<Monster> pM1 = Gbl_CreateMonster(p_ud_fy); // 产生了一只亡灵类怪物,也是多态,注意返回类型
shared_ptr<M_ParFactory> p_elm_fy(new M_ElementFactory());
shared_ptr<Monster> pM2 = Gbl_CreateMonster(p_elm_fy); // 产生了一只元素类怪物
shared_ptr<M_ParFactory> p_mec_fy(new M_MechanicFactory());
shared_ptr<Monster> pM3 = Gbl_CreateMonster(p_mec_fy); // 产生了一只机械类怪物
#endif
cout << "Over!\n";
return 0;
}
原型模式.cpp
#include <iostream>
#include <memory>
using namespace std;
// 对象克隆
namespace ns1
{
class Monster // 怪物
{
protected:
int m_life; // 生命值
int m_magic; // 魔法值
int m_attack; // 攻击力
public:
Monster(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
virtual ~Monster() {}
virtual shared_ptr<Monster> clone() const = 0; // 对象克隆
};
class M_Undead : public Monster // 亡灵类
{
public:
M_Undead(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "M_Undead(int life, int magic, int attack)" << endl; }
M_Undead(const M_Undead &tmpobj) : Monster(tmpobj) { cout << "M_Undead(const M_Undead &tmpobj)" << endl; }
public:
shared_ptr<Monster> clone() const override { return make_shared<M_Undead>(*this); }
};
class M_Element : public Monster // 元素类
{
public:
M_Element(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "M_Element(int life, int magic, int attack)" << endl; }
M_Element(const M_Element &tmpobj) : Monster(tmpobj) { cout << "M_Element(const M_Element &tmpobj)" << endl; }
public:
shared_ptr<Monster> clone() const override { return make_shared<M_Element>(*this); }
};
class M_Mechanic : public Monster // 机械类
{
public:
M_Mechanic(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "M_Mechanic(int life, int magic, int attack)" << endl; }
M_Mechanic(const M_Mechanic &tmpobj) : Monster(tmpobj) { cout << "M_Mechanic(const M_Mechanic &tmpobj)" << endl; }
public:
shared_ptr<Monster> clone() const override { return make_shared<M_Mechanic>(*this); }
};
shared_ptr<Monster> CreateMonster(const shared_ptr<Monster> &pMonster)
{
/*
if (dynamic_pointer_cast<M_Undead>(pMonster) != nullptr)
return make_shared<M_Undead>(*dynamic_pointer_cast<M_Undead>(pMonster).get()); // 亡灵类
else if (dynamic_pointer_cast<M_Element>(pMonster) != nullptr)
return make_shared<M_Element>(*dynamic_pointer_cast<M_Element>(pMonster).get()); // 元素类
else if (dynamic_pointer_cast<M_Mechanic>(pMonster) != nullptr)
return make_shared<M_Mechanic>(*dynamic_pointer_cast<M_Mechanic>(pMonster).get()); // 机械类
else
return nullptr;
*/
return pMonster->clone();
}
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<Monster> myPropMecMonster(new M_Mechanic(400, 0, 110)); // 创建一只机械类怪物对象作为原型对象以用于克隆目的
shared_ptr<Monster> p_CloneObj1 = myPropMecMonster->clone(); // 使用原型对象克隆出新的机械类怪物对象
shared_ptr<Monster> pmyPropEleMonster(new M_Element(200, 80, 100));
shared_ptr<Monster> p_CloneObj2 = pmyPropEleMonster->clone();
#endif
#if 1
using namespace ns1;
shared_ptr<Monster> pMonsterObj(new M_Element(200, 80, 100));
shared_ptr<Monster> copyObj = CreateMonster(pMonsterObj);
#endif
cout << "Over!\n";
return 0;
}
建造者模式.cpp
#include <iostream>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
// 按顺序分步骤创建复杂对象
// 复杂对象的构建与表示分离,同样的构建过程创建不同的表示
namespace ns1
{
class Monster // 怪物父类
{
public:
virtual ~Monster() {}
virtual void LoadTrunkModel(const string &strno) = 0; // 这里也可以写为空函数体,子类决定是否重新实现
virtual void LoadHeadModel(const string &strno) = 0;
virtual void LoadLimbsModel(const string &strno) = 0;
public:
void Assemble(const string &strmodelno) // 参数:模型编号,形如“1253679201245”等,每些位的组合都有一些特别的含义,这里无需深究
{
LoadTrunkModel(strmodelno.substr(4, 3)); // 载入躯干模型,截取某部分字符串以表示躯干模型的编号
LoadHeadModel(strmodelno.substr(7, 3)); // 载入头部模型并挂接到躯干模型上
LoadLimbsModel(strmodelno.substr(10, 3)); // 载入四肢模型并挂接到躯干模型上
}
};
class M_Undead : public Monster // 亡灵类怪物
{
public:
void LoadTrunkModel(const string &strno) override { cout << "body + other M_Undead function" << endl; }
void LoadHeadModel(const string &strno) override { cout << "head + other M_Undead function" << endl; }
void LoadLimbsModel(const string &strno) override { cout << "limb + other M_Undead function" << endl; }
};
class M_Element : public Monster // 元素类怪物
{
public:
void LoadTrunkModel(const string &strno) override { cout << "body + other M_Element function" << endl; }
void LoadHeadModel(const string &strno) override { cout << "head + other M_Element function" << endl; }
void LoadLimbsModel(const string &strno) override { cout << "limb + other M_Element function" << endl; }
};
class M_Mechanic : public Monster // 机械类怪物
{
public:
void LoadTrunkModel(const string &strno) override { cout << "body + other M_Mechanic function" << endl; }
void LoadHeadModel(const string &strno) override { cout << "head + other M_Mechanic function" << endl; }
void LoadLimbsModel(const string &strno) override { cout << "limb + other M_Mechanic function" << endl; }
};
class MonsterBuilder // 怪物构建器父类
{
protected:
shared_ptr<Monster> m_pMonster;
public:
virtual ~MonsterBuilder() {}
MonsterBuilder(const shared_ptr<Monster> &monster = nullptr) : m_pMonster(monster) {}
virtual void LoadTrunkModel(const string &strno) = 0; // 这里也可以写为空函数体,子类决定是否重新实现
virtual void LoadHeadModel(const string &strno) = 0;
virtual void LoadLimbsModel(const string &strno) = 0;
public:
void Assemble(const string &strmodelno) // 参数:模型编号,形如“1253679201245”等,每些位的组合都有一些特别的含义,这里无需深究
{
LoadTrunkModel(strmodelno.substr(4, 3)); // 载入躯干模型,截取某部分字符串以表示躯干模型的编号
LoadHeadModel(strmodelno.substr(7, 3)); // 载入头部模型并挂接到躯干模型上
LoadLimbsModel(strmodelno.substr(10, 3)); // 载入四肢模型并挂接到躯干模型上
}
shared_ptr<Monster> GetResult() const { return m_pMonster; }
};
class M_UndeadBuilder : public MonsterBuilder // 亡灵类怪物构建器类
{
public:
M_UndeadBuilder() : MonsterBuilder(make_shared<M_Undead>()) {}
void LoadTrunkModel(const string &strno) override { cout << "body + other M_Undead function" << endl; }
void LoadHeadModel(const string &strno) override { cout << "head + other M_Undead function" << endl; }
void LoadLimbsModel(const string &strno) override { cout << "limb + other M_Undead function" << endl; }
};
class M_ElementBuilder : public MonsterBuilder // 元素类怪物构建器类
{
public:
M_ElementBuilder() : MonsterBuilder(make_shared<M_Element>()) {}
void LoadTrunkModel(const string &strno) override { cout << "body + other M_Element function" << endl; }
void LoadHeadModel(const string &strno) override { cout << "head + other M_Element function" << endl; }
void LoadLimbsModel(const string &strno) override { cout << "limb + other M_Element function" << endl; }
};
class M_MechanicBuilder : public MonsterBuilder // 机械类怪物构建器类
{
public:
M_MechanicBuilder() : MonsterBuilder(make_shared<M_Mechanic>()) {}
void LoadTrunkModel(const string &strno) override { cout << "body + other M_Mechanic function" << endl; }
void LoadHeadModel(const string &strno) override { cout << "head + other M_Mechanic function" << endl; }
void LoadLimbsModel(const string &strno) override { cout << "limb + other M_Mechanic function" << endl; }
};
class MonsterDirector // 指挥者类
{
shared_ptr<MonsterBuilder> m_pMonsterBuilder; // 指向所有构建器类的父类
public:
MonsterDirector(const shared_ptr<MonsterBuilder> &ptmpBuilder) : m_pMonsterBuilder(ptmpBuilder) {}
void SetBuilder(const shared_ptr<MonsterBuilder> &ptmpBuilder) { m_pMonsterBuilder = ptmpBuilder; }
// 原MonsterBuilder类中的Assemble成员函数
shared_ptr<Monster> Construct(const string &strmodelno) // 参数:模型编号,形如“1253679201245”等,每些位的组合都有一些特别的含义,这里无需深究
{
m_pMonsterBuilder->LoadTrunkModel(strmodelno.substr(4, 3)); // 载入躯干模型,截取某部分字符串以表示躯干模型的编号
m_pMonsterBuilder->LoadHeadModel(strmodelno.substr(7, 3)); // 载入头部模型并挂接到躯干模型上
m_pMonsterBuilder->LoadLimbsModel(strmodelno.substr(10, 3)); // 载入四肢模型并挂接到躯干模型上
return m_pMonsterBuilder->GetResult(); // 返回构建后的对象
}
};
}
namespace ns2
{
class DailyHeaderData // 日报中的“标题”部分
{
string m_strDepName; // 部门名称
string m_strGenDate; // 日报生成日期
public:
DailyHeaderData(const string &strDepName, const string &strGenDate) : m_strDepName(strDepName), m_strGenDate(strGenDate) {}
string getDepName() const { return m_strDepName; } // 获取部门名称
string getExportDate() const { return m_strGenDate; } // 获取日报生成日期
};
class DailyContentData // 日报中的“内容主体”部分 中的 每一条描述数据
{
string m_strContent; // 该项工作内容描述
double m_dspendTime; // 完成该项工作花费的时间(单位:小时)
public:
DailyContentData(const string &strContent, double dspendTime) : m_strContent(strContent), m_dspendTime(dspendTime) {}
string getContent() const { return m_strContent; } // 获取该项工作内容描述
double getSpendTime() const { return m_dspendTime; } // 获取完成该项工作花费的时间
};
class DailyFooterData // 日报中的“结尾”部分
{
string m_strUserName; // 日报所属员工姓名
public:
DailyFooterData(const string &strUserName) : m_strUserName(strUserName) {}
string getUserName() const { return m_strUserName; } // 获取日报所属员工姓名
};
class ExportToTxtFile // 将日报导出到纯文本格式文件相关的类
{
public:
void doExport(const shared_ptr<DailyHeaderData> &dailyheaderobj, const vector<shared_ptr<DailyContentData>> &vec_dailycontobj, const shared_ptr<DailyFooterData> &dailyfooterobj)
{
string strtmp = "";
//(1)拼接标题
strtmp += dailyheaderobj->getDepName() + "," + dailyheaderobj->getExportDate() + "\n";
//(2)拼接内容主体,内容主体中的描述数据会有多条,因此需要迭代
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss; // 记得#include头文件sstream
oss << (*iter)->getSpendTime();
strtmp += (*iter)->getContent() + ":(spend time: " + oss.str() + "hour)" + "\n";
}
//(3)拼接结尾
strtmp += "reporter: " + dailyfooterobj->getUserName() + "\n";
//(4)导出到真实文件的代码略,只展示在屏幕上文件的内容
cout << strtmp;
}
};
class ExportToXmlFile // 将日报导出到XML格式文件相关的类
{
public:
void doExport(const shared_ptr<DailyHeaderData> &dailyheaderobj, const vector<shared_ptr<DailyContentData>> &vec_dailycontobj, const shared_ptr<DailyFooterData> &dailyfooterobj)
{
string strtmp = "";
//(1)拼接标题
strtmp += "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
strtmp += "<DailyReport>\n";
strtmp += " <Header>\n";
strtmp += " <DepName>" + dailyheaderobj->getDepName() + "</DepName>\n";
strtmp += " <GenDate>" + dailyheaderobj->getExportDate() + "</GenDate>\n";
strtmp += " </Header>\n";
//(2)拼接内容主体,内容主体中的描述数据会有多条,因此需要迭代
strtmp += " <Body>\n";
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss; // 记得#include头文件sstream
oss << (*iter)->getSpendTime();
strtmp += " <Content>" + (*iter)->getContent() + "</Content>\n";
strtmp += " <SpendTime>spend time: " + oss.str() + "hour" + "</SpendTime>\n";
}
strtmp += " </Body>\n";
//(3)拼接结尾
strtmp += " <Footer>\n";
strtmp += " <UserName>reporter: " + dailyfooterobj->getUserName() + "</UserName>\n";
strtmp += " </Footer>\n";
strtmp += "</DailyReport>\n";
//(4)导出到真实文件的代码略,只展示在屏幕上文件的内容
cout << strtmp;
}
};
class FileBuilder // 抽象构建器类(文件构建器父类)
{
protected:
string m_strResult;
public:
virtual ~FileBuilder() {}
virtual void buildHeader(const shared_ptr<DailyHeaderData> &dailyheaderobj) = 0; // 拼接标题
virtual void buildBody(const vector<shared_ptr<DailyContentData>> &vec_dailycontobj) = 0; // 拼接内容主体
virtual void buildFooter(const shared_ptr<DailyFooterData> &dailyfooterobj) = 0; // 拼接结尾
void clearResult() { m_strResult = ""; }
string GetResult() const { return m_strResult; }
};
class TxtFileBuilder : public FileBuilder // 纯文本文件构建器类
{
public:
void buildHeader(const shared_ptr<DailyHeaderData> &dailyheaderobj) override // 拼接标题
{
m_strResult += dailyheaderobj->getDepName() + "," + dailyheaderobj->getExportDate() + "\n";
}
void buildBody(const vector<shared_ptr<DailyContentData>> &vec_dailycontobj) override // 拼接内容主体
{
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss; // 记得#include头文件sstream
oss << (*iter)->getSpendTime();
m_strResult += (*iter)->getContent() + ":(spend time: " + oss.str() + "hour)" + "\n";
}
}
void buildFooter(const shared_ptr<DailyFooterData> &dailyfooterobj) override // 拼接结尾
{
m_strResult += "reporter: " + dailyfooterobj->getUserName() + "\n";
}
};
class XmlFileBuilder : public FileBuilder // XML文件构建器类
{
public:
void buildHeader(const shared_ptr<DailyHeaderData> &dailyheaderobj) override // 拼接标题
{
m_strResult += "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
m_strResult += "<DailyReport>\n";
m_strResult += " <Header>\n";
m_strResult += " <DepName>" + dailyheaderobj->getDepName() + "</DepName>\n";
m_strResult += " <GenDate>" + dailyheaderobj->getExportDate() + "</GenDate>\n";
m_strResult += " </Header>\n";
}
void buildBody(const vector<shared_ptr<DailyContentData>> &vec_dailycontobj) override // 拼接内容主体
{
m_strResult += " <Body>\n";
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss; // 记得#include头文件sstream
oss << (*iter)->getSpendTime();
m_strResult += " <Content>" + (*iter)->getContent() + "</Content>\n";
m_strResult += " <SpendTime>spend time: " + oss.str() + "hour" + "</SpendTime>\n";
}
m_strResult += " </Body>\n";
}
void buildFooter(const shared_ptr<DailyFooterData> &dailyfooterobj) override // 拼接结尾
{
m_strResult += " <Footer>\n";
m_strResult += " <UserName>reporter: " + dailyfooterobj->getUserName() + "</UserName>\n";
m_strResult += " </Footer>\n";
m_strResult += "</DailyReport>\n";
}
};
class FileDirector // 文件指挥者类
{
shared_ptr<FileBuilder> m_pFileBuilder; // 指向所有构建器类的父类
public:
FileDirector(const shared_ptr<FileBuilder> &ptmpBuilder) : m_pFileBuilder(ptmpBuilder) {}
// 组装文件
string Construct(const shared_ptr<DailyHeaderData> &dailyheaderobj, const vector<shared_ptr<DailyContentData>> &vec_dailycontobj, const shared_ptr<DailyFooterData> &dailyfooterobj)
{
m_pFileBuilder->clearResult(); // 先清空结果
// 注意,有时指挥者需要和构建器通过参数传递的方式交换数据,这里指挥者通过委托的方式把功能交给构建器完成
m_pFileBuilder->buildHeader(dailyheaderobj);
m_pFileBuilder->buildBody(vec_dailycontobj);
m_pFileBuilder->buildFooter(dailyfooterobj);
return m_pFileBuilder->GetResult();
}
};
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<Monster> pmonster(new M_Element()); // 创建一只元素类怪物
pmonster->Assemble("1253679201245");
#endif
#if 0
using namespace ns1;
shared_ptr<MonsterBuilder> pMonsterBuilder(new M_UndeadBuilder()); // 创建亡灵类怪物构建器类对象
shared_ptr<MonsterDirector> pDirector(new MonsterDirector(pMonsterBuilder));
shared_ptr<Monster> pMonster = pDirector->Construct("1253679201245"); // 这里就构造出了一个完整的怪物对象
#endif
#if 0
using namespace ns2;
shared_ptr<DailyHeaderData> pdhd(new DailyHeaderData("R&D Department I", "November 1st"));
shared_ptr<DailyContentData> pdcd1(new DailyContentData("Complete the requirement analysis of Project A", 3.5));
shared_ptr<DailyContentData> pdcd2(new DailyContentData("Determine the tools used for project A development", 4.5));
vector<shared_ptr<DailyContentData>> vec_dcd;
vec_dcd.push_back(pdcd1);
vec_dcd.push_back(pdcd2);
shared_ptr<DailyFooterData> pdfd(new DailyFooterData("Xiao Li"));
shared_ptr<ExportToTxtFile> file_ettxt(new ExportToTxtFile());
file_ettxt->doExport(pdhd, vec_dcd, pdfd);
shared_ptr<ExportToXmlFile> file_etxml(new ExportToXmlFile());
file_etxml->doExport(pdhd, vec_dcd, pdfd);
#endif
#if 1
using namespace ns2;
shared_ptr<DailyHeaderData> pdhd(new DailyHeaderData("R&D Department I", "November 1st"));
shared_ptr<DailyContentData> pdcd1(new DailyContentData("Complete the requirement analysis of Project A", 3.5));
shared_ptr<DailyContentData> pdcd2(new DailyContentData("Determine the tools used for project A development", 4.5));
vector<shared_ptr<DailyContentData>> vec_dcd;
vec_dcd.push_back(pdcd1);
vec_dcd.push_back(pdcd2);
shared_ptr<DailyFooterData> pdfd(new DailyFooterData("Xiao Li"));
shared_ptr<FileBuilder> pfb(new TxtFileBuilder());
shared_ptr<FileDirector> pDtr(new FileDirector(pfb));
cout << pDtr->Construct(pdhd, vec_dcd, pdfd) << endl;
pfb.reset(new XmlFileBuilder());
pDtr.reset(new FileDirector(pfb));
cout << pDtr->Construct(pdhd, vec_dcd, pdfd) << endl;
#endif
cout << "Over!\n";
return 0;
}
文章来源:https://blog.csdn.net/oqqyx1234567/article/details/128665682
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!