File tree Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ abstract class HomeChecker
4
+ {
5
+ protected $ successor ;
6
+
7
+ public abstract function check (HomeStatus $ home );
8
+
9
+ public function successor (HomeChecker $ successor )
10
+ {
11
+ $ this ->successor = $ successor ;
12
+ }
13
+
14
+ public function next (HomeStatus $ home )
15
+ {
16
+ if ($ this ->successor ) {
17
+ $ this ->successor ->check ($ home );
18
+ }
19
+ }
20
+ }
21
+
22
+ class Locks extends HomeChecker
23
+ {
24
+ public function check (HomeStatus $ home )
25
+ {
26
+ if (!$ home ->locked ) {
27
+ echo "The doors are not locked \n" ;
28
+ }
29
+
30
+ $ this ->next ($ home );
31
+ }
32
+ }
33
+
34
+ class Lights extends HomeChecker
35
+ {
36
+
37
+ public function check (HomeStatus $ home )
38
+ {
39
+ if (!$ home ->lightOff ) {
40
+ echo "The lights are still on \n" ;
41
+ }
42
+
43
+ $ this ->next ($ home );
44
+ }
45
+ }
46
+
47
+ class Alarm extends HomeChecker
48
+ {
49
+
50
+ public function check (HomeStatus $ home )
51
+ {
52
+ if (!$ home ->alarmOn ) {
53
+ echo "The alarm has not been set \n" ;
54
+ }
55
+
56
+ $ this ->next ($ home );
57
+ }
58
+ }
59
+
60
+ class HomeStatus
61
+ {
62
+ public $ alarmOn = true ;
63
+ public $ locked = true ;
64
+ public $ lightOff = false ;
65
+ }
66
+
67
+ /*Setup chain ring*/
68
+ $ locks = new Locks ();
69
+ $ lights = new Lights ();
70
+ $ alarm = new Alarm ();
71
+
72
+ /*Hooked together*/
73
+ $ locks ->successor ($ lights );
74
+ $ lights ->successor ($ alarm );
75
+
76
+ /*Called first ring of the chain*/
77
+ $ locks ->check (new HomeStatus ());
You can’t perform that action at this time.
0 commit comments