Memento Pattern

Posted by Abhishek on February 12, 2020

Design Patterns

Problem Statement

You have an editor in which you can save the content many times. You need an Undo feature in the editor with which you can undo the changes and get the previous content.

public class Program
{
    public static void Main(string[] args)
    {
        var editor = new Editor();
        editor.setContent("First Content");
        editor.setContent("Second Content");
        editor.setContent("Third Content");
        editor.undo();
        Console.WriteLine(editor.getContent());
        Console.ReadKey();
    }
}

The output should be “Second Content”.

Put some thoughts on how you can achieve this using your programming language. Do not google and try to find out a solution. If you have achieved the solution/not able to figure out the solution, let’s now look at the solution approach.

Solution Approach

Memento UML

Explanation of the UML

Implementation

public class Editor
{
    private string content;

    public EditorState createState()
    {
        return new EditorState(content);
    }

    public void restore(EditorState state)
    {
        content = state.getContent();
    }

    public void setContent(string content)
    {
        this.content = content;
    }

    public string getContent()
    {
        return this.content;
    }
}

public class EditorState
{
    private readonly String content;

    public EditorState(String content)
    {
        this.content = content;
    }

    public String getContent()
    {
        return content;
    }
}

public class History
{
    private List<EditorState> states = new List<EditorState>();

    public void push(EditorState state)
    {
        states.Add(state);
    }

    public EditorState pop()
    {
        var lastState = states[states.Count - 1];
        states.Remove(lastState);
        return states[states.Count - 1];
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var editor = new Editor();
        var history = new History();
        editor.setContent("First Content");
        history.push(editor.createState());

        editor.setContent("Second Content");
        history.push(editor.createState());

        editor.setContent("Third Content");
        history.push(editor.createState());

        editor.restore(history.pop()); //this is the UNDO

        Console.WriteLine(editor.getContent());
        Console.ReadKey();
    }
}

Thanks for reading this post. Enjoy !!
Share on: