Memento Design Pattern

Memento Design Pattern

29 Mar 2024
Intermediate
25.2K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Memento Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is memento pattern and how is it work?

What is Memento Design Pattern?

Memento design pattern used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.

This pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc.

Memento Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Memento Design Pattern is given below:

Memento Design Pattern C#

The classes, interfaces, and objects in the above UML class diagram are as follows:

  1. Originator

    This is a class that creates a memento object containing a snapshot of the Originator's current state. It also restores the Originator to a previously stored state by using SetMemento operation.

  2. Memento

    This is a class that holds the information about the Originator’s saved state.

  3. Caretaker

    This is a class that is used to hold a Memento object for later use. This acts as a store only; it never examines or modifies the contents of the Memento object.

C# - Implementation Code

public class Originator
{
 private string state;

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

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

public class Memento
{
 private string state;

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

 public string GetState()
 {
 return state;
 }
}

public class Caretaker
{
 public Memento Memento { get; set; }
}


Real Life Example:

Real Life Example of Memento Design Pattern C#

When to use it?

  1. The state of an object needs to be saved and restored at a later time.

  2. The state of an object cannot be exposed directly by using an interface without exposing implementation.

Read More Articles Related to Design patterns
What do you think?

I hope you will enjoy the Memento Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core (Project) Jun 23 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this