-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathCommandHistory.java
More file actions
30 lines (25 loc) · 803 Bytes
/
CommandHistory.java
File metadata and controls
30 lines (25 loc) · 803 Bytes
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
package simplejavacalculator.behavioral.command;
import java.util.Stack;
public class CommandHistory {
private final Stack<CalculatorCommand> undoStack = new Stack<>();
private final Stack<CalculatorCommand> redoStack = new Stack<>();
public void executeCommand(CalculatorCommand command) {
command.execute();
undoStack.push(command);
redoStack.clear();
}
public void undo() {
if (!undoStack.isEmpty()) {
CalculatorCommand command = undoStack.pop();
command.undo();
redoStack.push(command);
}
}
public void redo() {
if (!redoStack.isEmpty()) {
CalculatorCommand command = redoStack.pop();
command.execute();
undoStack.push(command);
}
}
}