39 lines
1.2 KiB
Java
39 lines
1.2 KiB
Java
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 ===");
|
|
}
|
|
}
|