Memento Design Pattern

The Memento design pattern is a behavioral software design pattern that is used to capture the internal state of an object so that it can be restored later. This pattern is particularly useful in cases where an object has a complex state and requires a mechanism to store and retrieve that state at a later time.

Definition:

The Memento design pattern is defined by three key elements:

  1. Originator: The object whose state is being captured and restored.
  2. Memento: An object that holds the state of the originator.
  3. Caretaker: An object that holds the memento and is responsible for restoring the state of the originator.

Purpose:

The purpose of the Memento design pattern is to provide a way to save and restore the state of an object without violating encapsulation. This pattern allows the originator to maintain control over its internal state and the memento to be used to store the state without the caretaker having access to the originator's internal state.

Benefits:

  1. Separates the concern of state preservation from the originator.
  2. Preserves encapsulation by keeping the originator's internal state private.
  3. Allows for easy restoration of the originator's state.
  4. Facilitates undo/redo functionality.

Memento C# Implementation:

Step 1: Create the Originator class.

class Originator 
{ 
   private string state;

   public void SetState(string state) 
   { 
      this.state = state; 
   } 

   public string GetState() 
   { 
      return state; 
   } 

   public Memento SaveStateToMemento() 
   { 
      return new Memento(state); 
   } 

   public void GetStateFromMemento(Memento memento) 
   { 
      state = memento.GetState(); 
   } 
}

Step 2: Create the Memento class.

class Memento 
{ 
   private string state; 

   public Memento(string state) 
   { 
      this.state = state; 
   } 

   public string GetState() 
   { 
      return state; 
   } 
}

Step 3: Create the Caretaker class.

class Caretaker 
{ 
   private List<Memento> mementoList = new List<Memento>(); 

   public void Add(Memento state) 
   { 
      mementoList.Add(state); 
   } 

   public Memento Get(int index) 
   { 
      return mementoList[index];
   } 
}

Step 4: Use the Memento design pattern in your code.

Originator originator = new Originator();
Caretaker caretaker = new Caretaker(); 

originator.SetState("State #1"); 
caretaker.Add(originator.SaveStateToMemento()); 

originator.SetState("State #2"); 
caretaker.Add(originator.SaveStateToMemento()); 

originator.SetState("State #3"); 
careTaker.Add(originator.SaveStateToMemento()); 

originator.SetState("State #4");
Console.WriteLine("Current state: " + originator.GetState()); 

originator.GetStateFromMemento(caretaker.Get(1)); 
Console.WriteLine("Previous state: " + originator.GetState()); 

Console.ReadKey();

The output of example:

memento result

Conclusion:

The Memento design pattern provides a simple and effective way to store and restore the internal state of an object. It allows you to preserve encapsulation while still allowing you to save and restore the state of an object by providing a flexible mechanism.

By implementing the Memento design pattern, you can improve the flexibility and maintainability of your code, making it easier to manage complex objects with multiple states.

By following the implementation steps outlined in this article, you can easily use the Memento design pattern in your own C# projects.