Memento - Behavioural


Overview:

The Memento Pattern in C# transforms your code into a time capsule storyteller—preserving memories and weaving a tech tale through the ages. Let's dive into this captivating coding narrative:

Implementation in C#:

In C#, the Memento Pattern involves capturing an object's internal state to allow undo or restore operations. Consider a text editor scenario where the caretaker saves and restores the editor's state:

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

    public EditorMemento(string content)
    {
        Content = content;
    }
}

// Originator
public class TextEditor
{
    private string content;

    public string Content
    {
        get => content;
        set
        {
            content = value;
            // Save state when content changes
            SaveState();
        }
    }

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

    public void RestoreState(EditorMemento memento)
    {
        content = memento.Content;
    }

    private void SaveState()
    {
        // Save current state for undo
        // Implementation details not shown
    }
}

// Caretaker
public class History
{
    private readonly List<EditorMemento> states = new List<EditorMemento>();

    public void SaveState(EditorMemento memento)
    {
        states.Add(memento);
    }

    public EditorMemento Undo()
    {
        // Retrieve and remove the last saved state for undo
        var lastState = states.Last();
        states.Remove(lastState);
        return lastState;
    }
}

Pros:

  1. Undo/Redo Functionality: Enables undo and redo operations with ease.

  2. Separation of Concerns: Keeps the originator and caretaker concerns separate.

  3. State Preservation: Allows capturing and restoring the state without exposing internal details.

Cons:

  1. Increased Memory Usage: Storing multiple states can increase memory usage.

  2. Complexity: Introducing mementos may add complexity to the codebase.

When to Use and When Not:

  • Use: When you need to implement undo/redo functionality or capture and restore an object's state.

  • Avoid: In scenarios where state preservation is unnecessary or when the overhead of managing mementos outweighs the benefits.

Usage in .NET Core Framework:

While not explicitly present in the .NET Core framework, principles of the Memento Pattern can be observed in serialization and deserialization processes. Libraries like Newtonsoft.Json facilitate the conversion of object states to JSON and back, showcasing a similar concept.

Real-Life Example:

Imagine a drawing application where the user can undo and redo brush strokes. The Memento Pattern is applied:

// Drawing application
var drawingApp = new DrawingApplication();
var canvas = new Canvas();

// User draws strokes
canvas.DrawStroke("Stroke1");
canvas.DrawStroke("Stroke2");

// Save state for undo
drawingApp.SaveState(canvas.CreateMemento());

// User undoes the last stroke
drawingApp.Undo(canvas);

// Redo the undone stroke
drawingApp.Redo(canvas);

Here, the DrawingApplication facilitates undo and redo operations, preserving the canvas's state with each stroke.

In conclusion, the Memento Pattern in C# turns your code into a time capsule storyteller—preserving memories and weaving a tech tale through the ages. While offering undo/redo functionality and state preservation, developers should consider the trade-offs in terms of increased memory usage and complexity. Its reflection in serialization and real-life applications showcases its adaptability, making it a charming choice for scenarios where code becomes a master storyteller, cherishing moments with each line of code.


No files yet, migration hasn't completed yet!