Lying design pattern notes (xv) の memo mode

For chestnuts

Problem Description

Keep playing the game progress.

Simple implementation

GameRole

/**
 * 游戏角色
 * Created by callmeDevil on 2019/8/11.
 */
public class GameRole {

    // 生命力
    private int vit;
    // 攻击力
    private int atk;
    // 防御力
    private int def;

    // 状态显示
    public void stateDisplay() {
        System.out.println("角色当前状态:");
        System.out.println(String.format("体力:%s", this.vit));
        System.out.println(String.format("攻击力:%s", this.atk));
        System.out.println(String.format("防御力:%s", this.def));
        System.out.println();
    }

    //  获得初始状态
    public void getInitState() {
        // 数据通常来自本机磁盘或远程数据库
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    // 战斗
    public void fight(){
        // 在与Boss大战后游戏数据损耗为0
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

    // 省略 get set
    
}

test

public class Test {
    public static void main(String[] args) {
        // 大战Boss前
        GameRole lufi = new GameRole();
        // 获得初始角色状态
        lufi.getInitState();
        lufi.stateDisplay();
        // 通过“游戏角色”新实例,保存进度
        GameRole backup = new GameRole();
        backup.setVit(lufi.getVit());
        backup.setAtk(lufi.getAtk());
        backup.setDef(lufi.getDef());
        // 大战Boss时,损耗严重,全部为0
        lufi.fight();
        lufi.stateDisplay();
        // GameOver不甘心,恢复进度,重新玩过
        lufi.setVit(backup.getVit());
        lufi.setAtk(backup.getAtk());
        lufi.setDef(backup.getDef());
        lufi.stateDisplay();
    }
}

Test Results

角色当前状态:
体力:100
攻击力:100
防御力:100

角色当前状态:
体力:0
攻击力:0
防御力:0

角色当前状态:
体力:100
攻击力:100
防御力:100

There is a problem

In this client calls, the details of the whole game character exposed, too much responsibility, we need to know the vitality of game characters, attack power, defense power of these details, but also for backup. If you later need to increase the "mana" or modify an existing force of some kind, that this part of the code needs to be modified, the same recovery time is the same problem.

Memento Pattern

definition

Without violating encapsulation, capture and the internal state of an object, and save the state outside the object. So that later you can restore the object to a previously saved state.

UML diagrams

Code

GameRole

/**
 * 游戏角色
 * Created by callmeDevil on 2019/8/11.
 */
public class GameRole {

    // 属性与简单实现GameRole相同

    // 保存角色状态
    public RoleStateMemento saveState() {
        return new RoleStateMemento(vit, atk, def);
    }

    // 恢复角色状态
    public void recoveryState(RoleStateMemento memento) {
        this.vit = memento.getVit();
        this.atk = memento.getAtk();
        this.def = memento.getDef();
    }

    // 其余方法与简单实现相同
    
}

RoleStateMemento

/**
 * 角色状态存储类
 * Created by callmeDevil on 2019/8/11.
 */
public class RoleStateMemento {

    // 属性与 简单实现 GameRole 相同

    // 将生命力、攻击力、防御力存入状态存储箱对象中
    public RoleStateMemento(int vit, int atk, int def){
        this.vit = vit;
        this.atk = atk;
        this.def = def;
    }

    // 省略 get set

}

RoleStateCaretaker

/**
 * 游戏状态管理者
 * Created by callmeDevil on 2019/8/11.
 */
public class RoleStateCaretaker {

    private RoleStateMemento memento;

    // 省略 get set

}

test

public class Test {
    public static void main(String[] args) {
        // 大战Boss前
        GameRole lufi = new GameRole();
        lufi.getInitState();
        lufi.stateDisplay();
        // 保存游戏进度
        RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
        stateAdmin.setMemento(lufi.saveState());// 将具体数据封装在了 Memento中
        // 大战Boss时,损耗严重
        lufi.fight();
        lufi.stateDisplay();
        // 恢复状态
        lufi.recoveryState(stateAdmin.getMemento());
        lufi.stateDisplay();
    }
}

Test Results

Simple to achieve the same

to sum up

  • To save the details to the package in the Memento, the day you want to save the changes do not affect the details of the client.
  • Memo mode is more suitable for more complex functions, but need to maintain property records or history classes, or just when the need to preserve the property a few of the attributes, Originator can revert to a previous state based on the stored information Memento.
  • If the command mode used in a system, the need to implement an undo function commands, the command mode can be used to store state memo mode undoable operations.
  • Use memo complex objects can be internal information other objects shielded.

Guess you like

Origin www.cnblogs.com/call-me-devil/p/11335226.html