Monday, January 19, 2015

Memento Design Pattern

Design Pattern > Behavioral Design Pattern > Memento Design Pattern

Sometimes it require to store the state of an object at somepoint and restore this state later.Memento pattern help in this case.
The intent of Memento design pattern is to store an object's internal state(outisde of this object) and restores it at later stage without violating the encaptulation principle.

Key Terms :
Originator -

  • The object whose state need to be saved
  • Has getter and setter for it's own state
  • Create and return memento object in method createMemento
  • get previous state from memento in method setMemento()

CareTaker -


  • Keeps list of mementos
  • Has addMemento method to add new memento to the list
  • return specific memento in getMemento method


Memento -


  • Is a POJO class
  • Set State of Originator
  • retuirn state of Originator in getState methd
  • Only Originator should assign and retrive its state


Client -

1. Create Originator, Caretaker objects
2. set State of Originator object
3. when state of originator needs to be saved then create Memento object by calling originator's createMemento method
4. Add memento to caretaker
repeat steps 2 to 4 as per your requirement to set states of Originator object
When need to restore originator state to previous state then
1. get memento object by calling getMemento on caretaker object
2. Set originator's state from the returned memento object in step 1 by calling originator's setMemento method
3. get restored state of Originator by callling it's getState method


Benefits of Using Memento Pattern :

Since object oriented programming dictates that objects should encapsulate their state it would violate this law if objects’ internal variables were accessible to external objects. The memento pattern provides a way of recording the internal state of an object in a separate object without violating this law

The memento eliminates the need for multiple creation of the same object for the sole purpose of saving its state.

The memento simplifies the Originator since the responsibility of managing Memento storage is no longer centralized at the Originator but rather distributed among the Caretakers

Java Implementation :
                In key terms section we have seen the pseudo code, now let's see actual java implementation


/**
 * Memento Class
 */
package com.kmingle.memento;

public class Memento {

String state;

public Memento(String _state){
state = _state;
}

public String getState(){
return state;
}
}

/**
 *  Originator class whose state needs to be stored/restored
 */
package com.kmingle.memento;


public class Originator {

private String state;

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public Memento createMemento(){
return new Memento(state);
}

public void setMemento(Memento memento){
state = memento.getState();
}
}

/**
 *  CareTaker Class
 */
package com.kmingle.memento;

import java.util.ArrayList;
import java.util.List;

public class CareTaker {

List mementoList = new ArrayList();

public void addMemento(Memento _memento){
mementoList.add(_memento);
}

public Memento getMemento(int i){
return mementoList.get(i);

}
}

/**
 * the demo class
 */
package com.kmingle.memento;

public class Client {

public static void main(String[] args) {

Originator org = new Originator();
CareTaker caretaker = new CareTaker();

org.setState("State1");
org.setState("state2"); // only store the state from here

Memento _memento = org.createMemento();
caretaker.addMemento(_memento);

org.setState("state3");

_memento = org.createMemento();
caretaker.addMemento(_memento);

org.setState("state4");
org.setState("state5");
// originator's current state
System.out.println("originator's current state : "+ org.getState());

// restore originator to previous state
_memento = caretaker.getMemento(1);
org.setMemento(_memento);
System.out.println("originator's restored state : "+ org.getState());

}

}

No comments:

Total Pageviews