Design Patterns - SPLessons

Momento Pattern

Chapter 24

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Momento Pattern

Momento Pattern

shape Description

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.

shape Advantages

  • Momento Pattern is simple to implement.
  • Momento is a private class so it cannot be used by other objects.

shape Conceptual
figure

shape Examples

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);
 }
 }

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.

shape Output

The result will be as follows.

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

Summary

shape Key Points

  • Undo and restore operations can be done easily.
  • Serialization can be used along with the Momento Pattern.