Skip to content

Commit

Permalink
[IM]: 添加设计模式
Browse files Browse the repository at this point in the history
  • Loading branch information
liujj committed Jul 5, 2022
1 parent feafea5 commit 629cc19
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 0 deletions.
80 changes: 80 additions & 0 deletions 03.code/18.设计模式/14.模版方法模式.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
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;
}
58 changes: 58 additions & 0 deletions 03.code/18.设计模式/15.策略模式.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
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;
}
127 changes: 127 additions & 0 deletions 03.code/18.设计模式/16.观察者模式.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <iostream>
#include <list>
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<AbstractHero*> 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;
}
116 changes: 116 additions & 0 deletions 03.code/18.设计模式/17.装饰模式.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <iostream>
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;
}

0 comments on commit 629cc19

Please sign in to comment.