备忘录模式

当你需要让对象返回之前的状态时(例如,你的用户请求“撤销”),就使用备忘录模式。

示例—游戏进度

由于游戏的难度很高,每个玩家越到后面的关卡越容易死。所以玩家强烈要求提供“储存进度”的命令,好让玩家能够储存游戏进度。这个“储存进度”的功能需要设计成能够抛出一个复活的角色,而进度停留在上一次过关的关卡上。

UML图表示

备忘录模式_第1张图片
备忘录模式-游戏进度

代码演示

游戏记录类

package Memento;

public class GameMemento {

    private int chapter;
    private String roleName;
    private int roleLevel;
    private String time;

    private GameMemento(int chapter, String roleName, int roleLevel, String time){
        this.chapter = chapter;
        this.roleName = roleName;
        this.roleLevel = roleLevel;
        this.time = time;
    }

    static GameMemento savedGameState(int chapter, String roleName, int roleLevel, String time){
        return new GameMemento(chapter, roleName, roleLevel, time);
    }

    public int getChapter() {
        return chapter;
    }

    public String getRoleName() {
        return roleName;
    }

    public int getRoleLevel() {
        return roleLevel;
    }

    public String getTime() {
        return time;
    }
}

游戏管理类

package Memento;

import java.util.Random;

public class MasterGameObject {

    private GameMemento gameState;

    private void savedGameState(){
        gameState = GameMemento.savedGameState(getCurrentChapter(), getCurrentRoleName(), getCurrentRoleLevel(), getCurrentTime());
    }

    public GameMemento getCurrentState(){
        savedGameState();
        System.out.println("Game is saved!!");
        describeCurrentState();
        return gameState;
    }

    public void restoreState(GameMemento gameState){
        this.gameState = gameState;
        System.out.println("Game is restored!!");
        describeCurrentState();
    }

    private int getCurrentChapter(){
        return 11;
    }
    private String getCurrentRoleName(){
        return "Leon";
    }

    private int getCurrentRoleLevel(){
        return 9;
    }

    private String getCurrentTime(){
        return "2010-10-5 22:24:09";
    }

    private void describeCurrentState(){
        if (gameState != null){
            System.out.println("Now Chapter is " + gameState.getChapter() + ", role's name is " + gameState.getRoleName()
            + ", role's level is " + gameState.getRoleLevel() + ", current time is " + gameState.getTime() + ".");
        }
    }

}

玩家类

package Memento;

public class Client {

    private GameMemento gameState;
    private MasterGameObject masterGameObject;

    Client(){
        masterGameObject = new MasterGameObject();
    }


    void gotoNewChapter(){
        gameState = masterGameObject.getCurrentState();
    }

    void playGame(){
        System.out.println("I'm killing monster");
        System.out.println("Oh yeah, I will be 10 Level right away");
    }

    void killed(){
        System.out.println("Ah!Ah!Ah!");
        System.out.println("You dead!! Game Over!!!");
    }

    void restore(){
        System.out.println("I want to restore game!!");
        masterGameObject.restoreState(gameState);
    }

}

测试代码

package Memento;

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

        Client client = new Client();
        client.gotoNewChapter();
        client.playGame();
        client.killed();
        client.restore();

    }
}

测试结果

Game is saved!!
Now Chapter is 11, role's name is Leon, role's level is 9, current time is 2010-10-5 22:24:09.
I'm killing monster
Oh yeah, I will be 10 Level right away
Ah!Ah!Ah!
You dead!! Game Over!!!
I want to restore game!!
Game is restored!!
Now Chapter is 11, role's name is Leon, role's level is 9, current time is 2010-10-5 22:24:09.

你可能感兴趣的:(备忘录模式)