Memento Pattern in C# – Real-Time Example (Undo / Restore State)

What is Memento Pattern?

The Memento Pattern is a behavioral design pattern that allows you to save and restore an object's previous state without exposing its internal details.

👉 Used mainly for:

  • Undo
  • Rollback
  • Checkpoints
  • History tracking

Why Use Memento Pattern?

  • Save object snapshots
  • Restore previous state
  • Implement undo/redo
  • Maintain encapsulation
  • Track history safely

Real-Time Scenario

Text Editor:

  • User types text
  • Save state
  • Undo changes

👉 Restore previous version easily.


Real-Time Example – Text Editor Undo Feature


Step 1: Memento Class


public class EditorMemento
{
    public string Content { get; }

    public EditorMemento(string content)
    {
        Content = content;
    }
}

Step 2: Originator Class


public class TextEditor
{
    public string Content { get; set; }

    // Save state
    public EditorMemento Save()
    {
        return new EditorMemento(Content);
    }

    // Restore state
    public void Restore(EditorMemento memento)
    {
        Content = memento.Content;
    }
}

Step 3: Caretaker Class


public class History
{
    private readonly Stack<EditorMemento> _history = new();

    public void Push(EditorMemento memento)
    {
        _history.Push(memento);
    }

    public EditorMemento Pop()
    {
        return _history.Pop();
    }
}

Usage Example


var editor = new TextEditor();
var history = new History();

editor.Content = "Version 1";
history.Push(editor.Save());

editor.Content = "Version 2";

Console.WriteLine(editor.Content);

editor.Restore(history.Pop());

Console.WriteLine(editor.Content);

Output


Version 2
Version 1

👉 Previous state restored successfully.


Key Concept

Instead of:


string oldValue = content;

We do:


history.Push(editor.Save());

👉 State snapshots managed properly.


Diagram Understanding


Originator (Editor)
        ↓
      Memento
        ↑
Caretaker (History)

👉 Caretaker stores object snapshots.


Advantages

  • ✔ Supports undo/redo
  • ✔ Preserves encapsulation
  • ✔ Easy rollback functionality
  • ✔ State history management
  • ✔ Cleaner state restoration

Disadvantages

  • ✖ High memory usage for many snapshots
  • ✖ Complexity increases with large objects
  • ✖ Caretaker management required

When to Use

Use Memento Pattern when:

  • Undo/redo functionality is needed
  • State rollback is important
  • Checkpoints/history tracking required
  • Object state should remain encapsulated

Real Project Mapping (.NET + Angular)

Feature Usage
Undo/Redo Memento
Draft save Memento
Workflow rollback Memento
Form state restore Memento
Game checkpoints Memento

ASP.NET Core Real Example

Transaction Rollback Concept


using var transaction =
    await db.Database.BeginTransactionAsync();

👉 Restore previous state if failure occurs.


Advanced Example – Invoice Draft System


Invoice Draft V1
Invoice Draft V2
Invoice Draft V3

👉 Restore earlier draft versions.


Memento vs State

Memento State
Saves object snapshots Changes object behavior
Focus on restoration Focus on state-driven actions
Used for undo/history Used for workflows

Pro Tip

Memento Pattern works well with:

  • Event sourcing
  • CQRS snapshots
  • Undo systems
  • Transaction rollback
  • Draft/version systems

Summary

Memento Pattern helps you:

  • Save and restore object state
  • Implement undo/redo
  • Maintain history safely

👉 Perfect for:

  • Text editors
  • Draft systems
  • Rollback functionality
  • Workflow recovery

Comments

Popular posts from this blog

Promises in Angular

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Csharp Coding - Session