-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
executable file
·108 lines (73 loc) · 2.34 KB
/
Copy pathcommand.cpp
File metadata and controls
executable file
·108 lines (73 loc) · 2.34 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
/*
how intelligent should a command be? A command can have a wide range of abilities.At one extreme it merely defines a binding between a receiver and the actions that carry out the request
at the other extreme it implements everything itself without delegating to a receiver at all. The last extreme is useful when you want to define command that are independent of existing
classes, when no suitable receiver exists, or when a command knows its receiver implicity. for example, a command that create another application window may be just are capable of creating
the window as any other object.
supporting undo and redo. Commands can support undo and redo capabilities, if they provide a way to reverse their execution, a ConcreateCommand class might need to store additional state to
do so.this state can include:
the Receiver object, which actually carries out operations in response to the request
the arguments to the operation performed on the receiver
any orignal values in the receiver that can change as a result of handling the request. the receiver must provide operations that let the command return the receiver to its prior state
using c++ template
*/
#include<iostream>
#include<vector>
using namespace std;
class Command
{
public:
virtual ~Command()
{
}
virtual void Execute() = 0;
protected:
Command()
{
}
};
template<class Receiver>
class SimpleCommand : public Command
{
public:
typedef void (Receiver::*Action)();
SimpleCommand(Receiver* r, Action a) : _receiver(r),_action(a)
{
}
virtual void Execute()
{
(_receiver->*_action)();
}
private:
Action _action;
Receiver* _receiver;
};
class MyClass
{
public:
void redo()
{
cout << "in myclass" << endl;
}
};
class YourClass
{
public:
void check()
{
cout << "check your class" << endl;
}
};
int main(int argc, char** argv)
{
MyClass* receiver = new MyClass;
Command* aCommand = new SimpleCommand<MyClass>(receiver,&MyClass::redo);
YourClass* youReceiver = new YourClass;
Command* yCommand = new SimpleCommand<YourClass>(youReceiver,&YourClass::check);
vector<Command*> commandQueue;
commandQueue.push_back(aCommand);
commandQueue.push_back(yCommand);
for(int i = 0 ; i < commandQueue.size(); i++)
{
commandQueue[i]->Execute();
}
}