The Memento Design Pattern is a behavioral design pattern that allows you to capture and restore the state of an object without exposing its internal details. It is commonly used to implement undo/redo functionality
Intent
- Save the state of an object so it can be restored later without violating encapsulation.
- Allow rolling back to a previous state when necessary.
Key Components
- Originator: The object whose state you want to save and restore.
- Memento: Stores the internal state of the originator. It acts as a snapshot.
- Caretaker: Manages the memento. It requests the originator to save or restore its state.
When to Use
- When an object’s state needs to be saved and restored later.
- To provide undo/redo functionality in applications.
- When preserving encapsulation is critical.