Memento
Memento patter helps us to persist state of application in given time, so we can easily roll back or traverse through history of changes.
Example - Save game progress
We are going to use memento pattern to save game whenever user reaches a checkpoint.
class Game {
private Checkpoint checkpoint;
public void setCheckpoint(Checkpoint checkpoint) {
this.checkpoint = checkpoint;
}
}
class Checkpoint implements Cloneable {
private int exp;
public Checkpoint(int exp) {
this.exp = exp;
}
public Checkpoint clone() {
return new Checkpoint(exp);
}
}
class GameProgressKeeper {
private List<Checkpoint> checkpoints = new ArrayList<>();
public void save(Checkpoint checkpoint) {
checkpoints.add(checkpoint.clone());
}
public Checkpoint getLastOne() {
return checkpoints.get(checkpoints.size() - 1);
}
}Here is how game could be played and what would have to happen in order to persist or roll back game state.
Last updated
Was this helpful?