本文主要是介绍c++ 设计模式 的课本范例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(1) 框架设计模式 model mode : 算法的框架不变,算法的细节可以改变。主要依赖多态。
class Player
{
protected:int life;int magic;int attack;virtual void effect_self() {}virtual void effect_enemy() {}virtual bool can_burn() = 0;
public:Player(int life, int magic, int attack) : life(life), magic(magic), attack(attack) {}virtual Player(){}void play_effect_burn() { cout << "play_effect_burn\n"; }void burn() // 模板模式:算法的框架不变,细节可以变{if (can_burn()){effect_enemy();effect_self();play_effect_burn();}}
};class Fighter : public Player
{
public:Fighter() : Player(100, 100, 50) {}void effect_self() override { this->life -= 30; }void effect_enemy() override { cout << "敌人被燃烧 40 血\n"; }bool can_burn() override { if (this->life >= 40) return true;else return false; }
};
(2)简单工厂模式:不要直接使用 new A() 创建对象,一旦对类 A 增删和修改参数,会紧耦合,牵一发动全身,用一个函数集中使用 new A ,返回创建好的对象,如同工厂批量生产产品一样。对构造对象时 的修改也限制在工厂方法里。
class Player // 角色父类
{
protected:int life;int magic;int attack;public:Player(int life, int magic, int attack) : life(life), magic(magic), attack(attack) {}virtual Player(){}
};class Fighter : public Player // 战士
{
public:Fighter() : Player(100, 100, 50) {}
};class Master : public Player // 法师
{
public:Master() : Player(50, 300, 150) {}
};class Create // 把 new 语句集中在产生对象的函数里,减小代码以后升级时需要修改的范围
{
public:static Player* createPlayer(string str){if(str == "fighter") return new Fighter();else if(str == "master")return new Master();}
};int main()
{auto pFighter = Create::createPlayer("fighter");auto pMaster = Create::createPlayer("master");delete pFighter;delete pMaster;return 0;
}
但工厂函数里的 if 选择,如果要创建新类,会修改原代码。面向对象的 OCP 原则:更新代码时,尽量追加新代码,而不是修改原代码,向增加开放,向修改关闭。如此引出工厂模式。
(3) 工厂模式:符合 OCP 规则的 用工厂方式生产对象:
class Player // 角色父类
{
protected:int life;int magic;int attack;public: Player(int life, int magic, int attack) : life(life), magic(magic), attack(attack) {}virtual Player(){}
};class Fighter : public Player // 战士
{
public: Fighter(int life, int magic, int attack) : Player(life , magic , attack) {}
};class Master : public Player // 法师
{
public: Master(int life, int magic, int attack) : Player(life, magic, attack) {}
};class Create // 制造生产对象的虚基类
{
public: virtual Player* createPlayer() = 0;virtual Create(){}
};class Create_Fighter : public Create // 对应对象的工厂类
{
public: Player* createPlayer() override { return new Fighter(50,50,50); }
};class Create_Monster : public Create
{
public: Player* createPlayer() override { return new Master(60,60,60); }
};int main()
{auto pFactFight = new Create_Fighter();auto pFighter = pFactFight->createPlayer();auto pFactMaster = new Create_Monster();auto pMastr = pFactMaster->createPlayer();delete pFactFight;delete pFactMaster;delete pFighter;delete pMastr;return 0;
}
(4) 抽象工厂模式,比工厂模式密度更高的生产对象的模式:一个类包含了多个生产对象的函数:
class Player // 角色父类
{
protected:int life;int magic;int attack;public: Player(int life, int magic, int attack) : life(life), magic(magic), attack(attack) {}virtual Player(){}
};class Fighter_Land : public Player // 陆上战士
{
public: Fighter_Land(int life, int magic, int attack) : Player(life , magic , attack) {}
};class Fighter_Sea : public Player // 海洋战士
{
public: Fighter_Sea(int life, int magic, int attack) : Player(life, magic, attack) {}
};class Master_Land : public Player // 陆上法师 ,游戏新版本,不同的游戏场景
{
public: Master_Land (int life, int magic, int attack) : Player(life, magic, attack) {}
};class Master_Sea : public Player // 海洋法师
{
public: Master_Sea(int life, int magic, int attack) : Player(life, magic, attack) {}
};class Create // 制造生产对象的虚基类
{
public: virtual Player* createPlayer() = 0;virtual Player* createMaster() = 0;virtual Create(){}
};class Create_Land : public Create // 不同场景下的角色生产工厂
{
public:Player* createPlayer() override { return new Fighter_Land(10,10,10); }Player* createMaster() override { return new Master_Land(20,20,20); }
};class Create_Sea : public Create
{
public:Player* createPlayer() override { return new Fighter_Sea(10,10,10); }Player* createMaster() override { return new Master_Sea(20, 20, 20); }
};int main()
{auto pCreate_Land = new Create_Land();auto pFighter_Land = pCreate_Land->createPlayer();;auto PMaster_Land = pCreate_Land->createMaster();auto pCreate_Sea = new Create_Sea();auto pFighter_Sea = pCreate_Sea->createPlayer();auto pMaster_Sea = pCreate_Sea->createMaster();delete pCreate_Land; delete pFighter_Land; delete PMaster_Land;delete pCreate_Sea; delete pFighter_Sea; delete pMaster_Sea;return 0;
}
(5)
谢谢
这篇关于c++ 设计模式 的课本范例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!