What is Memento Pattern in C#?

Introduction

The Memento pattern is a type of behavioral design pattern that enables an object's current state to be saved externally so that it can be restored to that state later on. This pattern is extremely useful when you need to implement undo functionality or maintain a history of states.

In C#, the Memento pattern can be implemented using a combination of three classes: the Originator, the Memento, and the Caretaker.

  1. Originator: This is the object whose state needs to be saved. It creates a memento containing a snapshot of its current state and also can restore its state from a memento.
  2. Memento: This is the object that stores the state of the Originator. It provides methods to retrieve the saved state and possibly update its state based on the Originator's needs.
  3. Caretaker: This is responsible for keeping track of the mementos. It stores and retrieves mementos but does not modify them.

Let's illustrate the Memento pattern in C# with a simple example. Suppose we have a text editor application, and we want to implement an undo feature using the Memento pattern.

using System;

// Originator
class Editor
{
    private string text;

    public string Text
    {
        get { return text; }
        set
        {
            text = value;
            Console.WriteLine("Text updated: " + text);
        }
    }

    public EditorMemento CreateMemento()
    {
        return new EditorMemento(text);
    }

    public void RestoreMemento(EditorMemento memento)
    {
        text = memento.Text;
        Console.WriteLine("Text restored: " + text);
    }
}

// Memento
class EditorMemento
{
    public string Text { get; }

    public EditorMemento(string text)
    {
        Text = text;
    }
}

// Caretaker
class History
{
    public EditorMemento Memento { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Editor editor = new Editor();
        History history = new History();

        editor.Text = "First draft";
        history.Memento = editor.CreateMemento();

        editor.Text = "Second draft";

        // Restore to previous state
        editor.RestoreMemento(history. Memento);
    }
}

In this example, the Editor class represents the Originator, which has a Text property representing the current text in the editor. It provides methods CreateMemento and RestoreMemento to save and restore the state, respectively.

The EditorMemento class represents the Memento, which stores the state of the Editor. It has a Text property representing the saved text.

The History class serves as the Caretaker, responsible for keeping track of the mementos.

In the Main method, we create an Editor instance and make some changes to its text. Before making changes, we save the current state CreateMemento and store it in the History object. Later, we modify the text and then restore it to the previous state using the RestoreMemento method.

Conclusion

The Memento pattern provides a clean way to implement undo functionality without exposing the internal state of objects. It promotes encapsulation by keeping the state information encapsulated within the Memento objects. This makes the implementation easier to maintain and extend.


Similar Articles