-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
69 lines (59 loc) · 1.13 KB
/
command.cpp
File metadata and controls
69 lines (59 loc) · 1.13 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
//对发送者和接收者完全解耦,发送者与接收者之间没有直接引用关系
// 发送请求的对象只需要知道如何发送请求,而不必知道如何完成请求
#include <vector>
#include <string>
#include <iostream>
class Command
{
public:
virtual void Execute() = 0;
};
class ConcreteCommand1 : public Command
{
std::string arg;
public:
ConcreteCommand1(const std::string& a) : arg(a)
{
}
void Execute() override
{
std::cout << "#1 process " << arg << std::endl;
}
};
class ConcreteCommand2 : public Command
{
std::string arg;
public:
ConcreteCommand2(const std::string& a) : arg(a)
{
}
void Execute() override
{
std::cout << "#2 process " << arg << std::endl;
}
};
class MacroCommand : public Command
{
std::vector<Command*> commands;
public:
void AddCommand(Command* c)
{
commands.emplace_back(c);
}
void Execute() override
{
for (const auto& c : commands)
{
c->Execute();
}
}
};
int main()
{
ConcreteCommand1 command1("Arg ###");
ConcreteCommand2 command2("Arg ###");
MacroCommand macro;
macro.AddCommand(&command1);
macro.AddCommand(&command2);
macro.Execute();
}