-
Notifications
You must be signed in to change notification settings - Fork 10
/
Example6.java
157 lines (125 loc) · 3.81 KB
/
Example6.java
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
/*
7개의 소켓을 가진 만능 리모컨
- 각 소켓마다 on/off 버튼이 있음
- global undo 버튼이 하나 있음
- undo 히스토리 관리하여 여러번 실행취소가 가능
- redo 히스토리 관리하여 여러번 다시실행 가능
*/
interface Command {
void execute();
void undo();
void redo();
}
class NoCommand implements Command {
public void execute() {}
public void undo() {}
public void redo() {}
}
class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Deque<Command> undoStack = new LinkedList<>();
Deque<Command> redoStack = new LinkedList<>();
public RemoteControl() {
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
for (int i = 0; i < 7; 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();
undoStack.offerLast(onCommands[slot]);
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
undoStack.offerLast(offCommands[slot]);
}
public void undoButtonWasPushed() {
if (!undoStack.isEmpty()) {
Command prevCommand = undoStack.pollLast();
redoStack.offerLast(prevCommand);
prevCommand.undo();
}
}
public void redoButtonWasPushed() {
if (!redoStack.isEmpty()) {
Command nextCommand = redoStack.pollLast();
undoStack.offerLast(nextCommand);
nextCommand.redo();
}
}
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");
}
stringBuff.append("[undo] " + undoStack.getClass().getName()); // added
return stringBuff.toString();
}
}
class Light {
String location;
public Light(String location) {
this.location = location;
}
public void on() {
System.out.println(location + " light on");
}
public void off() {
System.out.println(location + " light off");
}
}
class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.on();
}
public void undo() {
light.off();
}
public void redo() {
light.on();
}
}
class LightOffCommand implements Command {
Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.off();
}
public void undo() {
light.on();
}
public void redo() {
light.off();
}
}
public class Example6 {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl();
Light livingRoomLight = new Light("Living Room");
LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.undoButtonWasPushed();
remoteControl.undoButtonWasPushed();
remoteControl.redoButtonWasPushed();
remoteControl.redoButtonWasPushed();
}
}