Java Behavioral Design Patterns – Memento Design Pattern
In this article, we will cover one Memento Design Pattern which is categorized under behavioral design patterns. This pattern provides the capability to provide a restore (like undo) functionality over an object.
The memento pattern operates on a single object. It will record the state of the object by protecting objects internal structure or by protecting encapsulation property. It is like an UNDO mechanism which will be useful to recover from errors. Memento pattern supports to save the object state information so one can restore it in any case of errors. You can opt for this design pattern if your application:
- Needs to save the state of an object externally and you need to restore or rollback functionality
- The objects follow strict encapsulations
<Here are ALL other Java Design Patterns, explained in detail with examples>
Memento Design Pattern by Example
You need to provide 3 conceptual objects in order to successfully implement this pattern: Originator, Caretaker, and Memento
Originator
An originator is an object that has an internal state. It uses the memento to save and restore its internal state.
Caretaker
It never operates on the contents of a memento and it not even check the contents. It holds the memento object and is responsible for objects safekeeping. To restore the previous state, it returns the memento object to the originator.
Memento
A memento is an object that holds state information.
Let us go through a simple code. We will assume a simple game example. In this, if a user completes one round then it will be saved and he can start with next round. If a user fails to complete a round again they need to attempt previous round.
Memento Class
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Memento { private String state; public Memento(String state) { this.state = state; } public String getState() { return state; } } |
Originator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Originator { private String state; public void setState(String state) { System.out.println("Originator: Starting with : " + state); this.state = state; } public Memento saveState() { System.out.println("Originator: Completed :" + state); return new Memento(state); } public void restoreState(Memento m) { state = m.getState(); System.out.println("Originator: Now player need to start with " + state); } } |
Caretaker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Caretaker { static int cnt=1; private ArrayList<Memento> mementos = new ArrayList<>(); public void addToMemento(Memento m) { mementos.add(m); System.out.println("Round-" +cnt++ +" added to Memento\n"); } public Memento getMemento() { int lastSavedState = ((mementos.size() - 1) < 0) ? 0 : (mementos.size() - 1); Memento mem = mementos.get(lastSavedState); //remove last restored state. mementos.remove(lastSavedState); return mem; } } |
And here is the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Originator: Starting with : Round-1 Originator: Completed :Round-1 Round-1 added to Memento Originator: Starting with : Round-2 Originator: Completed :Round-2 Round-2 added to Memento Originator: Starting with : Round-3 Originator: Completed :Round-3 Round-3 added to Memento Originator: Starting with : Round-4 At this stage player fails Originator: Now player need to start with Round-3 |
Here we are simply mimicking that game is going on and states are saved automatically.
Conclusion
The Memento pattern is useful when there is a need to provide a Rollback mechanism in your program, so by using this pattern we can provide store and restore capability to an object.
You can refer complete code from our Git
Download Code
<Here are ALL other Java Design Patterns, explained in detail with examples>