Design Pattern For Beginners - Part 8 : Memento Design Pattern

Welcome to the Design Pattern For Beginners article series. In this article series we have been discussing various design patterns. If you are new in this series or very much interested in learning design patterns then please have a look at the following links.

This article explains the Memento Design Pattern. We will first discuss the basic need of the Memento Design Pattern and will discuss in which scenario it is relevant.

Why is the Memento Design Pattern necessary?

The Memento Design Pattern is useful when we want to save data in a temporary location and depending on the user's needs we can retrieve the old data.

So, let's think about the scenario with a form (yes, simple Windows Forms form with a few controls) and in the form load event data will be loaded. Now the user may update the loaded data and allowed to save it. And after saving the data the user can restore it (if she wishes).

Now, the problem is, once she updates the form, how will we restore the old data? Don't worry; the Memento Design Pattern will help us.

Let's look behind the scenes

OK, here we will solve our big question. How will we restore the old data after the update is done? Let's apply our common sense, when we are say we will restore the old data, that implies that somewhere we are keeping the old data and when necessary we will get it back.

Yes, in the Memento Pattern we will keep a replica of the original object and all modifications will be performed in the original object. Now, when we need the old data to be restored we can get it back from the replica object.

And our problem is solved.

How to implement?

OK, so far we have said what the Memento Design Pattern is and when it is useful. Now for the technical and implementation parts. We have already said that we will keep a copy of the original object. So let's start with a small class implementation.

At first we will design our original class and then we will implement the mechanism to keep a copy of the original object.

The original person class is:

  1. public class Person  
  2. {  
  3.     public String Name { getset; }  
  4.     public String Surname { getset; }  
  5.     MomentoPerson objMPerson = null;  
  6.     public Person()  
  7.     {  
  8.         Name = "Sourav";  
  9.         Surname = "Kayal";  
  10.         objMPerson = new MomentoPerson(Name,Surname);  
  11.     }  
  12.     public void Update(String name, string Surname)  
  13.     {  
  14.         this.Name = name;  
  15.         this.Surname = Surname;  
  16.     }  
  17.     public void Revert()  
  18.     {  
  19.         Name = objMPerson.Name;  
  20.         Surname = objMPerson.Surname;  
  21.     }  

Let's discuss the code snippet. Person() is the constructor and when the object is created it will initialize the Name and surname properties. And within this constructor the most wonderful thing is happening. Here we are maintaining a copy of the original object in another class (yes, in the MomentoPerson class).

In the following constructor, we defined the Update() method to update the original object and beneath Update() we have defined the Revert() method for restoring the modified object.

And here is our MomentoPerson class, which is nothing but a mirror of the original class:

  1. public class MomentoPerson  
  2. {  
  3.     public String Name { getset; }  
  4.     public string Surname { getset; }  
  5.     public MomentoPerson(String Name, String Surname)  
  6.     {  
  7.         this.Name = Name;  
  8.         this.Surname = Surname;  
  9.     }  

We are also seeing that the contents are the same properties as the Person class. And the constructor will be called from the constructor of the original object.

Let's now build the user interface. It's nothing but a simple form containing two TextBoxes. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using DesignPattern;  
  10. namespace DesignPattern  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         Person objP = new Person();  
  19.         private void Update_Click(object sender, EventArgs e)  
  20.         {  
  21.             objP.Update(this.txtName.Text, this.txtSurname.Text);  
  22.         }  
  23.         public void DisplayCustomer()  
  24.         {  
  25.             this.txtName.Text = objP.Name;  
  26.             this.txtSurname.Text = objP.Surname;  
  27.         }  
  28.         private void Cancel_Click(object sender, EventArgs e)  
  29.         {  
  30.             objP.Revert();  
  31.             DisplayCustomer();  
  32.         }  
  33.         private void Form1_Load(object sender, EventArgs e)  
  34.         {  
  35.             DisplayCustomer();  
  36.         }  
  37.     }  
  38. }
In the form load it will call DisplayCustomer(). And this function will show the values that will be assigned within the constructor.

The Update() function will pass an updated value from/to text boxes and the cancel function will call the revert method that will restore the value of the original object.

Here is the user interface.


Now we will update those TextBox values.


Again we will restore our old data.



Here we have shown how to implement the Memento Design Pattern. Hope you have enjoyed it.

Similar Articles