This is my blog , it could contain various topics from my own thoughts to computer programming ....

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.

//analogous to your friend recording the product code for you
//may be in a paper
public void addMemento(Integer sequence , Memento state){
this.store.put(sequence,state);
}
//analogous to the time where you ask your friend the produce code back
public Memento getMemento(Integer sequence){
return this.store.get(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();