C++_设计模式
单例模式
确保一个类只有一个实例,并提供一个全局访问点
-
饿汉模式
饿汉模式是在程序启动时就创建实例,因此不存在线程安全问题,但可能会造成资源浪费,特别是当单例对象的初始化代价较高或程序中没有频繁使用该对象时。
- 构造函数私有化,这样用户就不能任意定义该类型的对象了
- 定义该类型唯一的对象
- 通过一个static静态成员方法返回唯一的对象实例
-
-
#include <iostream> class SingletonHungry { private: static SingletonHungry* instance; SingletonHungry() {} // 私有构造函数 public: static SingletonHungry* getInstance() { return instance; } }; // 初始化静态成员 SingletonHungry* SingletonHungry::instance = new SingletonHungry(); int main() { SingletonHungry* s1 = SingletonHungry::getInstance(); SingletonHungry* s2 = SingletonHungry::getInstance(); // s1 和 s2 指向同一个实例 std::cout << (s1 == s2) << std::endl; // 输出 1 表示是同一个实例 return 0; }
-
-
懒汉设计模式
-
#include <iostream> //懒汉式单例设计模式 class Single { public: //3. 获取类的唯一实例对象的接口方法 static Single* getInstacne() { std::cout << "hello codechen,你成功的调用了一次构造函数" << char(10); if (instance == nullptr) { std::cout << "在这里对instance进行了初始化" << char(10); instance = new Single(); } return instance; } private: //2. 定义一个唯一的类的实例化的对象,为指针 static Single* instance; //1. 构造函数的私有化,不能够自己主动的去调用构造函数 Single() {//在实际的项目中,构造函数可能会需要做很多的工作 //比如说对一些成员变量的初始化,对一些数据的读取等等 } Single(const Single&) = delete; Single& operator = (const Single&) = delete; }; Single* Single::instance = nullptr; int main() { Single* ptr1 = Single::getInstacne(); Single* ptr2 = Single::getInstacne(); Single* ptr3 = Single::getInstacne(); delete ptr1; delete ptr2; delete ptr3; return 0; }
-
工厂模式
-
简单工厂模式
-
namespace _namespace1 { class Monster { public: Monster(int life, int magic, int attack) :m_life(life), m_magic(magic), m_attack(attack) {} virtual ~Monster() // 基类 析构 虚方法 { } protected: int m_life; int m_magic; int m_attack; }; class M_Undead : public Monster { public: M_Undead(int life, int magic, int attack) : Monster(life,magic, attack) { cout << "亡灵类动物" << endl; } }; class M_Element : public Monster { public: M_Element(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "元素类动物" << endl; } }; class M_Mechanic : public Monster { public: M_Mechanic(int life, int magic, int attack) : Monster(life, magic, attack) { cout << "机械类动物" << endl; } }; // 简单工厂模式 class MonsterFactor { public: Monster * createMonster(string strmontype) { Monster *ptrobj = nullptr; if (strmontype == "a") { ptrobj = new M_Undead(300, 100, 100); } else if (strmontype == "b") { ptrobj = new M_Element(300, 100, 100); } else if (strmontype == "c") { ptrobj = new M_Mechanic(300, 100, 100); } return ptrobj; } }; } void test2() { //2 简单工厂模式的实现思路:使用工厂类可以实现创建怪物的代码,用户在创建时候,与具体的类对象要实现的逻辑代码隔离。 _namespace1::MonsterFactor fac; _namespace1::Monster *m1 = fac.createMonster("a"); _namespace1::Monster *m2 = fac.createMonster("b"); _namespace1::Monster *m3 = fac.createMonster("c"); 实例化一个工厂对象,然后通过向工厂中传递对应的标识来创建对象。 /* 亡灵类动物 元素类动物 机械类动物 */ }
-
利弊分析
-
-
-
- 开闭原则:代码的扩展性问题:对扩展开发,对修改关闭。当增加新功能,不应该通过修改已经存在的代码,比如在createMonster()中增加if else(), 而是应该通过扩展代码比如增加新类,增加新成员函数来进行扩展。假如上边要增加100个怪物类型,就要增加100个 if else(),这样的做法不可取,如果只增加几个,这样方法也可以,所以应该在代码的可读性和可扩展性之间做出权衡
-
-
工厂方法模式
工厂方法模式简称工厂模式或多态工厂模式;与简单工厂模式比,灵活性更强,实现也更加复杂,一如更多的新类。每一个工厂类对应了怪物类,比如亡灵类对应的工厂为亡灵工厂类。工厂模式:修改代码不如增加代码好,符合开闭原则。
-
-
namespace _sp1 { // 怪物类父类 class CMonster { public: CMonster(int life,int maigc,int attack):m_life(life),m_magic(maigc), m_attack(attack) { } protected: int m_life; int m_magic; int m_attack; }; // 亡灵类 class CUnded : public CMonster { public: CUnded(int life, int maigc, int attack) :CMonster(life, maigc, attack) { cout << "亡灵类来到世界" << endl; } private: }; // 元素类 class CEle : public CMonster { public: CEle(int life, int maigc, int attack) :CMonster(life, maigc, attack) { cout << "元素类来到世界" << endl; } private: }; // 机械类 class CMecanical : public CMonster { public: CMecanical(int life, int maigc, int attack) :CMonster(life, maigc, attack) { cout << "机械类来到世界" << endl; } }; // 简单工厂模式:创建一个工厂类,在工厂类中返回对应的 怪物类; // 工厂方法 // 1 创建一个工厂基类; class CFactorMonster { public: virtual CMonster * createMonster() = 0; virtual ~CFactorMonster() { } }; // 2 创建每个怪物的工厂 class CFactorUnded : public CFactorMonster { public: virtual CMonster * createMonster() { CMonster *ptmp = new CUnded(200,300,400); return ptmp; } }; class CFactorCEle : public CFactorMonster { public: virtual CMonster * createMonster() { CMonster *ptmp = new CEle(200, 300, 400); // 多态 return ptmp; } }; // 创建一个全局方法 CMonster *GlobalCreateMonster(CFactorMonster *factory) { return factory->createMonster(); // 多态 } } void test2() { // 先 创建一个工厂父类;由于每个工厂具有固定的步骤,所以有工厂父类; _sp1::CFactorMonster *p = new _sp1::CFactorUnded(); _sp1::CMonster *pp = p->createMonster(); _sp1::CFactorMonster *p2 = new _sp1::CFactorCEle(); _sp1::CMonster *pp2 = p2->createMonster(); // 工厂模式创建了一个工厂父类,在此基础上,又增加了每个怪物对应的工厂; // 与简单工厂模式比,比之前的复杂,但是灵活性更强,实现了 开闭原则,付出的代价是新增加了每个怪物的工厂类; // }
-
-
抽象工厂模式
-
namespace _nsp1 { // 怪物类父类 class CMonster { public: CMonster(int life, int maigc, int attack) :m_life(life), m_magic(maigc), m_attack(attack) { } protected: int m_life; int m_magic; int m_attack; }; /// 下面分别实现这9个类别,每个怪物类都继承自怪物父类 // 城镇亡灵类 class CMonsterTownUndead : public CMonster { public: CMonsterTownUndead(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个城镇亡灵类型怪物来到了这个世界" << endl; } }; // 城镇元素类 class CMonsterTownElement : public CMonster { public: CMonsterTownElement(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个城镇元素类型怪物来到了这个世界" << endl; } }; // 城镇机械类 class CMonsterTownMechanic : public CMonster { public: CMonsterTownMechanic(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个城镇机械类型怪物来到了这个世界" << endl; } }; /// 山脉类 // 山脉亡灵类 class CMonsterMaintainUndead : public CMonster { public: CMonsterMaintainUndead(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个山脉亡灵类型怪物来到了这个世界" << endl; } }; // 山脉元素类 class CMonsterMaintainElement : public CMonster { public: CMonsterMaintainElement(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个山脉元素类型怪物来到了这个世界" << endl; } }; // 山脉机械类 class CMonsterMaintainMechanic : public CMonster { public: CMonsterMaintainMechanic(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个山脉机械类型怪物来到了这个世界" << endl; } }; /// 沼泽类 // 沼泽亡灵类 class CMonsterMarshUndead : public CMonster { public: CMonsterMarshUndead(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个沼泽亡灵类型怪物来到了这个世界" << endl; } }; // 沼泽元素类 class CMonsterMarshElement : public CMonster { public: CMonsterMarshElement(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个沼泽元素类型怪物来到了这个世界" << endl; } }; // 沼泽机械类 class CMonsterMarshMechanic : public CMonster { public: CMonsterMarshMechanic(int life, int magic, int attack) : CMonster(life, magic, attack) { cout << "一个沼泽机械类型怪物来到了这个世界" << endl; } }; /// 创建工厂 class CMonsterFactory { public: virtual CMonster *createMonsterUndead() = 0; virtual CMonster *createMonsterElement() = 0; virtual CMonster *createMonsterMechanic() = 0; virtual ~CMonsterFactory() { } }; // 城镇类工厂:一个工厂能生产一个产品族 class CMonsterFactoryTown : public CMonsterFactory { virtual CMonster *createMonsterUndead() { return new CMonsterTownUndead(100, 100, 100); } virtual CMonster *createMonsterElement() { return new CMonsterTownElement(100, 100, 100); } virtual CMonster *createMonsterMechanic() { return new CMonsterTownMechanic(100, 100, 100); } }; // 山脉类怪物工厂 class CMonsterFactoryMaintain : public CMonsterFactory { virtual CMonster *createMonsterUndead() { return new CMonsterMaintainUndead(100, 100, 100); } virtual CMonster *createMonsterElement() { return new CMonsterMaintainElement(100, 100, 100); } virtual CMonster *createMonsterMechanic() { return new CMonsterMaintainMechanic(100, 100, 100); } }; // 沼泽类怪物工厂 class CMonsterFactoryMarsh : public CMonsterFactory { virtual CMonster *createMonsterUndead() { return new CMonsterMarshUndead(100, 100, 100); } virtual CMonster *createMonsterElement() { return new CMonsterMarshElement(100, 100, 100); } virtual CMonster *createMarshMechanic() { return new CMonsterMaintainMechanic(100, 100, 100); } }; } int main() { _nsp1::CMonsterFactory *pc = new _nsp1::CMonsterFactoryTown(); _nsp1::CMonster *pM1 = pc->createMonsterUndead(); _nsp1::CMonster *pM2 = pc->createMonsterMechanic(); _nsp1::CMonster *pM3 = pc->createMonsterElement(); /* 一个城镇亡灵类型怪物来到了这个世界 一个城镇机械类型怪物来到了这个世界 一个城镇元素类型怪物来到了这个世界 */ system("pause"); return 0; }
-

浙公网安备 33010602011771号