The Memento Design Pattern

The Memento pattern provides a way to capture an object’s internal state so that it can be restored later.

Use the Memento pattern when you want to capture and externalize an object’s internal state without violating encapsulation, so the object can be restored to this state later.

  • TypeBehavioral
  • Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
  • Efficiency4
  • Learning Effort

    • Average4
    • Conceptual6
    • Debug and Maintain4
    • Implementation4
    • Prerequisites5
    • Versatility3

Things you should know before you start

  • Encapsulation Understanding of encapsulation is crucial, as the pattern aims to capture an object’s internal state without violating its encapsulation.

Minimal Example

Implementing an undo feature in a text editor to revert text changes.

class TextEditorMemento {
  constructor(private state: string) {}
  getState(): string { return this.state; }
}
const editor = new TextEditor();
editor.type('First sentence.');
const saved = editor.save();
editor.type('Second sentence.');
editor.restore(saved);

What’s happening in thie example

In this example, the `TextEditorMemento` serves as a Memento, storing a snapshot of the `TextEditor`’s state. The `clientCode` demonstrates how to use the Memento to implement an undo feature.

How the Memento Works

The Memento pattern is useful for implementing features like undo mechanisms.

It allows you to save snapshots of an object’s state and restore it when needed, all while respecting the object’s encapsulation.

Entities in the Memento Design Pattern

  • OriginatorThe Originator creates a memento containing a snapshot of its current internal state.

    • Originator has a one-to-one associates relationship with Memento

What to watch out for when using or considering this design pattern.

Avoid using the Memento pattern if your application doesn’t require undo mechanisms or if snapshots of an object’s state could result in a significant performance hit.

Design patterns often confused with the Memento Design Pattern

The following design patterns are often confused with the Memento
for various resons.

If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.

  • Command – The Command pattern encapsulates a request as an object, allowing for parameterization and queuing of requests.

    The Command pattern is similar to the Memento in the following ways.

    • Both patterns are often used to implement undo mechanisms.
    • Both are behavioral design patterns.
  • Here are some notable differences with the Memento pattern.

    • Memento captures the current state for later restoration, while Command stores actions to be undone or redone.
    • Memento focuses on state management, while Command focuses on action management.
  • Prototype – The Prototype pattern creates new objects by copying an existing object, known as the prototype.

    The Prototype pattern is similar to the Memento in the following ways.

    • Both patterns involve creating copies of objects.
    • Both can be used to save states.
  • Here are some notable differences with the Memento pattern.

    • Memento saves the internal state of an object, while Prototype clones the entire object.
    • Memento is used for undo mechanisms, while Prototype is used for object creation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top