Memento: sync post code blocks to repo exactly (add missing Editor methods), Java 25, H6 file labels, fix stale console output, README mapping
This commit is contained in:
31
03-behavioral/memento/README.md
Normal file
31
03-behavioral/memento/README.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Memento Design Pattern — Java Example
|
||||||
|
|
||||||
|
**Pattern:** Behavioral → Memento
|
||||||
|
**Article:** https://ankurm.com/memento-design-pattern-java/
|
||||||
|
|
||||||
|
## What this example shows
|
||||||
|
|
||||||
|
A text editor with undo support that doesn't leak its internal state. `EditorMemento` is an immutable snapshot with package-private accessors — only `Editor` (in the same package) can read it back; everyone else can only call `toString()`. `Editor` is the originator: it packs its fields into a memento on `save()` and unpacks them on `restore()`. `History` is the caretaker: it pushes and pops mementos on a stack without ever inspecting their contents. `Main` types text, saves checkpoints after most edits (deliberately skipping one, to show it's lost on undo), then undoes twice.
|
||||||
|
|
||||||
|
## How to run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
javac memento/*.java -d out/memento
|
||||||
|
java -cp out/memento memento.Main
|
||||||
|
```
|
||||||
|
|
||||||
|
Requires Java 25.
|
||||||
|
|
||||||
|
## Post Section ↔ File Mapping
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| The Memento: Immutable and Opaque | `EditorMemento.java` |
|
||||||
|
| The Originator (Editor) | `Editor.java` |
|
||||||
|
| The Caretaker (History) | `History.java` |
|
||||||
|
| Wiring it together (Main demo) | `Main.java` |
|
||||||
|
|
||||||
|
Note: the "Serialization as Memento" snippet (`ObjectOutputStream`/`ObjectInputStream` example) is illustrative only — it is not part of this repository's runnable example.
|
||||||
|
|
||||||
|
Article: https://ankurm.com/memento-design-pattern-java/
|
||||||
|
All patterns: https://ankurm.com/design-patterns-java/
|
||||||
17
README.md
17
README.md
@@ -372,6 +372,23 @@ javac 03-behavioral/mediator/*.java -d out/mediator
|
|||||||
java -cp out/mediator mediator.Main
|
java -cp out/mediator mediator.Main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Memento (`03-behavioral/memento/`)
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| The Memento: Immutable and Opaque | `EditorMemento.java` |
|
||||||
|
| The Originator (Editor) | `Editor.java` |
|
||||||
|
| The Caretaker (History) | `History.java` |
|
||||||
|
| Wiring it together (Main demo) | `Main.java` |
|
||||||
|
|
||||||
|
Note: the "Serialization as Memento" snippet (`ObjectOutputStream`/`ObjectInputStream` example) is illustrative only — it is not part of this repository's runnable example.
|
||||||
|
|
||||||
|
Run it:
|
||||||
|
```bash
|
||||||
|
javac 03-behavioral/memento/*.java -d out/memento
|
||||||
|
java -cp out/memento memento.Main
|
||||||
|
```
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
|
||||||
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
||||||
|
|||||||
Reference in New Issue
Block a user