Momento Design Pattern Example in C#

Hi,

(PLEASE NOTE: below is my simple understanding and explanation of the momento design pattern, just to give you a head-start with easy to understand explanation).

I thought of writing an example that simplifies the concept of using this design pattern. Momento is a design pattern that you use when you want to enable restoring previous states of objects (like enabling Undo functionality to the users) or you can use it also for logging old values of objects.

This pattern works by utilizing three main objects :

Original Object: which is your normal data class or any class that holds the information you need so that you program can use it. this class MUST PROVIDE two methods: save and restore. these methods will save and restore the state of the object.

Care Taker : is an interface to the momento class where you set the previous state of an object in, and restore it from this caretaker too. the original class should not deal with the Momento object directly.

Momento Object: is a class that has the same properties of the original class and a constructor that can set new values to all these properties when passed.

//Below is a sample easy to read code that I’ve written for this purpose:

//1) the original class

public class MainDataOrinigal
{
//this calss has to have two properties SaveMomento and Restore Momento beside its original Properties and methods

public string UserName { get; set; }
public string Passwword { get; set; }

public DataMomento SaveMomento()
{

return new DataMomento(this.UserName, this.Passwword);

}

public void RestorMomento(DataMomento _previousState)
{

this.UserName = _previousState.UserName;
this.Passwword = _previousState.Password;

}

}

//2) the caretaker class

public class DataMomentoCareTaker
{

public DataMomento KeepMomento { get; set; }

}

//3) the Momento Class

public class DataMomento
{

public string UserName { get; set; }
public string Password { get; set; }

public DataMomento(string _username, string _password)
{
this.UserName = _username;
this.Password = _password;
}
}

//lets write a simple Console app to test this:

///set orignial state

MainDataOrinigal orinigal = new MainDataOrinigal();
orinigal.UserName = “user111”;
orinigal.Passwword = “whateverpasswd”;

//save the state using Only CareTaker, no direct communications

DataMomentoCareTaker caretaker = new DataMomentoCareTaker();
caretaker.KeepMomento = orinigal.SaveMomento();

//change the orinigal state

orinigal.UserName = “AJ”;
orinigal.Passwword = “jjabrams”;

//done to here

//print out the current state

Console.WriteLine(“Current State ” + orinigal.UserName + ” ” + orinigal.Passwword);
Console.WriteLine(“calling caretaker to restore previous state momento “);
//Note Only CareTaker needs to be passed to the original RestoreMethod to restore
orinigal.RestorMomento(caretaker.KeepMomento);
Console.WriteLine(“Previous State ” + orinigal.UserName + ” ” + orinigal.Passwword);

Console.ReadLine();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Hope this helps!