Purpose Recap
The Memento Pattern lets an object save and restore its state without showing its internal data, so encapsulation (private data) is kept.
This suits features like Undo / Redo in text editors, games, and drawing tools.
Class Responsibilities (Overview)
1. TextEditor (Originator)
- The object whose state is saved.
- Knows how to create a snapshot (
save()) and how to restore one (restore()). - Does not know who stores the snapshots.
2. EditorMemento (Memento)
- Holds a snapshot of the editor’s state (here, the text content).
- Immutable: once created, it cannot be changed (its
contentfield isfinal). - Has a getter,
getContent(), so theTextEditorcan read the snapshot when it restores.
3. Caretaker
- Keeps the history, a stack of mementos (last in, first out).
- Handles saving and restoring states.
- Does not change or read the internal data of
TextEditororEditorMemento. - Acts as the "history manager" for undo/redo.
4. TextEditorMain
- Shows how to use the pattern.
- Creates the editor and caretaker, saves states, changes the content, and undoes a change.
How It Works (Step-by-Step Flow)
- Initial State Creation
editor.write("Hello World"); caretaker.saveState(editor);
- The editor writes some text.
saveState(editor)callseditor.save(), which creates a newEditorMementocontaining"Hello World".- The caretaker pushes this snapshot onto its
historystack.
-
State Change
editor.write("Hello Everyone"); caretaker.saveState(editor);- The editor writes new content.
- The caretaker saves this new state on the stack too. The stack now holds both snapshots.
-
Undo Operation
caretaker.undo(editor);- The caretaker pops the most recent snapshot (
"Hello Everyone") off the stack. That is the current state, so it is dropped. - Then it calls
editor.restore(history.peek())with the snapshot now on top ("Hello World").peek()reads the top without removing it. - Limit: if only one state is saved,
pop()empties the stack andpeek()throwsEmptyStackException. The code only checks that the stack is not empty before thepop().
- The caretaker pops the most recent snapshot (
-
Final Output
System.out.println(editor.getContent());- Prints
"Hello World". The undo reverted the last change.
- Prints
How This Fixes the Old Code
| Problem in Old Code | How Good Code Fixes It |
|---|---|
| No way to store previous states | The EditorMemento class now stores snapshots of each state. |
TextEditor risked exposing internal data | Snapshots live inside EditorMemento, so the editor's data stays private. |
| Undo/Redo logic was missing | The Caretaker keeps the history in a stack and provides undo. |
| Violated SRP (Single Responsibility) | TextEditor only edits text. Caretaker handles history. Each class has one job. |
Key Advantages of This Design
-
Encapsulation is preserved
- The internal state of
TextEditorstays hidden. Only the memento holds a copy.
- The internal state of
-
Easy to extend
- Adding redo or multi-level undo is straightforward.
-
SRP compliance
- Each class has one concern: editing, storing a snapshot, or managing history.
-
Reusability
- The same
Caretakeridea can be used for other objects that need history.
- The same
Summary
The improved code applies the Memento Pattern by giving each of three components its own job:
TextEditor→ OriginatorEditorMemento→ Memento (state holder)Caretaker→ State manager (undo/redo handler)
This design gives you undo while keeping the internal state private. The code is also easy to maintain and extend. This fixes the problems in the old code.