1.Part

In this post, I will go over one of the core Design Pattern in Gang of Four. Let’s start with a fundamental definition: Design Patterns. What is a design pattern why we need them?

https://chrisnolan.fandom.com/wiki/Memento?file=Memento_poster.jpg

GoF defines patterns as:

“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” Christopher Alexander

The definition above is from Design Pattern Bible. So those patterns solve problems that occur more than oft in the software development industry. If you have a similar problem you might use your previous solution again. It might require some customization but conceptionally is still quite similar. In quite a long development history programmer used those patterns over and over again then they became certain recipes for its corresponding issue.

You might claim that each problem in a software project individual. Sure each project has its own requirements but the design pattern is over how the data will be structured and so on. Beyond that, it’s more about architecture. There are over 20 patterns in this book. We will focus on a behavioral pattern called MEMENTO.

Memento pattern lets you save and restore the previous state of an object. It makes it possible to undo object previous state. In many cases you could use Memento i.e you are developing a game and your game object has different states according to the occurrence and in some case, you have back to the previous state with its previous specifications. So in this case the Memento pattern could be really helpful. Let’s implement memento in java:

We have a bank account that we transfer money from it. If the account has not sufficient balance then the transfer must be stopped without changing the account state. For that Memento could be a good candidate.

Here is the account class where we invoke the update method and creating

Memento to save object state.

public class Account {

private int amount;

/**
*
*
@param amount that will be transfered
*
@throws NotSufficientBalanceException
*/
public void update(int amount) throws NotSufficientBalanceException{
if ( this.amount + amount < -1000) throw new NotSufficientBalanceException("No enough money");

this.amount +=amount;
}

/**
* creating Memento instance to keep track the State of objectg
*
@return
*/
public Memento create(){
return new Memento(this.amount);
}

public void setMemento(final Memento memento){
this.amount = memento.getAmount();
}
}

Now our Memento should look as below: Sure your object does not have be named a Memento, you may give any name in sense of this pattern.

public class Memento {


private int amount;

/**
* constructor
*
@param amount
*/
public Memento(final int amount) {
this.amount = amount;
}
public int getAmount(){
return this.amount;
}
}

So now we can implement the class where the money transfer takes place and the Memento pattern is visible.

class Transfer {

public static void transfer(Account sender, Account reciever, int amount){
/**
* createing two memento instance to save actual state of account
* for account that want to send money
* and account that recieve money as well.
*/
final Memento senderMemento = sender.create();
final Memento recieverMemento = reciever.create();

try {
reciever.update(amount);
sender.update(-amount);
}catch (Exception e){
/**
* apparently account has no sufficient balance then the exception is thrown.
* time to restore object old state via Memento
*/
sender.setMemento(senderMemento);
reciever.setMemento(recieverMemento);
}
}
}

The next post might be about Command Pattern.

Resources
* Gangs of Four Design Patterns “Design Patterns: Elements of Reusable Object-Oriented Software” book
* https://www.baeldung.com/java-memento-design-pattern
* https://refactoring.guru/design-patterns/memento/java/example
* The source code is based on University of Bremen Software Engineering lecture.

Computer Science And DevOps things :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store