5 Memento (Behavioral) Design Pattern

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

  1. Originator: The object whose state you want to save and restore.
  2. Memento: Stores the internal state of the originator. It acts as a snapshot.
  3. Caretaker: Manages the memento. It requests the originator to save or restore its state.

When to Use

  1. When an object’s state needs to be saved and restored later.
  2. To provide undo/redo functionality in applications.
  3. When preserving encapsulation is critical.