-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightCommand.cpp
79 lines (65 loc) · 1.38 KB
/
LightCommand.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
/* @author: mirkancal */
#include <iostream>
#include <vector>
using namespace std;
class Light {
public:
void on(){
cout<<"Light's on!"<<endl;
}
void off(){
cout<<"Light's off!"<<endl;
}
Light(){};
};
class Command {
public:
virtual void Execute() = 0;
protected:
Command(){};
};
class LightOnCommand : public Command {
Light *light;
public:
LightOnCommand(Light *l) {
this->light = l;
};
void Execute() {
this->light->on();
}
};
class LightOffCommand : public Command {
Light* light;
public:
LightOffCommand(Light* l){
this->light = l;
};
void Execute(){
this->light->off();
}
};
class Invoker {
Command* on;
Command* off;
public:
Invoker(Command* on, Command* off){
this->on=on;
this->off=off;
};
void clickOn(){
this->on->Execute();
}
void clickOff(){
this->off->Execute();
}
};
int main() {
Light *philips = new Light();
Command *command1 = nullptr;
Command *command2 = nullptr;
command1 = new LightOnCommand(philips);
command2 = new LightOffCommand(philips);
Invoker *invoker = new Invoker(command1, command2);
invoker->clickOn();
invoker->clickOff();
}