Memento Pattern in C#

The Memento Pattern lets you capture an object's internal state so it can be restored later — without exposing its internal structure. It's like creating a snapshot of the current situation that you can roll back to if needed.

Real-Life Analogy: Game Save Feature

Think about saving your progress in a video game. You can pause and save the game state — your level, health, position — and later load it to resume from the same point.

You don't need to understand how the save system works internally. The game handles it behind the scenes, and you just load your last save when needed. That's what the Memento Pattern does in software.

Game saving and loading using snapshots of progress
Like a game saving progress, the Memento Pattern lets you store and restore the internal state of an object at any point.

Benefits of Memento Pattern

This pattern provides a clean way to implement undo/redo functionality. It protects the object's internal structure while still allowing recovery from mistakes.

It helps in situations where temporary states need to be remembered and possibly restored.

  • Allows rollback to a previous state
  • Preserves encapsulation (doesn't expose internal state)
  • Good for undo/redo features

When Should You Use It?

Use the Memento Pattern when your app needs to let users undo actions or recover a previous state. It's perfect when working with objects that can go through many changes and you want to track them.

Great use cases include:

  • Text editors or image editors with undo history
  • Game save/load mechanics
  • Undoable operations in forms

When Not to Use It: Avoid it if the state is too large or frequent saves would harm performance or memory.

What to Implement

To use this pattern, you need:

A main object (originator) that creates and restores state, a memento class to hold the snapshot, and a caretaker that manages history.

  • Originator: The object whose state will be saved
  • Memento: A snapshot holding internal state
  • Caretaker: Stores and restores mementos

How It Works in C#


// Memento
public class GameSave
{
    public int Level { get; }
    public int Health { get; }

    public GameSave(int level, int health)
    {
        Level = level;
        Health = health;
    }
}

// Originator
public class Game
{
    public int Level { get; set; }
    public int Health { get; set; }

    public GameSave Save() => new GameSave(Level, Health);
    public void Load(GameSave save)
    {
        Level = save.Level;
        Health = save.Health;
    }

    public void Show() =>
        Console.WriteLine($""Level: {Level}, Health: {Health}"");
}

// Caretaker
public class SaveSystem
{
    private readonly Stack<GameSave> history = new();

    public void Save(GameSave save) => history.Push(save);
    public GameSave Load() => history.Pop();
}

Usage:


var game = new Game { Level = 1, Health = 100 };
var saveSystem = new SaveSystem();

saveSystem.Save(game.Save());

game.Level = 2;
game.Health = 50;

game.Show();  // Level: 2, Health: 50

var previous = saveSystem.Load();
game.Load(previous);

game.Show();  // Level: 1, Health: 100

Final Thoughts

The Memento Pattern gives your users a safety net. Whether it's undoing a change or reloading a save, it lets you capture a safe state and return to it when needed.

It keeps the object's internal logic private while still providing powerful rollback capabilities.

Memento Pattern in C# | SimplyAdvanced.dev