loader

Ever wished you could freeze a moment in time and go back to it whenever you want? That’s exactly what the Memento Pattern does! It captures an object’s internal state without exposing its details, allowing you to restore it later—just like sealing a time capsule that holds your memories for future reference.

Why Bother with It?

  • State Preservation: Just as a time capsule preserves your cherished memories without revealing every secret, the Memento Pattern saves an object’s state while keeping its inner workings hidden.
  • Encapsulation: You can capture and restore an object’s state without breaking encapsulation. The internal details remain private, yet you can rewind to a previous state when needed.
  • Undo/Redo Functionality: Made a mistake? No problem! With mementos, you can implement undo features that revert your system back to a previous moment—like rewinding time.

How It Works (In Plain English)

  1. Originator: The object whose state you want to save. It creates a memento containing a snapshot of its current state.
  2. Memento: A simple container that holds the state of the originator. It doesn’t reveal any internal details—just a safe snapshot.
  3. Caretaker: The keeper of these time capsules. It stores and retrieves mementos without peeking inside, allowing you to restore the originator’s state when necessary.

C# Code Example

C#
using System;
using System.Collections.Generic;

// Memento: Captures and stores the state of the Originator.
public class Memento
{
    public string State { get; private set; }
    
    public Memento(string state)
    {
        State = state;
    }
}

// Originator: The object whose state is to be saved.
public class Originator
{
    public string State { get; set; }
    
    // Saves the current state inside a memento.
    public Memento SaveStateToMemento()
    {
        return new Memento(State);
    }
    
    // Restores the state from a memento.
    public void GetStateFromMemento(Memento memento)
    {
        State = memento.State;
    }
}

// Caretaker: Keeps a list of mementos.
public class Caretaker
{
    private List<Memento> _mementoList = new List<Memento>();
    
    public void Add(Memento memento)
    {
        _mementoList.Add(memento);
    }
    
    public Memento Get(int index)
    {
        return _mementoList[index];
    }
}

// Client Code
public class Program
{
    public static void Main()
    {
        Originator originator = new Originator();
        Caretaker caretaker = new Caretaker();
        
        // Set initial state and save a snapshot.
        originator.State = "State #1";
        originator.State = "State #2";
        caretaker.Add(originator.SaveStateToMemento());
        
        // Change state again and save another snapshot.
        originator.State = "State #3";
        caretaker.Add(originator.SaveStateToMemento());
        
        // Change state without saving.
        originator.State = "State #4";
        Console.WriteLine("Current State: " + originator.State);
        
        // Restore to a previous state.
        originator.GetStateFromMemento(caretaker.Get(1));
        Console.WriteLine("Restored State: " + originator.State);
    }
}

The Takeaway

The Memento Pattern is like creating a time capsule for your object’s state. It lets you capture a moment in time and restore it later—perfect for implementing undo/redo functionalities or simply preserving progress. By keeping the internal details hidden and only storing the necessary snapshot, this pattern makes your code more robust and easier to manage.

Happy coding—and may your time capsules always hold the perfect memories for when you need them!