-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
113 lines (90 loc) · 2.03 KB
/
main.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
/**
* The Chain of Responsibility Pattern.
*
* Gives the ability to chain object calls together.
*
* Allows for the client to make a request without having to know how that request is going to be handled.
*
* To Test: Toggle the booleans in the HomeStatus class from true to false. If any or all of the
* boolean values are set to false, a message will be displayed.
*
*
* User: Rich
* Date: 6/6/2015
* Time: 12:40 AM
*/
#include <cstdio>
using namespace std;
class HomeStatus {
public:
bool alarmOn = false;
bool locked = true;
bool lightsOff = true;
};
class HomeChecker {
protected:
HomeChecker *successor;
public:
virtual void check(HomeStatus *home) = 0;
void succeedWith(HomeChecker *successor)
{
this->successor = successor;
}
void next(HomeStatus *home)
{
if (this->successor)
{
this->successor->check(home);
}
}
};
class Locks : public HomeChecker {
public:
void check(HomeStatus *home)
{
if (!home->locked)
{
printf("The doors are not locked!! Abort abort.\n");
}
this->next(home);
}
};
class Lights : public HomeChecker {
public:
void check(HomeStatus *home)
{
if (!home->lightsOff)
{
printf("The lights are still on!! Abort abort.\n");
}
this->next(home);
}
};
class Alarm : public HomeChecker {
public:
void check(HomeStatus *home)
{
if (!home->alarmOn)
{
printf("The alarm has not been set!! Abort abort.\n");
}
this->next(home);
}
};
int main()
{
// create new instances
Locks *locks = new Locks();
Lights *lights = new Lights();
Alarm *alarm = new Alarm();
// set the successor chain
locks->succeedWith(lights);
lights->succeedWith(alarm);
// create HomeStatus instance
HomeStatus *home = new HomeStatus();
printf("Begin check.\n");
// start the chain
locks->check(home);
printf("End check.\n");
return 0;
}