• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于java:设计模式学习21Java实现备忘录模式

java 搞代码 3年前 (2022-01-28) 38次浏览 已收录 0个评论
文章目录[隐藏]

写在后面

  • 记录学习设计模式的笔记
  • 进步对设计模式的灵活运用

学习地址

https://www.bilibili.com/vide…

https://www.bilibili.com/vide…

参考文章

http://c.biancheng.net/view/1…

我的项目源码
https://gitee.com/zhuang-kang/DesignPattern

23,备忘录模式

23.1 中介者模式的定义和特点

备忘录(Memento)模式的定义:在不毁坏封装性的前提下,捕捉一个对象的外部状态,并在该对象之外保留这个状态,以便当前当须要时能将该对象复原到原先保留的状态。该模式又叫快照模式。

备忘录模式是一种对象行为型模式,其次要长处如下。

  • 提供了一种能够复原状态的机制。当用户须要时可能比拟不便地将数据恢复到某个历史的状态。
  • 实现了外部状态的封装。除了创立它的发起人之外,其余对象都不可能拜访这些状态信息。
  • 简化了发动人类。发起人不须要治理和保留其外部状态的各个备份,所有状态信息都保留在备忘录中,并由管理者进行治理,这合乎繁多职责准则。

其次要毛病是:

  • 资源耗费大。如果要保留的外部状态信息过多或者特地频繁,将会占用比拟大的内存资源。

23.2 备忘录模式的构造与实现

23.2.1 备忘录模式的构造

  1. 发起人(Originator)角色:记录以后时刻的外部状态信息,提供创立备忘录和复原备忘录数据的性能,实现其余业务性能,它能够拜访备忘录里的所有信息。
  2. 备忘录(Memento)角色:负责存储发起人的外部状态,在须要的时候提供这些外部状态给发起人。
  3. 管理者(Caretaker)角色:对备忘录进行治理,提供保留与获取备忘录的性能,但其不能对备忘录的内容进行拜访与批改。
  4. 备忘录有两个等效的接口:

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

23.2.2 代码实现

游戏中的某个场景,一游戏角色有生命力、攻击力、防御力等数据,在打Boss前和后肯定会不一样的,咱们容许玩家如果感觉与Boss决斗的成果不现实能够让游戏复原到决斗之前的状态。

要实现上述案例,有两种形式:

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

23.2.2.1 白箱备忘录模式

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

关系类图

GameRole

<code class="java">package com.zhuang.memento.white_box;

/**
 * @Classname GameRole
 * @Description 游戏角色类
 * @Date 2021/3/29 10:14
 * @Created by dell
 */

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

RoleStateMemento

<code class="java">package com.zhuang.memento.white_box;

/**
 * @Classname RoleStateMemento
 * @Description 游戏状态存储类 备忘录类
 * @Date 2021/3/29 10:14
 * @Created by dell
 */

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

RoleStateCaretaker

<code class="java">package com.zhuang.memento.white_box;

/**
 * @Classname RoleStateCaretaker
 * @Description 角色状态治理类
 * @Date 2021/3/29 10:15
 * @Created by dell
 */

public class RoleStateCaretaker {
    private RoleStateMemento roleStateMemento;

    public RoleStateMemento getRoleStateMemento() {
        return roleStateMemento;
    }

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

Client

<code class="java">package com.zhuang.memento.white_box;

/**
 * @Classname Client
 * @Description 备忘录模式 白箱 测试类
 * @Date 2021/3/29 10:15
 * @Created by dell
 */

public class Client {
    public static void main(String[] args) {
        System.out.println("--------------大战Boss前------------------------");
        //大战Boss前
        GameRole gameRole = new GameRole();
        gameRole.initState();
        gameRole.stateDisplay();

        //保留进度
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setRoleStateMemento(gameRole.saveState());

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

        System.out.println("--------------满血复活------------------------");
        gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
        gameRole.stateDisplay();
    }
}

23.2.2.2 黑箱备忘录模式

备忘录角色对发起人对象提供一个宽接口,而为其余对象提供一个窄接口。在Java语言中,实现双重接口的方法就是将备忘录类设计成发动人类的外部成员类。

RoleStateMemento 设为 GameRole 的外部类,从而将 RoleStateMemento 对象封装在 GameRole 外面;在里面提供一个标识接口 MementoRoleStateCaretaker 及其他对象应用。这样 GameRole 类看到的是 RoleStateMemento 所有的接口,而RoleStateCaretaker 及其他对象看到的仅仅是标识接口 Memento 所裸露进去的接口,从而保护了封装型。类图如下:

GameRole

<code class="java">package com.zhuang.memento.black_box;


import com.zhuang.memento.white_box.RoleStateMemento;

/**
 * @Classname GameRole
 * @Description 游戏角色类
 * @Date 2021/3/29 10:14
 * @Created by dell
 */

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

    //在外部定义备忘录外部类 RoleStateMemento(该外部类设置为公有的)

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

Memento

<code class="java">package com.zhuang.memento.black_box;

/**
 * @Classname Memento
 * @Description 窄接口
 * @Date 2021/3/29 10:40
 * @Created by dell
 */

public interface Memento {
}

RoleStateCaretaker

<code class="java">package com.zhuang.memento.black_box;


/**
 * @Classname RoleStateCaretaker
 * @Description 角色状态治理类
 * @Date 2021/3/29 10:15
 * @Created by dell
 */

public class RoleStateCaretaker {
    private Memento memento;

    public Memento getMemento() {
        return memento;
    }

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

Client

<code class="java">package com.zhuang.memento.black_box;

/**
 * @Classname Client
 * @Description 备忘录模式 黑箱 测试类
 * @Date 2021/3/29 10:15
 * @Created by dell
 */

public class Client {
    public static void main(String[] args) {
        System.out.println("--------------大战Boss前------------------------");
        //大战Boss前
        GameRole gameRole = new GameRole();
        gameRole.initState();
        gameRole.stateDisplay();

        //保留进度
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setMemento(gameRole.saveState());

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

        System.out.println("--------------满血复活------------------------");
        gameRole.recoverState(roleStateCaretaker.getMemento());
        gameRole.stateDisplay();
    }
}

23.3 备忘录模式利用场景

  • 须要保留与复原数据的场景,如玩游戏时的两头后果的存档性能。
  • 须要提供一个可回滚操作的场景,如 Word、记事本、Photoshop,idea等软件在编辑时按 Ctrl+Z 组合键,还有数据库中事务操作。

23.4 备忘录模式的注意事项和细节

  • 给用户提供一种能够复原的机制,能够使用户可能比拟不便回到某个历史的状态
  • 实现了信息的封装,使用户不须要关怀保留细节
  • 类成员变量过多,占用比拟大的资源,每一次保留都会耗费肯定内存

写在最初

  • 如果我的文章对你有用,请给我点个👍,感激你😊!
  • 有问题,欢送在评论区指出!💪

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于java:设计模式学习21Java实现备忘录模式

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址