Memento Design Pattern — 3 Minute Series

Undo. Undo. Undo.

Elric Edward
Oct 4, 2022
Photo by Fredy Jacob on Unsplash

_00 / Concept

Memento Design Pattern provides a solution to get and restore the previous step. This pattern cuts down the duty and lets each role handle the specific job. There are several variants for implementing this pattern. But the basic concept will be the same, one history owner, one to create a snapshot or restore the state from the snapshot, and the last is the snapshot itself.

_01 / Key Roles

  • CareTaker: The history holder.
  • Originator: Provides methods for creating and restoring Memento.
  • Memento: The snapshot.

_02 / Trade-offs

🟢 Provide a pattern so you can create a snapshot and return to the previous step.
🔴 Cost more memory.
🔴 You cannot guarantee the states in the dynamic language.

--

--