@@ -137,7 +137,7 @@ _Design of the Remote Control_
137
137
138
138
First, create the ` Command.java ` interface. This defines what every Command must do:
139
139
140
- ``` java
140
+ ``` java title="Command.java"
141
141
interface Command {
142
142
public void execute ();
143
143
public void undo ();
@@ -146,7 +146,7 @@ interface Command {
146
146
147
147
Next, make the Receiver classes. A Receiver carries out the real work.
148
148
149
- ``` java
149
+ ``` java title="Light.java"
150
150
class Light {
151
151
public void off () {
152
152
System . out. println(" Light is Turned Off" );
@@ -158,7 +158,7 @@ class Light {
158
158
}
159
159
```
160
160
161
- ``` java
161
+ ``` java title="LivingRoom.java"
162
162
class LivingRoom {
163
163
public void off () {
164
164
System . out. println(" Living Room Light is turned off" );
@@ -178,7 +178,7 @@ class LivingRoom {
178
178
}
179
179
```
180
180
181
- ``` java
181
+ ``` java title="CeilingFan.java"
182
182
class CeilingFan {
183
183
public static final int OFF = 0 ;
184
184
public static final int HIGH = 3 ;
@@ -214,7 +214,7 @@ class CeilingFan {
214
214
215
215
Now write the concrete Command classes. Each one wraps a Receiver and calls its methods.
216
216
217
- ``` java
217
+ ``` java title"LightOnCommand.java"
218
218
class LightOnCommand implements Command {
219
219
private Light light;
220
220
@@ -239,7 +239,7 @@ class LightOnCommand implements Command {
239
239
}
240
240
```
241
241
242
- ``` java
242
+ ``` java title="LightOffCommand.java"
243
243
class LightOffCommand implements Command {
244
244
private Light light;
245
245
@@ -264,7 +264,7 @@ class LightOffCommand implements Command {
264
264
}
265
265
```
266
266
267
- ``` java
267
+ ``` java title="LivingRoomOnCommand.java"
268
268
class LivingRoomOnCommand implements Command {
269
269
private LivingRoom livingroom;
270
270
@@ -291,7 +291,7 @@ class LivingRoomOnCommand implements Command {
291
291
}
292
292
```
293
293
294
- ``` java
294
+ ``` java title="LivingRoomOffCommand.java"
295
295
class LivingRoomOffCommand implements Command {
296
296
private LivingRoom livingroom;
297
297
@@ -322,7 +322,7 @@ You would follow the same pattern for the CeilingFan commands.
322
322
323
323
Finally, put everything together in the Invoker:
324
324
325
- ``` java
325
+ ``` java title="RemoteControl.java"
326
326
class RemoteControl {
327
327
private Command [] onCommands;
328
328
private Command [] offCommands;
0 commit comments