The Memento Pattern

The Memento Pattern

Introduction

The Memento Pattern is a tool for preserving and restoring the state of an object, promoting flexibility and undo/redo operations. This pattern captures and externalises an object’s internal state, allowing for later retrieval and restoration.

Understanding the Memento Pattern

The Memento Pattern revolves around the concept of a caretaker object that manages snapshots of an originator object’s state.

These snapshots, known as mementos, encapsulate the originator’s state without exposing its implementation details, enabling flexible state management.

Benefits of the Memento Pattern

The Memento Pattern offers several advantages, including:

  • State Preservation: It allows for capturing and externalising an object’s state.
  • Undo/Redo Operations: It facilitates undo/redo functionality by providing snapshots of previous states.
  • Flexible State Management: It promotes flexible state management without exposing implementation details.
  • Encapsulation: It promotes encapsulation by isolating the originator’s state from external access.
  • Tamper Resistance: It protects the originator’s state from being modified by unauthorised parties.
  • Save/Load Operations: It enables save/load operations by storing mementos in persistent storage.

Types of Memento Patterns

The Memento Pattern encompasses several variations, each with its specific characteristics:

  • Shallow Memento: Stores a complete copy of the originator’s state.
  • Deep Memento: Recursively stores the state of nested objects.
  • Multiple Memento: Stores multiple snapshots of the originator’s state.

Implementing the Memento Pattern in C#

To illustrate the implementation of the Memento Pattern in C#, consider a simplified scenario of managing a game character’s state:

Originator:

The originator represents the game character and encapsulates its state.

public class GameCharacter
{
    private int health;
    private string name;

    public int Health => health;

    public void SetHealth(int health)
    {
        this.health = health;
    }

    public string Name => name;

    public void SetName(string name)
    {
        this.name = name;
    }
}

Caretaker:

The caretaker manages the originator’s mementos.

public class GameCaretaker
{
    private Memento memento;

    public void SaveState(GameCharacter originator)
    {
        memento = originator.CreateMemento();
    }

    public Memento RetrieveState() => memento;
}

Using the Memento Pattern:

To use the memento pattern for saving and restoring a game character’s state:

public class Memento
{
    private int health;
    private string name;

    public Memento(GameCharacter originator)
    {
        this.health = originator.Health;
        this.name = originator.Name;
    }

    public int Health => health;

    public string Name => name;
}

This example demonstrates a basic implementation of the Memento Pattern in C# for managing a game character’s state.

The memento pattern can be extended to handle more complex game scenarios and state management requirements.

Conclusion

The Memento Pattern is a valuable tool for preserving and restoring an object’s state, promoting flexibility and undo/redo operations in C# code.

It enables the capture and management of object state, facilitating flexible state manipulation and implementing undo/redo features.

By leveraging the Memento Pattern, developers can create more resilient applications to errors and user actions.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.