Please enable Javascript to correctly display the contents on Dot Net Tricks!

Memento Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 23 Feb 2017
Total Views : 496   
Updated On : 23 Feb 2017
 

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 share what is memento pattern and how is it work?

What is Memento Design Pattern?

This 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 examine or modify 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 need to be saved and restored at a later time.

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

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
3 APR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
25 MAR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

Know More
25 MAR
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

Know More
21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

6 MAR
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+