行为型模式 —— 备忘录模式

备忘录模式

备忘录模式提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤,当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原,很多软件都提供了撤销操作,如word、记事本、idea等软件在编译时按Ctrl+z组合键能撤销当前操作,使文档恢复到之前的状态。

1 定义

又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态

2 结构

备忘录模式的主要角色如下:

  • 发起人角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息
  • 备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人
  • 管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改

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

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

3 实现

游戏挑战boss:游戏中的某个场景,一游戏角色有生命力、攻击力、防御力等数据,在打boss前和后一定会不一样,我们允许玩家如果感觉Boss决斗的效果不理想可以让游戏恢复到决斗之前的状态。
要实现上述案例,有两种实现方式:

  • “白箱”备忘录模式
  • “黑箱”备忘录模式
3.1 “白箱”备忘录模式

备忘录角色对任何对象都提供一个接口,即宽接口,备忘录角色的内部所存储的状态就对所有对象公开。

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

    public RoleStateMemento() {
    }

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

    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 {
    //声明RoleStateMemento对象
    private RoleStateMemento roleStateMemento;

    public RoleStateMemento getRoleStateMemento() {
        return roleStateMemento;
    }

    public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
        this.roleStateMemento = roleStateMemento;
    }
}
//测试类
public class Test {
    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());
        gameRole.fight();//大战boss
        System.out.println("大战boss后...............");
        gameRole.stateDisplay();//展示状态
        System.out.println("恢复状态...............");
        gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());//恢复状态
        gameRole.stateDisplay();//展示状态
    }
}

测试结果:
大战boss前…
角色生命力100
角色攻击力100
角色防御力100
大战boss后…
角色生命力0
角色攻击力0
角色防御力0
恢复状态…
角色生命力100
角色攻击力100
角色防御力100

注意: 白箱备忘录模式是破坏封装性的,但是通过程序员自律,同样可以在一定程度上实现模式的大部分用意。

3.2 “黑箱”备忘录模式

备忘录角色对发起人对象提供一个宽接口,而为其他对象提供一个窄接口,在java语言中,实现双重接口的办法就是将备忘录类设计成发起人类的内部成员类
将RoleStateMemento设为GameRole的内部类,从而将RoleStateMemento对象封装在GameRole里面;在外面提供一个标识接口Memento给RoleStateMemento及其他对象使用。这样GameRole类看到的是RoleStateMemento所有的接口,而RoleStateMemento及其他对象看到的仅仅是标识接口Memento所暴露出来的接口,从而维护了封装性。

窄接口Memento,这是一个标识接口,因此没有定义出任何的方法

//备忘录标识接口,对外提供的窄接口
public interface Memento {
}
//游戏角色类(发起人角色)
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);
    }

    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;
    }
	//私有内部类,备忘录角色类
    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 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 {
    //声明RoleStateMemento对象
    private Memento memento;

    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
//测试类
public class Test {
    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());
        gameRole.fight();//大战boss
        System.out.println("大战boss后...............");
        gameRole.stateDisplay();//展示状态
        System.out.println("恢复状态...............");
        gameRole.recoverState(roleStateCaretaker.getMemento());//恢复状态
        gameRole.stateDisplay();//展示状态
    }
}

测试结果:
大战boss前…
角色生命力100
角色攻击力100
角色防御力100
大战boss后…
角色生命力0
角色攻击力0
角色防御力0
恢复状态…
角色生命力100
角色攻击力100
角色防御力100

4 优缺点

优点:

  • 提供了一种可以恢复状态的机制:当用户需要时能够比较方便地将数据恢复到某个历史的状态
  • 实现了内部状态的封装:除了创建它的发起人之外,其他对象都不能够访问这些状态信息(指“黑箱”备忘录模式)
  • 简化了发起人类:发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则

缺点:

  • 资源消耗大:如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源

5 使用场景

  • 需要保存与恢复数据的场景,如玩游戏的存档功能
  • 需要提供一个可回滚操作的场景,如记事本、idea的组合键Ctrl+z,数据库事务等
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值