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:
public class Person
{
public String Name { get; set; }
public String Surname { get; set; }
MomentoPerson objMPerson = null;
public Person()
{
Name = "Sourav";
Surname = "Kayal";
objMPerson = new MomentoPerson(Name,Surname);
}
public void Update(String name, string Surname)
{
this.Name = name;
this.Surname = Surname;
}
public void Revert()
{
Name = objMPerson.Name;
Surname = objMPerson.Surname;
}
}
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:
public class MomentoPerson
{
public String Name { get; set; }
public string Surname { get; set; }
public MomentoPerson(String Name, String Surname)
{
this.Name = Name;
this.Surname = Surname;
}
}
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.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DesignPattern;
namespace DesignPattern
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Person objP = new Person();
private void Update_Click(object sender, EventArgs e)
{
objP.Update(this.txtName.Text, this.txtSurname.Text);
}
public void DisplayCustomer()
{
this.txtName.Text = objP.Name;
this.txtSurname.Text = objP.Surname;
}
private void Cancel_Click(object sender, EventArgs e)
{
objP.Revert();
DisplayCustomer();
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayCustomer();
}
}
}
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.
Conclusion
Here we have shown how to implement the Memento Design Pattern. Hope you have enjoyed it.