Skip to main content

Memento Pattern

Learn Memento Pattern (Behavioral Patterns, Design Patterns in Java) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

What is Memento Pattern? Memento Pattern captures and externalizes an object's internal state so it can be restored later - without violating encapsulation (no other object ever sees the fields being snapshotted directly). Real-Life Example: • A text editor's Undo feature - each save point remembers the document's exact content. • A video game's checkpoint/save system - restoring a saved game returns the world to that exact state. Why do we need Memento? • To implement undo/redo without exposing an object's private internals to whoever manages the history. • To let an object's state be saved...

More lessons in Behavioral Patterns