/*
@作者:莫忘输赢
@时间:
2020/02/22 22:56
@版本:v1
@命令模式
@实现:
1建立命令队列
2将命令写入日志
3接收的一方可以拒绝
4添加新的命令不影响其他类
*/
#include<iostream>
#include<string>
#include<vector>
//using namespace std;
//#include <vld.h>
//烤肉师傅
class Barbucer
{
public:
void MakeMutton()
{
std::cout << "烤羊肉串" << std::endl;
}
void MakeChickenWing()
{
std::cout << "烤鸡翅" << std::endl;
}
};
//抽象命令类 设置命令和执行命令
class Command
{
protected:
Barbucer* receiver;
public:
Command(Barbucer* temp)
{
receiver = temp;
}
virtual void ExecuteCmd() = 0;
};
//烤羊肉串命令
class BakeMuttonCmd : public Command
{
public:
BakeMuttonCmd(Barbucer *temp) :Command(temp){ }
virtual void ExecuteCmd()
{
receiver->MakeMutton();
}
};
//考鸡翅命令
class BakeChickenWingCmd : public Command
{
public:
BakeChickenWingCmd(Barbucer *temp) :Command(temp){ }
virtual void ExecuteCmd()
{
receiver->MakeChickenWing();
}
};
//服务员类
class Waiter
{
protected:
std::vector<Command* > m_CmdList;
public:
void SetCmd(Command *temp)
{
m_CmdList.push_back(temp);
std::cout << "增加订单" << std::endl;
}
//
void Notify()
{
std::vector<Command*> ::iterator p = m_CmdList.begin();
while (p != m_CmdList.end())
{
(*p)->ExecuteCmd();
p++;
}
}
};
//客户端
int main(int argc, char **argv)
{
//店里添加烤肉师傅 菜单 服务员 顾客
Barbucer* barbucer = new Barbucer();
Command* cmd1 = new BakeMuttonCmd(barbucer);
Command* cmd2 = new BakeChickenWingCmd(barbucer);
Waiter* wter = new Waiter();
//点菜
wter->SetCmd(cmd1);
wter->SetCmd(cmd2);
//服务员通知
wter->Notify();
//释放内存
delete wter;
delete cmd2;
delete cmd1;
delete barbucer;
return 0;
}