-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand mdoe.cpp
More file actions
112 lines (105 loc) · 1.93 KB
/
Copy pathcommand mdoe.cpp
File metadata and controls
112 lines (105 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//烤肉师傅
class Barbucer
{
public:
void MakeMutton()
{
cout<<"烤羊肉"<<endl;
}
void MakeChickenWing()
{
cout<<"烤鸡翅膀"<<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 ChickenWingCmd : public Command
{
public:
ChickenWingCmd(Barbucer* temp) : Command(temp){}
virtual void ExecuteCmd()
{
receiver->MakeChickenWing();
}
};
//服务员类
class Waiter
{
protected:
vector<Command*> m_commandList;
public:
void SetCmd(Command* temp)
{
m_commandList.push_back(temp);
cout<<"增加定单"<<endl;
}
void DelCmd(Command* temp)
{
vector<Command*>::iterator p=m_commandList.begin();
while(p!=m_commandList.end())// p是索引器,指向命令队列指针
{
if((*p) == temp)
{
m_commandList.erase(p);
cout<<"移除定单"<<endl;
break;
}
p++;
}
}
//通知执行
void Notify()
{
vector<Command*>::iterator p=m_commandList.begin();
while(p!=m_commandList.end())
{
(*p)->ExecuteCmd();
p++;
}
}
};
//客户端
int main()
{
//店里添加烤肉师傅、菜单、服务员等顾客
Barbucer* barbucer=new Barbucer();
Command* cmd= new BakeMuttonCmd(barbucer);
Command* cmd2=new ChickenWingCmd(barbucer);
Command* cmd3=new ChickenWingCmd(barbucer);
Waiter* girl = new Waiter();
//点菜
girl->SetCmd(cmd);
girl->SetCmd(cmd2);
girl->SetCmd(cmd3);
//服务员通知
girl->Notify();
girl->DelCmd(cmd3);
girl->Notify();
system("pause");
return 0;
}