Design Patterns Mastery

Memento Pattern: Capturing and restoring object state

1 Views Updated 5/4/2026

The Memento Pattern

The Memento Pattern provides the ability to restore an object to its previous state (undo via rollback). It captures and externalizes an object's internal state without violating Encapsulation. The object itself is the only one who knows how to "read" the state back.

1. Why not just save properties?

If you have 50 private fields, a "Zapper" class shouldn't have to know about all of them to save a backup. The Memento pattern creates a tiny "Snapshot" object that only the original class understands.

public class Editor 
{
    private string _content;
    
    public Memento Save() => new Memento(_content);
    
    public void Restore(Memento m) => _content = m.GetContent();
}

public class Memento // Immutable snapshot
{
    private readonly string _state;
    public Memento(string s) => _state = s;
    public string GetContent() => _state;
}

2. Real-World Scenario: Game Save States

In a game engine, when you hit "Quick Save," the engine takes a Memento of the Player, the NPCs, and the World. When you die, it feeds those Mementos back into the objects to reset their HP, position, and inventory.

4. Interview Mastery

Q: "How is the Memento pattern different from simple Serialization?"

Architect Answer: "Intent and Encapsulation. **Serialization** is used to convert an object into a transferrable format (JSON/XML) for storage or transmission. **Memento** is used specifically to capture an 'In-Memory Snapshot' for the purpose of undoing a current operation. Memento protects encapsulation by ensuring that no other class can see or modify the snapshotted state; it is a 'Black Box' to everyone except the creator."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)