Recently, I implemented some design patterns following this book. This book introduces 23 design patterns that all programmers should know when you write according to OOP instructions. So now I want to write this article about some of these patterns and explain these. All codes that used in this article are put here

What is Memento pattern?

Memento pattern is used for recording past statuses and for recovering susequently. For example when you use text editor such as vim or emacs, do you use undo? Why do you think this undo command can perform? As one way, all statuses(sometimes not all) are recorded as a type of object. Then you can recover this object to use this recovered data in your application. Let’s picking up game example, examine the structure of memento pattern.

Main

import game.Gamer;
import game.Memento;

public class Main {
    public static void main(String[] args) {
    Gamer gamer = new Gamer(100);
    Memento memento = gamer.createMemento();
        for (int i = 0; i < 100; i++) {
            System.out.println("=== " + i);
            System.out.println("Current: " + gamer);
            gamer.bet();
            System.out.println("Money: ¥" + gamer.getMoney());

            if (gamer.getMoney() > memento.getMoney()) {
                System.out.println("Save current state");
                memento = gamer.createMemento();
            } else if (gamer.getMoney() < memento.getMoney() / 2) {
                System.out.println("Restore previous state");
                gamer.restoreMemento(memento);
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
            System.out.println("");
        }
    }
}

On every turn, a gamer bets and gets money or some fruits. If you get more money at the last of game you win. It’s very simple. Please pay attention to line 7. Mement pattern is used there. This memento object is used when gamer can get more money or reduce his money to half of previous one. For saving, call gamer.createMemento() and for restoring, call gamer.restoreMemento(). All concepts that are included in memento pattern are these. It is easy to understand this pattern. Do you think you can write this pattern in your production code tomorrow? Please try it.

As a reference, memento class is also placed here. But it is not core concept of memento pattern. You can arrange this concrete logic for your use if you could only save and restore statuses.

package game;

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

public class Memento {
    int money;
    ArrayList fruits;

    public int getMoney() {
	        return money;
    }

    public Memento(int money) {
        this.money = money;
        this.fruits = new ArrayList();
    }

    void addFruit(String fruit) {
        fruits.add(fruit);
    }

    public List getFruits() {
        return (List)fruits.clone();
    }
}

Is there any unclear stuff? Please look into this codes.

Thank you.