22_备忘录模式

2021-01-21  本文已影响0人  真是个点子王

结构

备忘录有两个等效的接口:

  • 窄接口:管理者(Caretaker)对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口(narror Interface),这个窄接口只允许他把备忘录对象传给其他的对象。
  • 宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口(wide Interface),这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。

案例

白箱备忘录模式

// 游戏角色类(发起人角色)
// GameRole.java
public class GameRole {

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

    // 初始化状态
    public void initState(){
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    // 战斗的方法
    public void fight(){
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

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

    // 恢复角色状态
    public void recoverState(RoleStateMemento roleStateMemento){
        // 将备忘录对象中存储的状态赋值给当前对象的成员
        this.vit = roleStateMemento.getVit();
        this.atk = roleStateMemento.getAtk();
        this.def = roleStateMemento.getDef();
    }

    // 展示状态功能
    public void stateDisplay(){
        System.out.println("角色生命力:" + vit);
        System.out.println("角色攻击力:" + atk);
        System.out.println("角色防御力:" + def);
    }
}

// 备忘录角色类
// RoleStateMemento.java
public class RoleStateMemento {
    private int vit; // 生命力
    private int atk; // 攻击力
    private int def; // 防御力

    public RoleStateMemento(int vit, int atk, int def) {
        this.vit = vit;
        this.atk = atk;
        this.def = def;
    }

    public RoleStateMemento() {
    }

    public int getVit() {
        return vit;
    }

    public void setVit(int vit) {
        this.vit = vit;
    }

    public int getAtk() {
        return atk;
    }

    public void setAtk(int atk) {
        this.atk = atk;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }
}

// 备忘录对象管理角色
// RoleStateCaretaker.java
public class RoleStateCaretaker {
    // 声明RoleStateMemento类型的变量
    private RoleStateMemento roleStateMemento;

    public void setRoleStateMemento(RoleStateMemento roleStateMemento){
        this.roleStateMemento = roleStateMemento;
    }

    public RoleStateMemento getRoleStateMemento() {
        return roleStateMemento;
    }
}

// Client.java
public class Client {
    public static void main(String[] args) {
        System.out.println("--------- 大战boss前 ----------");
        // 创建一个游戏角色对象
        GameRole gameRole = new GameRole();
        gameRole.initState();   // 初始化状态操作
        gameRole.stateDisplay();

        // 将该游戏角色内部状态进行备份
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setRoleStateMemento(gameRole.saveState());

        System.out.println("--------- 大战boss后 ----------");
        // 损耗严重
        gameRole.fight();
        gameRole.stateDisplay();

        System.out.println("--------- 恢复状态 ----------");
        gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());

        gameRole.stateDisplay();
    }
}

黑箱备忘录模式

// 备忘录接口,对外提供的窄接口
// Memento.java
public interface Memento {
}

// 游戏角色类(发起人角色)
// GameRole.java
public class GameRole {

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

    // 初始化状态
    public void initState(){
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    // 战斗的方法
    public void fight(){
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

    // 保存状态功能
    public Memento saveState(){
        return new RoleStateMemento(vit,atk,def);
    }

    // 恢复角色状态
    public void recoverState(Memento memento){
        RoleStateMemento roleStateMemento = (RoleStateMemento)memento;
        // 将备忘录对象中存储的状态赋值给当前对象的成员
        this.vit = roleStateMemento.getVit();
        this.atk = roleStateMemento.getAtk();
        this.def = roleStateMemento.getDef();
    }

    // 展示状态功能
    public void stateDisplay(){
        System.out.println("角色生命力:" + vit);
        System.out.println("角色攻击力:" + atk);
        System.out.println("角色防御力:" + def);
    }

    private class RoleStateMemento implements Memento{
        private int vit; // 生命力
        private int atk; // 攻击力
        private int def; // 防御力

        public RoleStateMemento(int vit, int atk, int def) {
            this.vit = vit;
            this.atk = atk;
            this.def = def;
        }

        public RoleStateMemento() {
        }

        public int getVit() {
            return vit;
        }

        public void setVit(int vit) {
            this.vit = vit;
        }

        public int getAtk() {
            return atk;
        }

        public void setAtk(int atk) {
            this.atk = atk;
        }

        public int getDef() {
            return def;
        }

        public void setDef(int def) {
            this.def = def;
        }
    }
}

// 备忘录对象管理对象
// RoleStateCaretaker.java
public class RoleStateCaretaker {
    private Memento memento;

    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}

// Client.java
public class Client {
    public static void main(String[] args) {
        System.out.println("--------- 大战boss前 ----------");
        // 创建一个游戏角色对象
        GameRole gameRole = new GameRole();
        gameRole.initState();   // 初始化状态操作
        gameRole.stateDisplay();

        // 将该游戏角色内部状态进行备份
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setMemento(gameRole.saveState());

        System.out.println("--------- 大战boss后 ----------");
        // 损耗严重
        gameRole.fight();
        gameRole.stateDisplay();

        System.out.println("--------- 恢复状态 ----------");
        gameRole.recoverState(roleStateCaretaker.getMemento());

        gameRole.stateDisplay();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读