《C++新经典设计模式》之第3章 工厂模式、原型模式、建造者模式
-
-
-
- 简单工厂模式.cpp
- 抽象工厂模式.cpp
- 工厂方法模式.cpp
- 原型模式.cpp
- 建造者模式.cpp
简单工厂模式.cpp
#include
#include
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")
return make_shared<M_Undead>(300, 50, 80);
else if (strmontype == "elm")
return make_shared<M_Element>(200, 80, 100);
else if (strmontype == "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")
return make_shared<M_Undead>(300, 50, 80);
else if (strmontype == "elm")
return make_shared<M_Element>(200, 80, 100);
else if (strmontype == "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");
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");
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
#include
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;
shared_ptr<AbstractFactory> pChinaFactory(new ChinaFactory());
shared_ptr<Body> pChinaBody = pChinaFactory->createBody();
shared_ptr<Clothes> pChinaClothes = pChinaFactory->createClothes();
shared_ptr<Shoes> pChinaShoes = pChinaFactory->createShoes();
shared_ptr<BarbieDoll> pbd1obj(new BarbieDoll(pChinaBody, pChinaClothes, pChinaShoes));
pbd1obj->Assemble();
shared_ptr<AbstractFactory> pJapanFactory(new JapanFactory());
shared_ptr<AbstractFactory> pAmericaFactory(new AmericaFactory());
shared_ptr<Body> pChinaBody2 = pChinaFactory->createBody();
shared_ptr<Clothes> pJapanClothes = pJapanFactory->createClothes();
shared_ptr<Shoes> pAmericaShoes = pAmericaFactory->createShoes();
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
#include
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; }
};
class M_ParFactory
{
public:
virtual ~M_ParFactory() {}
virtual shared_ptr<Monster> createMonster() const = 0;
};
class M_UndeadFactory : public M_ParFactory
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Undead>(300, 50, 80); }
};
class M_ElementFactory : public M_ParFactory
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Element>(200, 80, 100); }
};
class M_MechanicFactory : public M_ParFactory
{
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
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Undead>(300, 50, 80); }
};
class M_ElementFactory : public M_ParFactory
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Element>(200, 80, 100); }
};
class M_MechanicFactory : public M_ParFactory
{
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
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Undead>(300, 50, 80); }
};
class M_ElementFactory : public M_ParFactory
{
public:
shared_ptr<Monster> createMonster() const override { return make_shared<M_Element>(200, 80, 100); }
};
class M_MechanicFactory : public M_ParFactory
{
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();
}
}
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
#include
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)
{
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
#include
#include
#include
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)
{
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)
{
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; }
shared_ptr<Monster> Construct(const string &strmodelno)
{
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 = "";
strtmp += dailyheaderobj->getDepName() + "," + dailyheaderobj->getExportDate() + "\n";
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss;
oss << (*iter)->getSpendTime();
strtmp += (*iter)->getContent() + ":(spend time: " + oss.str() + "hour)" + "\n";
}
strtmp += "reporter: " + dailyfooterobj->getUserName() + "\n";
cout << strtmp;
}
};
class ExportToXmlFile
{
public:
void doExport(const shared_ptr<DailyHeaderData> &dailyheaderobj, const vector<shared_ptr<DailyContentData>> &vec_dailycontobj, const shared_ptr<DailyFooterData> &dailyfooterobj)
{
string strtmp = "";
strtmp += "\n";
strtmp += "\n";
strtmp += " ;
strtmp += " " + dailyheaderobj->getDepName() + "\n";
strtmp += " " + dailyheaderobj->getExportDate() + "\n";
strtmp += " \n";
strtmp += " \n";
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss;
oss << (*iter)->getSpendTime();
strtmp += " " + (*iter)->getContent() + "\n";
strtmp += " spend time: " + oss.str() + "hour" + "\n";
}
strtmp += " \n";
strtmp += " ;
strtmp += " reporter: " + dailyfooterobj->getUserName() + "\n";
strtmp += " \n";
strtmp += "\n";
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;
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
{
public:
void buildHeader(const shared_ptr<DailyHeaderData> &dailyheaderobj) override
{
m_strResult += "\n";
m_strResult += "\n";
m_strResult += " ;
m_strResult += " " + dailyheaderobj->getDepName() + "\n";
m_strResult += " " + dailyheaderobj->getExportDate() + "\n";
m_strResult += " \n";
}
void buildBody(const vector<shared_ptr<DailyContentData>> &vec_dailycontobj) override
{
m_strResult += " \n";
for (auto iter = vec_dailycontobj.cbegin(); iter != vec_dailycontobj.cend(); ++iter)
{
ostringstream oss;
oss << (*iter)->getSpendTime();
m_strResult += " " + (*iter)->getContent() + "\n";
m_strResult += " spend time: " + oss.str() + "hour" + "\n";
}
m_strResult += " \n";
}
void buildFooter(const shared_ptr<DailyFooterData> &dailyfooterobj) override
{
m_strResult += " ;
m_strResult += " reporter: " + dailyfooterobj->getUserName() + "\n";
m_strResult += " \n";
m_strResult += "\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;
}