Showing posts with label memento design pattern. Show all posts
Showing posts with label memento design pattern. Show all posts

Sunday, December 1

Memento Design Pattern

This pattern talks about restoring the original state at any point of time . For example

I start with knowing the mileage of bike around reference value 80KM/h

I store this value in an string instance say : mileage

My reference value / Original value is 80KM/h

Now I start knowing mileage of various bikes and keep updating the the latest known mileage in same string instace :  mileage

current value might change from 80 to 70 to 50 to 40 to 100 and so on  depending upon the mileage of bikes.

So at any point of time If i want to know the reference value , It's no more there is mileage string.

So how do I make sure that I keep on working on current value as well as be able to retrieve the original value at any point of time.

There might be many ways you ca think across , Memento design pattern suggest a standard way to do that.

Pattern revolves around two contributors

Originator
Care Taker

Originator stores the original value in a non-modified way.
Care Taker takes care of returing current or original value at any point of time
Client directly interacts with Care taker to get current or original value


Now lets see how simple is it to implement it in java :


Originator class stores the original value and it can't be modified .







public class Originator {

    private final String mileage ="80";
   
    public String getFinalInstance(){
        return mileage;
    }

}
CareTaker provides method to get to current or original mileage at any point of time


public class CareTaker {

    private String mileage;
    public CareTaker(String instance) {
       this.mileage=instance;
    }

   public String saveCurrentInstance(String instance){
       this.mileage=instance;
       return this.mileage;
   }
  
   public String restoreDefaultInstance(){
       Originator finalInstance=new Originator();
      return  finalInstance.getFinalInstance();
}
   public String getCurrentValue(){
       return  mileage;
   }
}



-->


And here is our Client who interacts with CareTaker to play with current value and reach original value at any point of time.


public class MementoClient {

    public MementoClient() {
        // TODO Auto-generated constructor stub
    }
   
    public static void main(String args[]){
       
       System.out.println("Some original reference value is already set .......");
        CareTaker instance =new CareTaker("50");
        System.out.println("Let us create an instance with current value ::: 50");
        System.out.println("Lets find out the  original reference  value ");
       System.out.println("it is :::" + instance.restoreDefaultInstance());
        System.out.println("and the current value is :: ");
       System.out.println(instance.getCurrentValue());
        System.out.println("Lets change the value to 40");
        instance.saveCurrentInstance("40");
        System.out.println("Now current value is :::  " + instance.getCurrentValue());
        System.out.println("Let's again change the value to :::100");
        instance.saveCurrentInstance("100");
        System.out.println("Now current value is : " + instance.getCurrentValue());
        System.out.println("Now I want to get to the original  value");
        System.out.println("it is ::: " + instance.restoreDefaultInstance());

    }

}







Executing this application as java application will print below logs on console :

===============================
Some original reference value is already set .......
Let us create an instance with current value ::: 50
Lets find out the  original reference  value
it is :::80
and the current value is ::
50
Lets change the value to 40
Now current value is :::  40
Let's again change the value to :::100
Now current value is : 100
Now I want to get to the original  value
it is ::: 80

================================