备忘录模式

一,定义

备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态。
这里写图片描述
Originator(发起人):负责创建一个备忘录memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的那些状态

Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象,Originator能看到一个宽接口,允许它访问返回到先前状态所需的所有数据。

Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或者检查。

二,示例

实现一个游戏进度备忘录
这里写图片描述

public class RoleGame {
    //保存游戏进度的备忘录
    private int vit; //生命力
    private int atk; //攻击力
    private int def; //防御力

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

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

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

    //显示状态
    public void displayState(){
        System.out.println("角色当前状态:");
        System.out.println("生命力:" + vit);
        System.out.println("攻击力:" + atk);
        System.out.println("防御力:" + def);
    }

    //战斗
    public void fight(){
        vit = 0;
        atk = 0;
        def = 0;
    }
}

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 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;
    }
}

public class RoleStateCaretaker {
    private RoleStateMemento memento;

    public RoleStateMemento getMemento() {
        return memento;
    }

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

//test main
    //备忘录模式
    public static void mementoModel() {
        //大战boss前
        RoleGame role = new RoleGame();
        System.out.println("战斗前:");
        role.initState();
        role.displayState();
        //保存进度
        RoleStateCaretaker caretaker = new RoleStateCaretaker();
        caretaker.setMemento(role.saveState());
        //大战boss
        System.out.println("战斗后:");
        role.fight();
        role.displayState();
        //恢复状态
        System.out.println("恢复后:");
        role.recoverState(caretaker.getMemento());
        role.displayState();
    }

输出结果:
战斗前:
角色当前状态:
生命力:100
攻击力:100
防御力:100
战斗后:
角色当前状态:
生命力:0
攻击力:0
防御力:0
恢复后:
角色当前状态:
生命力:100
攻击力:100
防御力:100

三,总结

优点:

  • 1,Memento模式,比较适用于功能比较复杂,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento将信息还原到前一状态。
  • 2,使用备忘录模式可以把复杂的对象内部信息对其他的对象屏蔽起来。
  • 3,当角色的状态改变的时候,有可能这个状态无效,这个时候就可以用暂时存储起来的备忘录将状态复原。

缺点:

  • 1,角色状态需要完整存储到备忘录对象,如果状态数据很大很多,那么在资源消耗上,备忘录对象会非常耗内存。

参考:《大话设计模式》

示例源码:https://github.com/wangxp423/ExerciseDesignmodel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值