From 580b2d1a1359815360eae420444dba44b741fcae Mon Sep 17 00:00:00 2001 From: liujj Date: Tue, 5 Jul 2022 17:52:34 +0800 Subject: [PATCH] =?UTF-8?q?[IM]:=20=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\346\250\241\345\274\217.md" | 417 +++++++++++++++++- 1 file changed, 416 insertions(+), 1 deletion(-) diff --git "a/02.c++\347\254\224\350\256\260/16.C++\350\256\276\350\256\241\346\250\241\345\274\217.md" "b/02.c++\347\254\224\350\256\260/16.C++\350\256\276\350\256\241\346\250\241\345\274\217.md" index a954666..e21701a 100644 --- "a/02.c++\347\254\224\350\256\260/16.C++\350\256\276\350\256\241\346\250\241\345\274\217.md" +++ "b/02.c++\347\254\224\350\256\260/16.C++\350\256\276\350\256\241\346\250\241\345\274\217.md" @@ -1282,10 +1282,162 @@ int main() { # 9.模板方法模式 -0 - +定义一个操作中算法的框架,而将一些步骤延迟到子类中。模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。 + +```cpp +#include +using namespace std; + +class DrinkTemplate { +public: + // 煮水 + virtual void BoildWater() = 0; + // 冲泡 + virtual void Brew() = 0; + // 导入杯中 + virtual void PourInCup() = 0; + // 加入辅料 + virtual void AddSomething() = 0; + + // 模板方法 + + void Make() { + BoildWater(); + Brew(); + PourInCup(); + AddSomething(); + } +}; + +// 冲咖啡 +class Coffee : public DrinkTemplate { +public: + // 煮水 + virtual void BoildWater() { + cout << "boild water..." << endl; + } + // 冲泡 + virtual void Brew() { + cout << "brew coffee..." << endl; + } + // 导入杯中 + virtual void PourInCup() { + cout << "put coffee in cup..." << endl; + } + // 加入辅料 + virtual void AddSomething() { + cout << "add somethin... " << endl; + } +}; + +// 茶 +class Tea : public DrinkTemplate { +public: + // 煮水 + virtual void BoildWater() { + cout << "boild water..." << endl; + } + // 冲泡 + virtual void Brew() { + cout << "brew tea..." << endl; + } + // 导入杯中 + virtual void PourInCup() { + cout << "put tea in cup..." << endl; + } + // 加入辅料 + virtual void AddSomething() { + cout << "add somethin... " << endl; + } +}; + +void test01() { + Tea *tea = new Tea(); + tea->Make(); + + cout << "----" << endl; + Coffee *coffee = new Coffee(); + coffee->Make(); +} + +int main() { + + test01(); + return 0; +} + +``` + + + + # 10.策略模式 +> 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们还可以相互替换。策略模式让算法独立于使用它的客户二独立变化。 + +```cpp +#include +using namespace std; + +// 抽象武器 武器策略 +class WeaponStrategy { +public: + virtual void UseWeapon() = 0; +}; + +class Knife: public WeaponStrategy { +public: + virtual void UseWeapon() { + cout << "使用匕首" << endl; + } +}; + +class AK47: public WeaponStrategy { +public: + virtual void UseWeapon() { + cout << "使用AK47" << endl; + } +}; + +class Character { +public: + void setWeapon(WeaponStrategy *weapon) { + this->pWeapon = weapon; + } + + void ThrowWeapon() { + this->pWeapon->UseWeapon(); + } + +public: + WeaponStrategy *pWeapon; +}; + +void test() { + Character *character = new Character; + WeaponStrategy *knife = new Knife; + WeaponStrategy *ak47 = new AK47; + + character->setWeapon(knife); + character->ThrowWeapon(); + + character->setWeapon(ak47); + character->ThrowWeapon(); + + delete ak47; + delete knife; +} + + +int main() { + test(); + + return 0; +} + +``` + # 11.命令模式 @@ -1294,6 +1446,269 @@ int main() { # 12.观察者模式 +观察者模式是用于简历一种对象与对象之间的依赖关系,一个对象发生改变时将自动通知其他对象,其他对象将相应做出反应。在观察者模式中,发生改变的对象称为观察目标,而被通知的对象称为观察者,一个观察目标可以对应多个观察者 + + + +```cpp +#include +#include +using namespace std; + + +// 抽象的英雄 抽象的观察者 +class AbstractHero { +public: + virtual void Update() = 0; +}; + +// 具体的英雄 具体的观察者 +class HeroA: public AbstractHero { +public: + HeroA() { + cout << "英雄A正在撸boss..." << endl; + } + + virtual void Update() { + cout << "英雄A停止撸,待机状态..." << endl; + } +}; + +class HeroB: public AbstractHero { +public: + HeroB() { + cout << "英雄B正在撸boss..." << endl; + } + + virtual void Update() { + cout << "英雄B停止撸,待机状态..." << endl; + } +}; + +class HeroC: public AbstractHero { +public: + HeroC() { + cout << "英雄C正在撸boss..." << endl; + } + virtual void Update() { + cout << "英雄C停止撸,待机状态..." << endl; + } +}; + +class HeroD: public AbstractHero { +public: + HeroD() { + cout << "英雄D正在撸boss..." << endl; + } + + virtual void Update() { + cout << "英雄D停止撸,待机状态..." << endl; + } +}; + +class HeroE: public AbstractHero { +public: + HeroE() { + cout << "英雄E正在撸boss..." << endl; + } + + virtual void Update() { + cout << "英雄E停止撸,待机状态..." << endl; + } +}; + +// 观察目标 抽象 +class AbstractBoss { +public: + // 添加观察者 + virtual void addHero(AbstractHero *hero) = 0; + // 删除挂观察者 + virtual void deleteHero(AbstractHero *hero) = 0; + // 通知所有观察者 + virtual void notify() = 0; +}; + +// 具体观察者 BOSSA +class BOSSA : public AbstractBoss { +public: + // 添加观察者 + virtual void addHero(AbstractHero *hero) { + pHeroList.push_back(hero); + } + // 删除挂观察者 + virtual void deleteHero(AbstractHero *hero) { + pHeroList.remove(hero); + } + // 通知所有观察者 + virtual void notify() { + for (auto it = pHeroList.begin(); it != pHeroList.end(); ++it) { + (*it)->Update(); + } + } +public: + list pHeroList; +}; + +void test() { + // 创建观察者 + AbstractHero *heroA = new HeroA; + AbstractHero *heroB = new HeroB; + AbstractHero *heroC = new HeroC; + AbstractHero *heroD = new HeroD; + AbstractHero *heroE = new HeroE; + + // 创建观察目标 + AbstractBoss *bossA = new BOSSA; + bossA->addHero(heroA); + bossA->addHero(heroB); + bossA->addHero(heroC); + bossA->addHero(heroD); + bossA->addHero(heroE); + + cout << "heroC 阵亡..." << endl; + bossA->deleteHero(heroC); + + cout << "boss 死了.....停止工攻击....抢装备..." << endl; + bossA->notify(); + +} + +int main() { + test(); + + return 0; +} + + +``` + # 13.装饰器模式 + +​ 装饰器模式又叫包装模式,通过一种对客户端透明的方式老扩展对象功能,是继承关系的一种替代。 + +​ 装饰器模式就是把要附加的功能飞别放在单独的类中,并让这个类包含它要装饰的对象,当需要执行时,客户端就可以有选择的、按顺序的使用装饰功能包装 + + + +```cpp +#include +using namespace std; + +using namespace std; + +// 一般情况下,用继承实现类的功能的拓展 +// 装饰模式 可以动态的给一个类增加功能 + +// 抽象英雄 +class AbstractHero { +public: + virtual void ShowStatus() = 0; +public: + int hp; // 血条 + int mp; // 蓝条 + int at; // 攻击 + int df; // 防御 +}; + +// 具体英雄 +class HeroA: public AbstractHero { +public: + HeroA() { + hp = 0; + mp = 0; + at = 0; + df = 0; + } + virtual void ShowStatus() { + cout << "血条: " << hp << endl; + cout << "蓝条: " << mp << endl; + cout << "攻击: " << at << endl; + cout << "防御: " << df << endl; + } +}; + +// 英雄穿上某个装饰物 +// 装饰物基类 +class AbstractEquipment : public AbstractHero { +public: + AbstractEquipment(AbstractHero *hero) { + this->pHero = hero; + } + + virtual void ShowStatus() = 0; +public: + AbstractHero *pHero; +}; + +// 狂徒 +class KuangtuEquipment: public AbstractEquipment { +public: + KuangtuEquipment(AbstractHero *hero) : AbstractEquipment(hero) {} + // 增加额外功能 + void addKuangtu() { + cout << "英雄穿上狂徒... 防御+30..." << endl; + this->hp = this->pHero->hp; + this->mp = this->pHero->mp; + this->at = this->pHero->at; + this->df = this->pHero->df + 30; + + delete this->pHero; + } + virtual void ShowStatus() { + addKuangtu(); + cout << "血条: " << hp << endl; + cout << "蓝条: " << mp << endl; + cout << "攻击: " << at << endl; + cout << "防御: " << df << endl; + } +}; + +// 无尽之刃 +class WujingKnife: public AbstractEquipment { +public: + WujingKnife(AbstractHero *hero) : AbstractEquipment(hero) {} + // 增加额外功能 + void addWujing() { + cout << "英雄拿上无尽之刃... 攻击+80..." << endl; + this->hp = this->pHero->hp; + this->mp = this->pHero->mp; + this->at = this->pHero->at + 80; + this->df = this->pHero->df; + + delete this->pHero; + } + virtual void ShowStatus() { + addWujing(); + cout << "血条: " << hp << endl; + cout << "蓝条: " << mp << endl; + cout << "攻击: " << at << endl; + cout << "防御: " << df << endl; + } +}; + +void test() { + AbstractHero *hero = new HeroA; + hero->ShowStatus(); + + cout << "-------" << endl; + // 给裸奔的英雄穿上衣服 + hero = new KuangtuEquipment(hero); + hero->ShowStatus(); + + // 给英雄装备武器 + cout << "----" << endl; + hero = new WujingKnife(hero); + hero->ShowStatus(); +} + + +int main() { + test(); + + return 0; +} + +``` +