Memento
Memento captures an originator’s state so it can be restored without exposing the internal representation to the caretaker. The saved state must not change when the live object changes.
What you’ll understand
- Memento captures an originator’s state so it can be restored without exposing the internal representation to the caretaker. The saved state must not change when the live object changes.
- The editor copies its owned paragraph collection into a memento and restores from that copy.
- Evaluate the tradeoff: Full snapshots can be expensive. Large histories may use deltas or bounded checkpoints, but their ownership and restoration contracts must remain clear.
Useful first: Classes and Objects
The idea and when to recognize it
Memento captures an originator’s state so it can be restored without exposing the internal representation to the caretaker. The saved state must not change when the live object changes.
- Requirement: An editor saves a checkpoint before appending a paragraph.
- Change to anticipate: Restore the saved paragraphs after editing without making the checkpoint follow later mutations.
- Design goal: The editor copies its owned paragraph collection into a memento and restores from that copy.
Start with the concrete problem
An editor saves a checkpoint before appending a paragraph.
Restore the saved paragraphs after editing without making the checkpoint follow later mutations.
| Direct implementation | Revised design |
|---|---|
| Saving a reference to the live paragraph array is not a snapshot. | The editor copies its owned paragraph collection into a memento and restores from that copy. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | Full snapshots can be expensive. Large histories may use deltas or bounded checkpoints, but their ownership and restoration contracts must remain clear. |
Responsibilities and contracts
The editor copies its owned paragraph collection into a memento and restores from that copy.
- Editor: Creates and restores snapshots. Public operations: save(), restore(snapshot)
- Memento: Owns saved values. Public operations: paragraphs
- History: Keeps opaque checkpoints. Public operations: push(snapshot)
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A game can capture a local checkpoint of inventory and position before an experimental move.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
Full snapshots can be expensive. Large histories may use deltas or bounded checkpoints, but their ownership and restoration contracts must remain clear.
Common mistake: Saving a reference to the live paragraph array is not a snapshot.
- Explain the choice: The editor copies its owned paragraph collection into a memento and restores from that copy.
- Challenge your choice: Full snapshots can be expensive. Large histories may use deltas or bounded checkpoints, but their ownership and restoration contracts must remain clear.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…