Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
28
03-behavioral/command/CommandHistory.java
Normal file
28
03-behavioral/command/CommandHistory.java
Normal file
@@ -0,0 +1,28 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user