Files
design-patterns/03-behavioral/command/Main.java

41 lines
1.2 KiB
Java

package command;
/**
* Command Design Pattern — Runnable Demo
* Run: javac command/*.java -d out/command && java -cp out/command command.Main
* Article: https://ankurm.com/command-design-pattern-java/
*/
public class Main {
public static void main(String[] args) {
System.out.println("=== Command Design Pattern Demo ===\n");
TextEditor editor = new TextEditor();
CommandHistory history = new CommandHistory();
System.out.println("Initial: " + editor);
history.execute(new InsertCommand(editor, "Hello", 0));
System.out.println("After: " + editor);
history.execute(new InsertCommand(editor, " World", 5));
System.out.println("After: " + editor);
history.execute(new DeleteCommand(editor, 5, 6));
System.out.println("After: " + editor);
System.out.println("\n-- Undo sequence --");
history.undo();
System.out.println("After undo: " + editor);
history.undo();
System.out.println("After undo: " + editor);
history.undo();
System.out.println("After undo: " + editor);
history.undo(); // nothing left
System.out.println("\n=== Demo complete ===");
}
}