05 Dec 2023



Advanced

The Memento design pattern is a behavioral pattern that allows an object's internal state to be captured and restored without exposing its implementation details. In C#, the Memento pattern typically involves the following main components:

  1. Originator:

    • Represents the object whose state needs to be saved (and possibly restored).
    • Creates a memento object to store its internal state.
    • Uses the memento to restore its state.
    public class Originator
    {
        private string state;
    
        public string State
        {
            get { return state; }
            set
            {
                Console.WriteLine($"Setting state to: {value}");
                state = value;
            }
        }
    
        public Memento CreateMemento()
        {
            return new Memento(state);
        }
    
        public void SetMemento(Memento memento)
        {
            Console.WriteLine($"Restoring state from Memento: {memento.State}");
            state = memento.State;
        }
    }
    
  2. Memento:

    • Stores the internal state of the originator object.
    • Can provide getters and setters to allow the originator to access or modify its state.
    public class Memento
    {
        public string State { get; }
    
        public Memento(string state)
        {
            State = state;
        }
    }
    
  3. Caretaker:

    • Holds a reference to the memento but does not manipulate its contents.
    • Can keep a history of mementos to support undo functionality.
    public class Caretaker
    {
        public Memento Memento { get; set; }
    }
    
  4. Client:

    • Requests the originator to save or restore its state using mementos.
    class Client
    {
        static void Main()
        {
            Originator originator = new Originator();
            Caretaker caretaker = new Caretaker();
    
            // Set and save the state
            originator.State = "State1";
            caretaker.Memento = originator.CreateMemento();
    
            // Change the state
            originator.State = "State2";
    
            // Restore the state from the memento
            originator.SetMemento(caretaker.Memento);
        }
    }
    

In this example:

  • Originator is the object whose state needs to be saved and restored. It creates mementos to store and restore its internal state.
  • Memento is the object that stores the internal state of the originator. It is used by the originator to capture its state and restore it later.
  • Caretaker is an object that holds a reference to a memento. It can keep a history of mementos to support undo functionality.
  • The Client creates instances of the originator and caretaker, requests the originator to save and restore its state using mementos, and manipulates the state to demonstrate the ability to restore it.

The Memento pattern is useful when you need to implement undo functionality or save and restore the state of an object without exposing its internal details. It allows you to encapsulate the object's state and provides a way to revert the object to a previous state.

software-design-patterns
memento-design-pattern
c#