-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteControl.java
More file actions
66 lines (61 loc) · 1.64 KB
/
RemoteControl.java
File metadata and controls
66 lines (61 loc) · 1.64 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
60
61
62
63
64
65
66
package designPattern.headfirst.chapter_06Command.macro;
import designPattern.headfirst.chapter_06Command.macro.command.Command;
import designPattern.headfirst.chapter_06Command.macro.command.NoCommand;
/**
* 遥控器
*
* @author ken
*
* @date 2017年7月12日 上午9:51:20
*/
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command unCommand;
public RemoteControl(int num) {
onCommands = new Command[num];
offCommands = new Command[num];
Command noCommand = new NoCommand();
for (int i = 0; i < num; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
unCommand=noCommand;
}
/**
*
* @param slot 开关的位置
* @param onCommand 开启命令
* @param offCommand 关闭命令
* @date 2017年7月12日 上午10:09:07
*/
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
unCommand=onCommands[slot];
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
unCommand=offCommands[slot];
}
public void undoButtonWasPushed(){
unCommand.undo();
}
public String toString(){
StringBuffer sb=new StringBuffer();
sb.append("____RemoteControl__\n");
for(int i=0;i<onCommands.length;i++){
sb.append("[slot");
sb.append(i);
sb.append("]");
sb.append(onCommands[i].getClass().getSimpleName());
sb.append(" ");
sb.append(offCommands[i].getClass().getSimpleName());
sb.append("\n");
}
return sb.toString();
}
}