Behavioural Design patterns: Memento
The memento pattern is all about state. You use it when you want to restore the object to its previous state. You have an object, you apply some actions and you are able to revert those actions and get the object in the various states it has been before.
Most of us implement various algorithms and sometimes we do need to evaluate em. Imagine having a program that evaluates trade decisions and you want to go to a previous state and check what would happen to the account balance if you change the formula, and then evaluate the account balance for each step.
The steps of the algorithm and what you should change will definitely vary. The memento pattern will assist us to our mission and help us do some fast in memory evaluation.
We will have a Memento object that contains the account balance. Memento will represent the balance on different phases.
package com.gkatzioura.design.behavioural.memento; public class Memento { private Double balance; public Memento(Double balance) { this.balance = balance; } public Double getBalance() { return balance; } }
Then we will create the originator object. The originator will contain the current state. It can pass it back as a memento when we need to store it. Also we can use a memento object to it restore a certain state.
package com.gkatzioura.design.behavioural.memento; public class Originator { private Double balance; public void setBalance(Double balance) { this.balance = balance; } public Double getBalance() { return balance; } public Memento saveToMemento() { return new Memento(balance); } public void restoreToState(Memento memento) { balance = memento.getBalance(); } }
The last step is the CareTaker object, this will contain the history of our balance. The caretaker will fetch the object state during their various phases.
package com.gkatzioura.design.behavioural.memento; import java.util.ArrayList; import java.util.List; 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); } }
So let’s put them all together.
package com.gkatzioura.design.behavioural.memento; public class MementoMain { public static void main(String[] args) { Double balance = 20.1d; Originator originator = new Originator(); originator.setBalance(balance); CareTaker careTaker = new CareTaker(); careTaker.add(originator.saveToMemento()); /** * Do a transaction */ originator.setBalance(balance-2); careTaker.add(originator.saveToMemento()); /** * Do a transaction */ originator.setBalance(balance+4); careTaker.add(originator.saveToMemento()); System.out.println(careTaker.get(0).getBalance()); System.out.println(careTaker.get(1).getBalance()); System.out.println(careTaker.get(2).getBalance()); } }
You can find the source code on github.
Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Behavioural Design patterns: Memento Opinions expressed by Java Code Geeks contributors are their own. |
You can add bitemporality (https://en.wikipedia.org/wiki/Bitemporal_Modeling) to your memento and store it on the database and you have full fledged history framework that tells you from when to when the information is valid