Behavioral Design Pattern : Memento

Avinash Dhumal
3 min readSep 2, 2023

--

Image generated from AI

Introduction

In the world of software development, design patterns play a crucial role in creating well-structured and maintainable code. One such design pattern is the Memento pattern, which provides a way to capture and restore an object’s internal state. In this article, we will explore the Memento design pattern using C# examples, explained in a way that even a kid can understand. We will discuss the pros and cons of using this pattern too.

What is the Memento Design Pattern?

Imagine you have a toy car that can change its color. Sometimes, you may want to switch the color to red, and other times you may want it to be blue. Now, what if you want to remember the previous color and easily switch back to it? This is where the Memento design pattern comes into play.

The Memento pattern is used to capture an object’s state at a particular time and store it in a separate object called a Memento. This allows the object to be restored to its previous state later on if needed. In other words, the Memento pattern provides a way to save and restore the state of an object.

C# Example — Saving and Restoring a Toy Car’s Color

Let’s dive into a C# example that demonstrates the Memento pattern. We will create a ToyCar class that can change its color, and we will use the Memento pattern to save and restore its state.

Class Diagram
// Memento class
public class ToyCarMemento
{
public string Color { get; }

public ToyCarMemento(string color)
{
Color = color;
}
}

// Originator class
public class ToyCar
{
private string _color;

public string Color
{
get => _color;
set
{
_color = value;
Console.WriteLine($"Car color changed to {_color}.");
}
}

public ToyCarMemento SaveState()
{
return new ToyCarMemento(Color);
}

public void RestoreState(ToyCarMemento memento)
{
Color = memento.Color;
}
}

// Caretaker class
public class ColorChanger
{
private ToyCarMemento _memento;

public void ChangeColor(ToyCar car, string color)
{
_memento = car.SaveState();
car.Color = color;
}

public void UndoColorChange(ToyCar car)
{
car.RestoreState(_memento);
}
}

// Usage
pulic class Program
{
static void Main()
{
var car = new ToyCar();
var changer = new ColorChanger();

changer.ChangeColor(car, "Red");

// Let's save the current state
var savedState = car.SaveState();
changer.ChangeColor(car, "Blue");
changer.ChangeColor(car, "Green");

// Undo the last color change
changer.UndoColorChange(car);
Console.WriteLine($"Current car color: {car.Color}");

// Restore the saved state
car.RestoreState(savedState);
Console.WriteLine($"Restored car color: {car.Color}");
}
}

In this example, we have three main classes:

  • ToyCar: Represents the object whose state we want to save and restore. It has properties and methods to change and retrieve the car’s color. It also provides methods to save and restore its state using the Memento pattern.
  • ToyCarMemento: Represents the Memento object that stores the state of the ToyCar. It has a property to hold the car’s color.
  • ColorChanger: Acts as the caretaker, responsible for changing the color of the ToyCar and providing an undo mechanism using the Memento pattern.

Pros and Cons of Using the Memento Pattern

Now, let’s discuss the advantages and disadvantages of using the Memento pattern.

Pros and Cons

Conclusion

The Memento design pattern provides a convenient way to save and restore an object’s state. In this article, we explored the Memento pattern using a C# example, explained in a kid-friendly manner. We discussed the advantages, such as state encapsulation and undo/redo functionality, as well as the disadvantages, including increased memory usage and potential performance impact. By understanding and utilizing design patterns like the Memento pattern, developers can create more flexible and maintainable software solutions.

If you enjoy the content I create and would like to show your appreciation, you can buy me a coffee!

--

--

Avinash Dhumal

13+ years of software architect, design, development, management, and support experience in Microsoft technologies using Azure & AWS Cloud services.