In this post, We will talk and learn about the Memento Design Pattern in Java.
Key Points About Memento Design Pattern :
- Memento design pattern falls under behavioral design pattern.
- This pattern is mainly used when we want to save the state of an object so that we can restore later on.
- Memento pattern is used to implement this in such a way that the saved state data of the object is not accessible outside of the object, this protects the integrity of saved state data.
- Memento pattern mainly uses three actor Classes:
- Memento: It contains the state of an object to be restored.
- Originator: It creates and stores states in Memento objects and
- Caretaker: This object is responsible to restore object state from Memento.
Memento Design Pattern in JDK:
- java.util.Date(the setter methods do that, Date is internally represented by a long value)
- All implementations of java.io.Serializable
Now Let’s move towards the implementation of the Memento Design Pattern.
Below is the complete source code:
CareTaker.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kkjavatutorials.caretaker; import java.util.ArrayList; import java.util.List; import com.kkjavatutorials.memento.Memento; /** * @author KK JavaTutorials *This object is responsible to restore object state from Memento. */ public class CareTaker { private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento state) { mementoList.add(state); } public Memento get(int index) { return mementoList.get(index); } } |
Memento.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkjavatutorials.memento; /** * @author KK JavaTutorials *It contains the state of an object to be restored */ public class Memento { private String state; public Memento(String state) { this.state = state; } public String getState() { return state; } } |
Originator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.kkjavatutorials.originator; import com.kkjavatutorials.memento.Memento; /** * @author KK JavaTutorials *It creates and stores states in Memento objects */ 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(); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kkjavatutorials.client; import com.kkjavatutorials.caretaker.CareTaker; import com.kkjavatutorials.originator.Originator; /** * @author KK JavaTutorials * Client program to use implementaed * Memento Design Pattern */ public class ClientTest { public static void main(String args[]) { //Create an object of Originator Originator originator = new Originator(); //Create an object of CareTaker CareTaker careTaker = new CareTaker(); //Intially setting states in originator originator.setState("State - 1"); originator.setState("State - 2"); //Saving originator states in careTaker careTaker.add(originator.saveStateToMemento()); //adding one more state originator originator.setState("State - 3"); //Save this state in careTaker careTaker.add(originator.saveStateToMemento()); //adding latest state originator originator.setState("State - 4"); System.out.println("Current State is: " + originator.getState()); originator.getStateFromMemento(careTaker.get(0)); System.out.println("First saved State was: " + originator.getState()); originator.getStateFromMemento(careTaker.get(1)); System.out.println("Second saved State was: " + originator.getState()); } } |
Output of this Program:
Current State is: State – 4
First saved State was: State – 2
Second saved State was: State – 3
You May Also Like:
- Visitor Design Pattern in Java
- State Design Pattern in Java
- Command Design Pattern in Java
- Observer Design Pattern in Java
- Strategy Design Pattern in java
- Mediator Design Pattern in Java
- Chain of Responsibility Design Pattern in Java
- Template Design Pattern in Java
- Bridge Design Pattern in Java.
- Flyweight Design Pattern in Java
- Composite Design Pattern in Java
- Proxy Design Pattern in Java
- Facade Design Pattern in Java
- Decorator Design Pattern in Java
- Adapter Design Pattern in Java
- Builder Pattern in Java
- Abstract Factory Pattern in Java
- Factory design pattern in java
- Prototype Pattern in Java
- Singleton Design Pattern in java
That’s all about the Memento Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.