Friday, April 13, 2012

Going Technical...Memento Pattern

Today let's talk about another pattern , namely "Memento" pattern. This pattern is quite simple compared to many other pattern. So let's try to think about an analogy for this pattern first and later we will try to understand the pattern with the help of the analogy.

Imagine that your brother is calling you from home and he wants you to buy a new hard drive for his computer since the old one is no longer working. During the telephone conversation , your brother mentions the product code of the old hard drive. Let's say this product code is as long as 16 characters and you need to remember them before your brother hangs up the phone. Well in this situation , assume that you have not got a pen or a pencil with you making the situation even more difficult for you. But luckily a friend of yours is around in your apartment at this moment and you ask him to remember those characters for you.

Once the conversation is over , you would ask your friend those characters and then would probably write it down somewhere for future usage. In this scenario your friend did not really know for what those characters were and you did not give him any information about the conversation either. All you asked your friend was to keep the characters in his memory and give it back to you later when needed.

In "Memento" pattern this is what you would do. Asking another object B(let's say this is your friend) to keep the current state of the object A (let's say this is you) and later on when the object A needs to revert back to it's previous state , it would request object B to provide the needed information which is analogous to your friend telling you back the product code.

In general this is useful in undo operations in a particular process. It is also important to note that the external object (analogous to Object B , or your friend) which keeps track of the state of another object (analogous to Object A , or you) would not be able to see the internal information of the object A. Hence object A's information is not exposed (in other words , encapsulation is not violated). Usually the token which contains the information about the Object A is named as "Memento" and object B would manages such "Memento"s internally.

Well, now it is our time to see this in code,

//This class is analogous to YOU
public class ClassA{
 private String currentProductCode = "";

//this is analogous to asking your friend to keep this code recorded
 public Memento getState(){
  Memento _memento = new Memento();
  return _memento;

//this is analogous to getting the product code from your friend and use it for your purpose
 public void updateState(Memento memento){
  this.currentProductCode = memento.getProductCode();

//This class is analogous to your FRIEND
import java.util.HashMap;
import java.util.Map;

public class ClassB{
 private Map store = new HashMap();

//analogous to your friend recording the product code for you
//may be in a paper
 public void addMemento(Integer sequence , Memento state){,state);
//analogous to the time where you ask your friend the produce code back
 public Memento getMemento(Integer sequence){

//This is the structure which carries the internal information of object A
//usually in the analogy , you could assume this one could be a note book

public class Memento{
 private String productCode;
 public String getProductCode(){
  return this.productCode;
 public void setProducCode(String productCode){
  this.productCode = productCode;

//everything in a main method
public static void main(String[] args){
 //you are telling the product code to your friend here
 ClassA a = new ClassA();
 Memento state = a.getState();

 //your friend is recording the product code
 ClassB b = new ClassB();

 //later , you will get that back from your friend
 Memento memento = b.getMemento(1);



Najma said...

clear explanation Ranil..good article

Ranil said...

nice to know that you guys learn something out from my blog :)

Anonymous said...

byDude. Enough of Patterns.. For a change blog some thing else :)

Ranil said...

haha , let me put something else next time then :)

Going Technical , Prototype pattern

Well this is the year of 2014 and I have not made a single post yet for this year. Frankly I did not have a good real life example for expla...