Memento Design Pattern in C#

Memento design pattern comes under behavioral design pattern category of Gang of four (GoF) design patterns. This article explains what is memento pattern and how to implement memento design pattern in C#.

Memento Design Pattern

Memento pattern allows you to store the internal state of an object and you can restore the object later using stored state. Let’s consider the design of a game in which you want to store the state of a given object over different check points/ levels and later the player will be able to restore the object using the stored states. Memento design pattern will be helpful in that kind of scenario.

Gang of Four(GoF) Definition:
Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.

Memento Design Pattern – UML Diagram

Memento Design Pattern in C# - UML Diagram

Participants in above UML diagram are:

  • Memento: stores the internal state of originator object. The state variable represents the internal state.
  • Originator: creates and uses memento. This class is saved and restored later using memento’s state.
    • The state variable represents the internal state of originator.
    • Creates a memento(CreateMemento) to save the internal state of originator.
    • Set a memento (SetMemento) to restore the internal state of originator. This method accepts a memento object.
  • Caretaker: This class is responsible for memento’s safekeeping. The client will never access the memento object directly. Caretaker object is responsible for client interaction.

Memento Design Pattern in C# – Example

Problem Statement

Let’s implement a simple game to understand memento design pattern in C#.

  • Consider a game of three levels.
  • Any player who clears all three level wins the game.
  • Player will be given three life lines to win the game.
  • If player clears any intermediate level (less than final level) and loses lifeline in next level, then again it will start from last complete level. Here memento pattern will be useful to store the state of completed level and to restore the same level.

Class Diagram

Memento Design Pattern in C# - ClassDiagram

Participants in above class diagram are:

  • Player: Originator Class
  • Memento: Memento Class
  • CareTaker: Caretaker Class

Memento Pattern in C# – Implementation Code

Result

Memento Design Pattern in C# -output

Hope you get the idea about how to implement memento design pattern in C#. Please post your comments for queries and feedback. Thanks for reading this article.

 

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

One thought on “Memento Design Pattern in C#

  • April 25, 2017 at 1:57 pm
    Permalink

    Hey Gyanendu, thanks for your example, I was wondering if is correct to save a memento persistently in a file to use it like a save and load game. Is there a better way to save a game?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *