Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
38
03-behavioral/memento/Main.java
Normal file
38
03-behavioral/memento/Main.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package memento;
|
||||
|
||||
/**
|
||||
* Memento Design Pattern — Runnable Demo
|
||||
* Run: javac memento/*.java -d out/memento && java -cp out/memento memento.Main
|
||||
* Article: https://ankurm.com/memento-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Memento Design Pattern Demo ===\n");
|
||||
|
||||
Editor editor = new Editor();
|
||||
History history = new History();
|
||||
|
||||
System.out.println("Initial: " + editor);
|
||||
|
||||
editor.type("Hello");
|
||||
history.save(editor.save());
|
||||
System.out.println("After type: " + editor);
|
||||
|
||||
editor.type(", World");
|
||||
history.save(editor.save());
|
||||
System.out.println("After type: " + editor);
|
||||
|
||||
editor.type("! How are you?");
|
||||
System.out.println("After type: " + editor);
|
||||
|
||||
System.out.println("\n-- Undo --");
|
||||
editor.restore(history.undo());
|
||||
System.out.println("After undo: " + editor);
|
||||
|
||||
editor.restore(history.undo());
|
||||
System.out.println("After undo: " + editor);
|
||||
|
||||
System.out.println("\nHistory empty: " + (history.size() == 0));
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user