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