Memento Pattern: Game Save Feature
The Memento Pattern lets you save and restore the state of an object, like taking a snapshot at a certain time. It is useful when users want to undo changes or roll back to an earlier state.
With this pattern, you can keep the saved state private and safe, so it does not affect the rest of your app. It is often used in editors, games, and systems that need backup or history.
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.

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
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
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.
Where Is It Used in the Real World?
The Memento Pattern is used whenever users need to undo/redo actions or roll back to a previous state. It's standard in editing software and games.
- Text editors and IDEs (undo/redo)
- Image/video editors (revert to previous states)
- Games (save and restore game progress)
- Configuration management (restore previous settings)
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.