C++设计模式之策略模式
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 Hero {
public:
void setWeapon(WeaponStrategy *weapon)
{
this->weapon = weapon;
}
void throwWeapon()
{
this->weapon->useWeapon();
}
private:
WeaponStrategy* weapon;
};
int main()
{
Hero* hero = new Hero;
WeaponStrategy* knife = new Knife;
WeaponStrategy* ak47 = new Ak47;
hero->setWeapon(knife);
hero->throwWeapon();
hero->setWeapon(ak47);
hero->throwWeapon();
delete knife;
delete ak47;
delete hero;
}
C++设计模式之命令模式 命令模式:将一个请求封装为一个对象,让我们可以用不同的请求对客户进行参数化。对请求排队或者记录请求日志,以及支持可撤销的操作。命令模式是一种对象行为模式,其别名为动作或事务模式。 命令模式可以将请求发送者和接收者完全解构,发送者和接受者之间没有直接引用关系,发送请求的对象只需要直到如何发送请求,而不必知道如何完成请求。
例子: 客户端(clinet)需要发送各种请求给服务端(server),server需要依次有序处理各种请求
class HandleProtocl {
public:
void addMoney()
{
cout << "添加金钱" << endl;
}
void addEquiment()
{
cout << "添加设备" << endl;
}
};
class AbstractCommand {
public:
virtual void handle() = 0;
};
class AddMoneyCommand : public AbstractCommand
{
public:
AddMoneyCommand(HandleProtocl* protocol)
{
this->protocol = protocol;
}
void handle()
{
this->protocol->addMoney();
}
private:
HandleProtocl* protocol;
};
class AddEquimentCommand : public AbstractCommand
{
public:
AddEquimentCommand(HandleProtocl* protocol)
{
this->protocol = protocol;
}
void handle()
{
this->protocol->addEquiment();
}
private:
HandleProtocl* protocol;
};
class Server {
public:
void addRequest(AbstractCommand* commond)
{
qCommand.push(commond);
}
void startHandle()
{
while (!qCommand.empty())
{
AbstractCommand* commond = qCommand.front();
commond->handle();
qCommand.pop();
}
}
private:
queue<AbstractCommand*> qCommand;
};
int main()
{
HandleProtocl* protocol = new HandleProtocl;
AbstractCommand* addMoney = new AddMoneyCommand(protocol);
AbstractCommand* addEquiment = new AddEquimentCommand(protocol);
Server* server = new Server;
server->addRequest(addMoney);
server->addRequest(addEquiment);
server->startHandle();
}
C++设计模式之观察者模式
背景: 交通灯和车辆的场景,当交通灯发生改变,汽车的行为也随之发生改变
观察者模式是用于建立一种对象与对象之间的依赖关系,一个对象发生改变时将自动通知其他对象,其他对象将相应作出反应。 在观察者模式中,发生改变的对象的称为观察目标,而被通知的对象称为观察者。一个观察目标可以对应多个观察者。
class AbstractHero {
public:
virtual void update() = 0;
};
class HeroA : public AbstractHero {
public:
HeroA()
{
cout << "英雄A正在打Boss..." << endl;
}
void update()
{
cout << "英雄A停止攻击..." << endl;
}
};
class HeroB : public AbstractHero {
public:
HeroB()
{
cout << "英雄B正在打Boss..." << endl;
}
void update()
{
cout << "英雄B停止攻击..." << endl;
}
};
class AbstractBoss {
public:
virtual void addHero(AbstractHero* hero) = 0;
virtual void delHero(AbstractHero* hero) = 0;
virtual void notify() = 0;
};
class BossA : public AbstractBoss {
public:
void addHero(AbstractHero* hero)
{
pHeroList.push_back(hero);
}
void delHero(AbstractHero* hero)
{
pHeroList.remove(hero);
}
void notify()
{
for (auto it = pHeroList.begin(); it != pHeroList.end(); it++)
{
(*it)->update();
}
}
private:
list<AbstractHero*> pHeroList;
};
void test01()
{
AbstractHero* heroA = new HeroA;
AbstractHero* heroB = new HeroB;
AbstractBoss* bossA = new BossA;
bossA->addHero(heroA);
bossA->addHero(heroB);
cout << "heroB阵亡" << endl;
bossA->delHero(heroB);
cout << "Boss死了, 通知其他英雄停止攻击" << endl;
bossA->notify();
}
C++设计模式之装饰者模式 装饰者模式又称包装模式,通过一种对客户端透明的方式来扩展对象功能,是对继承关系的一种替代。 装饰者模式就是把要附加的功能分别放在单独的类中,并让这个类包含他要装饰的对象,当需要执行时,客户端就可以有选择的、按顺序的使用装饰功能包装
应用场景: 一般情况下用继承实现类的功能扩展 装饰者模式可以动态给一个类增加功能
class AbstractMyHero {
public:
virtual void showStatus() = 0;
public:
int mHp = 10;
int dHp = 10;
};
class MyHero :public AbstractMyHero
{
public:
virtual void showStatus()
{
cout << "血量:" << this->mHp << endl;
cout << "攻击力:" << this->dHp << endl;
}
};
class AbstractEquipment : public AbstractMyHero {
public:
AbstractEquipment(AbstractMyHero* hero)
{
this->pHero = hero;
}
virtual void showStatus() {}
public:
AbstractMyHero* pHero;
};
class KuangtuEquipment : public AbstractEquipment {
public:
KuangtuEquipment(AbstractMyHero* hero) :AbstractEquipment(hero) {}
void AddKuangtu()
{
this->mHp = this->pHero->mHp + 30;
this->dHp = this->pHero->dHp;
}
virtual void showStatus()
{
AddKuangtu();
cout << "血量:" << this->mHp << endl;
cout << "攻击力:" << this->dHp << endl;
}
};
class WujinEquipment : public AbstractEquipment {
public:
WujinEquipment(AbstractMyHero* hero) :AbstractEquipment(hero) {}
void AddKuangtu()
{
this->mHp = this->pHero->mHp;
this->dHp = this->pHero->dHp + 20;
}
virtual void showStatus()
{
AddKuangtu();
cout << "血量:" << this->mHp << endl;
cout << "攻击力:" << this->dHp << endl;
}
};
int main()
{
AbstractMyHero* hero = new MyHero;
hero->showStatus();
AbstractEquipment* equipment = new KuangtuEquipment(hero);
equipment->showStatus();
delete equipment;
equipment = new WujinEquipment(hero);
equipment->showStatus();
}
|