29 lines
750 B
Java
29 lines
750 B
Java
package command;
|
|
|
|
import java.util.ArrayDeque;
|
|
import java.util.Deque;
|
|
|
|
/**
|
|
* Invoker — holds command history and triggers execute/undo.
|
|
* It doesn't know what the commands do; it just calls execute() and undo().
|
|
*/
|
|
public class CommandHistory {
|
|
private final Deque<Command> history = new ArrayDeque<>();
|
|
|
|
public void execute(Command cmd) {
|
|
cmd.execute();
|
|
history.push(cmd);
|
|
System.out.println(" Executed: " + cmd.getDescription());
|
|
}
|
|
|
|
public void undo() {
|
|
if (history.isEmpty()) {
|
|
System.out.println(" Nothing to undo.");
|
|
return;
|
|
}
|
|
Command cmd = history.pop();
|
|
cmd.undo();
|
|
System.out.println(" Undone: " + cmd.getDescription());
|
|
}
|
|
}
|