-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbehavioral-command.php
228 lines (199 loc) · 5.99 KB
/
behavioral-command.php
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* This example just to simulate how Command can be work
* 2nees.com
*/
/**
* Interface TVControlCommand - This is the Command Interface which its contain all common command method
* 2nees.com
*/
interface TVControlCommand {
public function execute();
}
/**
* Class TVBaseCommand - This is optional abstract class, I used it since this declaration common for all Concrete Command Class
*/
abstract class TVBaseCommand implements TVControlCommand {
protected TVControl $control;
/**
* TVTurnOnControlCommand constructor.
* @param TVControl $control
*/
public function __construct(TVControl $control)
{
$this->control = $control;
}
}
#region Concrete Command Classes which its used to execute specific work
class TVTurnOnControlCommand extends TVBaseCommand {
public function execute()
{
if($this->control->isTvTurnStatus()){
return;
}
$this->control->turnOn();
}
}
class TVTurnOffControlCommand extends TVBaseCommand {
public function execute()
{
if(!$this->control->isTvTurnStatus()){
return;
}
$this->control->turnOff();
}
}
class TVVolumeUpControlCommand extends TVBaseCommand {
public function execute()
{
if(!$this->control->isTvTurnStatus()){
echo "TV is OFF!, Turn TV ON to change volume UP!" . PHP_EOL;
return;
}
$this->control->increaseVolume();
echo "Your Volume: {$this->control->getVolume()}" . PHP_EOL;
}
}
class TVVolumeDownControlCommand extends TVBaseCommand {
public function execute()
{
if(!$this->control->isTvTurnStatus()){
echo "TV is OFF!, Turn TV ON to change volume Down!" . PHP_EOL;
return;
}
$this->control->decreaseVolume();
echo "Your Volume: {$this->control->getVolume()}" . PHP_EOL;
}
}
#endregion
/**
* Class TVControl - Receiver Class, this class represent the real place for doing actual work
*/
class TVControl {
private int $volume;
private bool $tvTurnStatus;
/**
* TVControl constructor.
* @param int $volume
* @param bool $tvTurnStatus
*/
public function __construct(int $volume, bool $tvTurnStatus)
{
$this->volume = $volume;
$this->tvTurnStatus = $tvTurnStatus;
}
public function turnOff(): bool {
$this->tvTurnStatus = false;
echo "TV is turned OFF" . PHP_EOL;
return $this->tvTurnStatus;
}
public function turnOn(): bool {
$this->tvTurnStatus = true;
echo "TV is turned ON" . PHP_EOL;
return $this->tvTurnStatus;
}
public function decreaseVolume(): void
{
$volume = $this->volume - 1;
if($volume >= 0){
$this->volume = $volume;
}
}
public function increaseVolume(): void
{
$volume = $this->volume + 1;
if($volume <= 7){
$this->volume = $volume;
}
}
public function isTvTurnStatus(): bool
{
return $this->tvTurnStatus;
}
public function getVolume(): int
{
return $this->volume;
}
}
/**
* Class Application - Sender (Invoker) - This class for init request,
* its can control more than one command, its trigger a command nested of send it directly
*
* Note: Maybe in our case here, is not needed, but usually its needed for keep history or collaborate and share it via component and so on...
*/
class Application {
private TVTurnOffControlCommand $turnOffControlCommand;
private TVTurnOnControlCommand $turnOnControlCommand;
private TVVolumeUpControlCommand $volumeUpControlCommand;
private TVVolumeDownControlCommand $volumeDownControlCommand;
/**
* Application constructor.
* @param TVTurnOffControlCommand $turnOffControlCommand
* @param TVTurnOnControlCommand $turnOnControlCommand
* @param TVVolumeUpControlCommand $volumeUpControlCommand
* @param TVVolumeDownControlCommand $volumeDownControlCommand
*/
public function __construct(
TVTurnOffControlCommand $turnOffControlCommand,
TVTurnOnControlCommand $turnOnControlCommand,
TVVolumeUpControlCommand $volumeUpControlCommand,
TVVolumeDownControlCommand $volumeDownControlCommand
) {
$this->turnOffControlCommand = $turnOffControlCommand;
$this->turnOnControlCommand = $turnOnControlCommand;
$this->volumeUpControlCommand = $volumeUpControlCommand;
$this->volumeDownControlCommand = $volumeDownControlCommand;
}
public function on() {
$this->turnOnControlCommand->execute();
}
public function off() {
$this->turnOffControlCommand->execute();
}
public function up() {
$this->volumeUpControlCommand->execute();
}
public function down() {
$this->volumeDownControlCommand->execute();
}
public function doSomethingElseViaTheseCommand() {
$this->on();
$this->up();
$this->up();
$this->up();
}
// You can build history object to save history of execute to do some action....
}
// Client
$tv = new TVControl(2, true);
$app = new Application(
new TVTurnOffControlCommand($tv),
new TVTurnOnControlCommand($tv),
new TVVolumeUpControlCommand($tv),
new TVVolumeDownControlCommand($tv)
);
/**
* This loop to simulate we are press on remote or TV internal button, we share invoker as you see and can we use and put it any where...
*/
for ($i = 0; $i < 10; $i++){
switch (rand(1, 8)):
case 1:
case 2:
$app->on();
break;
case 3:
case 4:
$app->off();
break;
case 5:
case 6:
$app->up();
break;
case 7:
case 8:
$app->down();
break;
endswitch;
}
echo "==================================================" . PHP_EOL;
$app->doSomethingElseViaTheseCommand();