What is Memento Design Pattern

Serra Saracoglu
3 min readNov 12, 2020

Why do we use patterns?

They require us a way to solve problems related to software development with a proven solution.

We will start to talk about design patterns in programming and our first pattern is going to be Memento Pattern.The main aim of the Memento pattern is to save the state of an object and the restore it again if necessary.

If you are asking what is what is state of object? Check this link out before I write whole another paper: https://marcus-biel.com/what-is-an-objects-state/

We can easily define it as a UNDO action like in MS Word or like used in some games which have been saved before, long in short you can use memento pattern when you want to restore your state of object to previous state of the object.

So, how we will do it?

We are gonna have three special classes in this pattern which are they called originator, memento and caretaker.

What originator class do ?What do you think?

Originator class; is going to be our “saver and restorer” of state object.

Caretaker class; will store all states of the object on which it operates and returns these states and adds new states of the object.

Memento class; will store the current state of the object, its state can be read by the Originator class.

memento Pattern

So after all technical and theorical part we need some codes and we have to finish homework actually:) We are gonna write code for undo our move.

Let’s start with the Memento Class!

public class StateMove {
private String move;

public StateMove(String move) {
this.move = move;
}

public StateMove createMove() {
return new StateMove(move);
}

public void restore(StateMove state) {
move = state.getMove();
}

public String getMove() {
return move;
}


}

It stores and returns the current state of the object.

Let’s go on with our caretaker class.

import java.util.ArrayList;
import java.util.List;

public class PrevMove {
private List<StateMove> moves=new ArrayList<>();

public void push(StateMove move){
moves.add(move);
}
public StateMove pop(){
var lastIndex=moves.size()-1;
var lastState=moves.get(lastIndex);
moves.remove(lastState);
return lastState;
}
}

Here is our originator class…

public class Move {
private String move;

public String getMove() {
return move;
}

public void setMove(String move) {
this.move = move;
}
public StateMove createMove(){
return new StateMove(move);
}
public void restore(StateMove state){
move=state.getMove();
}
}

And we can’t forget our Main class never ever of course.

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

Move move = new Move(); //move object created
PrevMove prevMove = new PrevMove(); //previous move object also ceated

move.setMove("Be Bad Guy"); //set move comes from originator it gets move object
//move.setMove("Make Him Cry");
prevMove.push(move.createMove()); //return state object

move.setMove("Apologize");
prevMove.push(move.createMove());

move.setMove("Smile");
System.out.println("Current State: " + move.getMove());

move.restore(prevMove.pop()); //what stored in the previous mode at above
System.out.println("First saved State: " + move.getMove());
move.restore(prevMove.pop());
System.out.println("Second saved State: " + move.getMove());

}
}
design Patterns

I hope it was explaining enough you to understand the basic structure.

I want to keep update with other main design patterns as well.

See you in next pattern.

Byee!

--

--