-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommand.cpp
More file actions
88 lines (75 loc) · 1.77 KB
/
Copy pathcommand.cpp
File metadata and controls
88 lines (75 loc) · 1.77 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
/*!
* \brief Command.Encapsulate a request as an object,thereby
* letting you parameterize clients with different requests,
* queue or log requests,and support undoable operations.
*
* Invoker => ( Command => Receiver )
* |
* ConcreteCommand
*/
#include <iostream>
#include <vector>
// Receiver.
class Engineer {
public:
void Coding() {
std::cout << "Coding." << std::endl;
}
void Meeting() {
std::cout << "Meeting." << std::endl;
}
};
// Command.
class Command {
public:
Command() {}
virtual void ExecuteCommand() = 0;
protected:
Engineer *receiver_;
};
class CodingCommand :public Command {
public:
CodingCommand(Engineer *receiver) { receiver_ = receiver; }
void ExecuteCommand() {
receiver_->Coding();
}
};
class MeetingCommand :public Command {
public:
MeetingCommand(Engineer *receiver) { receiver_ = receiver; }
void ExecuteCommand() {
receiver_->Meeting();
}
};
// Invoker.
class Manager {
public:
Manager() {
command_list_.clear();
}
void SetCommand(Command *command) {
command_list_.push_back(command);
}
void Action() {
for (unsigned int i = 0; i < command_list_.size(); i++) {
command_list_[i]->ExecuteCommand();
}
}
private:
std::vector<Command *> command_list_;
};
int main(int argc, char *argv[]) {
Manager *manager = new Manager();
Engineer *engineer = new Engineer();
Command *command_one = new MeetingCommand(engineer);
Command *command_two = new CodingCommand(engineer);
manager->SetCommand(command_one);
manager->SetCommand(command_two);
// Engineer is going to excute those commands here.
manager->Action();
delete manager;
delete engineer;
delete command_one;
delete command_two;
return 0;
}