-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandPattern.cpp
175 lines (160 loc) · 4.15 KB
/
CommandPattern.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
//
// The classes and/or objects participating in this pattern are:
//
// 1. Command (Command)
// - declares an interface for executing an operation.
// 2. ConcreteCommand (CalculatorCommand)
// - defines a binding between a Receiver object and an action.
// - implements Execute by invoking the corresponding operation(s) on Receiver
// 3. Client (Calculator Application)
// - creates a ConcreteCommand object and sets its receiver.
// 4. Invoker (User)
// - asks the command to carry out the request
// 5. Receiver (Calculator)
// - knows how to perform the operations associated with carrying out
// a request. Any class may serve as a Receiver.
//
//
// "Command"
//
class EventHandler {
public:
virtual void Execute() = 0;
protected:
EventHandler(){};
};
//r
// "Receiver"
//
class MouseDriver {
public:
MouseDriver() {};
void Action(int x, int y) {
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
};
class KeyboardDriver {
public:
KeyboardDriver() {};
void Action(char x) {
switch (x) {
case 'a' :
cout << "a" << endl;
break;
case 's' :
cout << "s" << endl;
break;
case 'd' :
cout << "d" << endl;
break;
case 'f' :
cout << "f" << endl;
break;
case 'g' :
cout << "g" << endl;
break;
case 'h' :
cout << "h" << endl;
break;
case 'j' :
cout << "j" << endl;
break;
case 'k' :
cout << "k" << endl;
break;
case 'l' :
cout << "l" << endl;
break;
case 'q' :
cout << "q" << endl;
break;
case 'w' :
cout << "w" << endl;
break;
case 'e' :
cout << "e" << endl;
break;
case 'r' :
cout << "r" << endl;
break;
case 't' :
cout << "t" << endl;
break;
case 'y' :
cout << "y" << endl;
break;
case 'u' :
cout << "u" << endl;
break;
default:
cout << "UNDEFINED INPUT" << endl;
break;
}
}
};
//
// "ConcreteCommand"
//
class MouseEventHandler : public EventHandler {
public:
// Constructor
MouseEventHandler(MouseDriver *mouseDriver, int x, int y)
: _mouseDriver(mouseDriver), _x(x), _y(y) {}
void Execute() {
_mouseDriver->Action(_x, _y);
}
private:
MouseDriver* _mouseDriver;
int _x;
int _y;
};
class KeyboardEventHandler : public EventHandler {
public:
// Constructor
KeyboardEventHandler(KeyboardDriver *keyboardDriver, char x)
: _keyboardDriver(keyboardDriver), _x(x){}
void Execute() {
_keyboardDriver->Action(_x);
}
private:
KeyboardDriver* _keyboardDriver;
char _x;
};
// "Invoker"
class User {
public:
User() {};
void Compute(EventHandler *eventHandler){
eventHandler->Execute();
}
private:
MouseDriver *_mouseDriver;
KeyboardDriver *_keyboardDriver;
vector<EventHandler *> *_eventHandlers;
};
int main() {
// Create user and let her compute
EventHandler *eventHandler = nullptr;
auto *user = new User();
auto *mouseDriver = new MouseDriver();
auto *keyboardDriver = new KeyboardDriver();
eventHandler = new MouseEventHandler(mouseDriver, 20, 50);
user->Compute(eventHandler);
eventHandler = new MouseEventHandler(mouseDriver, 50, 200);
user->Compute(eventHandler);
eventHandler = new MouseEventHandler(mouseDriver, 20, 300);
user->Compute(eventHandler);
eventHandler = new KeyboardEventHandler(keyboardDriver, 'a');
user->Compute(eventHandler);
/*
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
*/
}