Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Behavioral Patterns

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.

Game game = new Game();

// playing game... until the player reaches the first checkpoint
Checkpoint checkpoint1 = new Checkpoint(100); // other values...
game.setCheckpoint(checkpoint1);

GameProgressKeeper progressKeeper = new GameProgressKeeper();
progressKeeper.save(checkpoint1);

// continue playing game... until the player reaches the second checkpoint
// but the player died, roll back the game to the last saved checkpoint
progressKeeper.getLastOne();
game.setCheckpoint(checkpoint1);
PreviousVisitorNextInterpreter

Last updated 5 years ago

Was this helpful?