-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteControl.java
More file actions
59 lines (50 loc) · 1.44 KB
/
RemoteControl.java
File metadata and controls
59 lines (50 loc) · 1.44 KB
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
import java.util.ArrayList;
import java.util.List;
//package headfirst.designpatterns.command.remote;
//
// This is the invoker
//
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
List<Command> commandLog;
int undo = 0;
public RemoteControl() {
onCommands = new Command[8];
offCommands = new Command[8];
commandLog = new ArrayList<>();
Command noCommand = new NoCommand();
commandLog.add(noCommand);
for (int i = 0; i < 8; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
}
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
commandLog.add(onCommands[slot]);
undo = commandLog.size() - 1;
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
commandLog.add(offCommands[slot]);
undo = commandLog.size() - 1;
}
public void undoButtonWasPushed(){
commandLog.get(undo).undo();
undo--;
}
public String toString() {
StringBuffer stringBuff = new StringBuffer();
stringBuff.append("\n------ Remote Control -------\n");
for (int i = 0; i < onCommands.length; i++) {
stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + "\n");
}
return stringBuff.toString();
}
}