Why we Should Use Memento Design Pattern in Java

Posted By : Pavan Kumar | 30-Nov-2018

In this blog, I'm going to tell you that why and when we should use the memento design pattern.

Memento Design Pattern:-

Memento design pattern comes under the Behavioural design patterns of java.
Memento pattern is used to restore the previous state of your object. suppose your application is progressing and you want to save checkpoints in your application and restore back to those checkpoints later.

It has 3 major components which are detailed below -

  1.  Originator:- Originator is an object for which you want to save the state. It creates the memento and uses it in future to restore the state.
  2.  Memento:-   It is used to maintain the state of Originator. It's Just POJO implementation.
  3.  Caretaker:-   Caretaker is used to keep track of multiple memento to maintain checkpoints.

Advantages of Memento design pattern:-

 We can use Serialization approach to achieve memento design pattern implementation which is more generic rather than Memento design pattern where every object needs to have its own Memento class implementation.

 

Disadvantage of Memento Pattern:-

If Originator Object is large then the size of memento object will also be large and it will use a lot of memory. 

 

Let's Take a look at the implementation of code:-

Firstly we will create a Memento class in which we have a State as String field.

Memento.java

public class Memento {
    private String state;

    public Memento(String state){
        this.state = state;
    }

    public String getState(){
        return state;
    }
}

 

Now we will create an Originator class.

Originator.java

public class Originator {
    private String state;

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

    public String getState(){
        return state;
    }

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

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

 

Now we will create a CareTaker class which is used to keep track of mementos

CareTaker.java

public class CareTaker {
    private List mementoList = new ArrayList();

    public void add(Memento state){
        mementoList.add(state);
    }

    public Memento get(int index){
        return mementoList.get(index);
    }
}

 

Finally , we will create a test class which will store the object's state and restore it to a specific state or checkpoint.

MementoPatternDemo.java

public class MementoPatternTest {
    public static void main(String[] args) {

        Originator originator = new Originator();
        CareTaker careTaker = new CareTaker();

        originator.setState("State ONE");
        originator.setState("State TWO");
        careTaker.add(originator.saveStateToMemento());

        originator.setState("State THREE");
        careTaker.add(originator.saveStateToMemento());

        originator.setState("State FOUR");
        System.out.println("Current State: " + originator.getState());

        originator.getStateFromMemento(careTaker.get(0));
        System.out.println("First saved State: " + originator.getState());
        originator.getStateFromMemento(careTaker.get(1));
        System.out.println("Second saved State: " + originator.getState());
    }
}


Thanks

About Author

Author Image
Pavan Kumar

Pavan is a bright Java developer. He is a learner by heart and has a passion and profile to adapt various technologies.

Request for Proposal

Name is required

Comment is required

Sending message..