Memento: Introduction

Get a brief introduction to the Memento design pattern.

The Memento design pattern is used specifically for storing intermediate states of some data so that the actions can be undone and the state can be reverted.

Summarized concept of the Memento design pattern

The Memento design pattern can be summarized as follows:

  • There’s an Originator object, which acts as the origin of the data that can change its state at will.
  • There’s a Memento object representing a single data snapshot.
  • Originator object can generate a new instance of Memento when some action is performed or accept an instance of Memento to reset its data.
  • There’s also a Caretaker object, which stores a collection of the Memento objects in a stack. This stack represents the history of changes.
  • The Caretaker object also coordinates the history of changes with the Originator object and allows the consumer to undo the actions.

Get hands-on with 1400+ tech skills courses.