Momento Pattern is a Behavioral Pattern, which is used to store the state of the object. Momento Pattern consists of two objects 'originator' and 'caretaker'. Originator state should be saved, so inner classes are used for storing the state and the inner class is known as "momento". Caretaker is a helper class.
Advantages
Momento Pattern is simple to implement.
Momento is a private class so it cannot be used by other objects.
Conceptual
figure
Momento: Stores inside condition of the Originator object. The state can incorporate any number of state variables. Memento must have two interfaces, an interface to the guardian. This interface must not permit any operations or entrance to the inner state put away by the momento and consequently respects embodiment. The other interface is to the originator and permits the originator to get any state variables important for the originator to restore the previous state.
Originator: Makes a Momento object catch the originators inward state. Utilizes the token item to restore its previous state.
Caretaker: Takes charge of keeping the token. The keepsake is dark to the guardian, and the overseer must not work on it.
Examples
[java]
public class Info
{
String personName;// stores the person name
int dayNumber;// stores the daynumber
int weight;// stores weight
public Info(String personName, int dayNumber, int weight)
{
this.personName = personName;
this.dayNumber = dayNumber;
this.weight = weight;
}
public String toString()
{
return "Name: " + personName + ", day number: " + dayNumber + ", weight: " + weight;
}
public void setDayNumberAndWeight(int dayNumber, int weight)
{
this.dayNumber = dayNumber;
this.weight = weight;
}
public Memento save()
{
return new Memento(personName, dayNumber, weight);
}
public void restore(Object objMemento)
{
// using the objects methods are called
Memento memento = (Memento) objMemento;
personName = memento.mementoPersonName;
dayNumber = memento.mementoDayNumber;
weight = memento.mementoWeight;
}
// memento object that stores the saved state of the originator
private class Memento
{
String mementoPersonName;
int mementoDayNumber;
int mementoWeight;
public Memento(String personName, int dayNumber, int weight)
{
mementoPersonName = personName;
mementoDayNumber = dayNumber;
mementoWeight = weight;
}
}
}
// caretaker - saves and restores a DietInfo object's state via a memento
// note that DietInfo.Memento is not visible to the caretaker, so we need to cast the memento to a Object
public class DietCaretaker
{
Object objMemento;
public void saveState(Info info)
{
objMemento = info.save();
}
public void restoreState(Info info)
{
Info.restore(objMemento);
}
}
public class Memento1
{
public static void main(String[] args)
{
// caretaker
DietCaretaker dietCaretaker = new DietCaretaker();
// originator
Diet diet = new DietInfo("Fem", 1, 25);
System.out.println(info);
info.setDayNumberAndWeight(2, 50);
System.out.println(info);
System.out.println("Saving state.");
infoCaretaker.saveState(info);
info.setDayNumberAndWeight(3,75);
System.out.println(info);
dietInfo.setDayNumberAndWeight(4, 100);
System.out.println(info);
System.out.println("Restoring saved state.");
dietInfoCaretaker.restoreState(info);
System.out.println(info);
}
}[/java]
Info is the caretaker class that is used to store the state(momento).Momento is not visible to the caretaker. The data present in momento is protected and caretaker protects the state of Info.Momento class demonstrates the use of Momemto Pattern.
Output
The result will be as follows.
[java]Name: Fem, day number: 1, weight: 25
Name: Fem, day number: 2, weight: 50
Saving state.
Name: Fem, day number: 3, weight: 75
Name: Fem, day number: 4, weight: 100
Restoring saved state.
Name: Fem, day number: 2, weight: 50[/java]
Summary
Key Points
Undo and restore operations can be done easily.
Serialization can be used along with the Momento Pattern.