Memento pattern is a way to preserve internal state of an object without violating encapsulation.
It is a widely used behavioral pattern.
There are three section of this pattern
Originator: This is the original object for that memento get created
Memento: This is the copy of original class to preserve the original state of originator object.
Care Taker: This object holds memento object to restore to originator at any point of time.
Requirement: During changing of object state if something goes wrong, object can be reverted to original state.
Use: Original state can be re-stored at any point of time during application running.
Problem Solved: Generally when required people restore object to its original state from database. So by using this pattern no need to query database to restore object.
Sample Code:
/// <summary>
It is a widely used behavioral pattern.
There are three section of this pattern
Originator: This is the original object for that memento get created
Memento: This is the copy of original class to preserve the original state of originator object.
Care Taker: This object holds memento object to restore to originator at any point of time.
Requirement: During changing of object state if something goes wrong, object can be reverted to original state.
Use: Original state can be re-stored at any point of time during application running.
Problem Solved: Generally when required people restore object to its original state from database. So by using this pattern no need to query database to restore object.
Sample Code:
/// <summary>
///
Original class
/// </summary>
public class
Origintor
{
private string name;
private string mobile;
private string eMail;
public Origintor(string _name, string
_mobile, string _email)
{
name = _name;
mobile = _mobile;
eMail = _email;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Mobile
{
get
{
return mobile;
}
set
{
mobile = value;
}
}
public string Email
{
get
{
return eMail;
}
set
{
eMail = value;
}
}
public Memento SaveMemento()
{
return new Memento(name,
mobile, eMail);
}
public void RestoreMemento(Memento
objMemento)
{
this.Name =
objMemento.Name;
this.Mobile =
objMemento.Mobile;
this.Email =
objMemento.Email;
}
}













