• 第二十一章·备忘录模式


    一、备忘录模式概述

    定义:
    备忘录模式:在不破坏封装的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。

    备忘录模式提供来一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤,当新的状态无效或者存在问题时,可以使用暂存起来的备忘录将状态复原,当前很多软件所提供的撤销操作中就使用了备忘录模式。

    二、备忘录模式的结构和实现

    2.1 备忘录模式的结构

    备忘录模式包含以下3个角色:

    1. Originator(原发器):一个普通的类,它通过创建一个备忘录来存储当前内部状态,也可以使用备忘录来恢复其内部状态;
    2. Memento(备忘录):用于存储原发器的内部状态;
    3. Caretaker(负责人):负责保存备忘录,但是不能对备忘录的内容进行操作或检查。

    2.2 备忘录模式的实现

    //原发器

    /**
     * 象棋类,充当原发器
     */
    public class Chessman {
    
        private String label;
        private int x;
        private int y;
    
        public Chessman(String label,int x,int y){
            this.label =label;
            this.x = x;
            this.y = y;
        }
    
        //保存备忘
        public ChessmanMemento save(){
            return new ChessmanMemento(this);
        }
    
        //撤销操作
        public void restore(ChessmanMemento memento){
            this.label = memento.getLabel();
            this.x = memento.getX();
            this.y = memento.getY();
        }
    
        public String getLabel() {
            return label;
        }
    
        public void setLabel(String label) {
            this.label = label;
        }
    
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    //备忘录

    /**
     * 象棋备忘录,充当备忘录角色
     */
    public class ChessmanMemento {
        private String label;
        private int x;
        private int y;
    
        public ChessmanMemento(Chessman chessman){
            this.label = chessman.getLabel();
            this.x = chessman.getX();
            this.y = chessman.getY();
        }
    
        public String getLabel() {
            return label;
        }
    
        public void setLabel(String label) {
            this.label = label;
        }
    
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    //负责人

    /**
     * 负责人角色
     */
    public class MementoCaretaker {
    
        private ChessmanMemento chessmanMemento;
    
        public ChessmanMemento getChessmanMemento() {
            return chessmanMemento;
        }
    
        public void setChessmanMemento(ChessmanMemento chessmanMemento) {
            this.chessmanMemento = chessmanMemento;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    //客户端

    public class Client {
    
        public static void main(String[] args) {
    
            /**
             * 案例需求描述:
             * 在一款运行在Android平台的触碰式中国象棋软件,有些用户经常走错棋或误操作,
             * 因此提供一个"悔棋"功能,用户走错棋之后可以恢复到前一个步骤。
             * 请使用备忘录模式实现此功能。
             *
             */
    
            MementoCaretaker mc = new MementoCaretaker();
    
            Chessman chessman = new Chessman("车", 1, 1);
            display(chessman);
            mc.setChessmanMemento(chessman.save());//保存状态,下棋之前
            chessman.setX(3);
            display(chessman);
            mc.setChessmanMemento(chessman.save());//保存状态,下棋之前
            chessman.setY(5);
            display(chessman);
            System.out.println("*****悔棋*****");
            chessman.restore(mc.getChessmanMemento());
            display(chessman);
    
        }
    
        public static void display(Chessman chessman) {
            System.out.println("棋子:" + chessman.getLabel() + "当前为止为:第" + chessman.getX() + "行,第" + chessman.getY() + "列");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    三、备忘录模式的优缺点和适用环境

    3.1 备忘录模式的优点

    1. 提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤;
    2. 还实现了对信息的封装

    3.2 备忘录模式的缺点

    1. 如果需要保存的原发器类的成员变量太多,就不可避免的占用大量的存储资源。

    3.3 备忘录模式的适用环境

    1. 保存一个对象在某一个时刻的全部状态或部分状态,这样以后需要时它能够恢复到先前的状态,实现撤销操作;
    2. 防止外界对象破坏一个对象历史状态的封装性,避免将对象历史状态的实现细节暴露给外界对象。

    【参考文献】:
    本文是根据刘伟的《Java设计模式》一书的学习笔记,仅供学习用途,勿做其他用途,请尊重知识产权。

    【本文代码仓库】:https://gitee.com/xiongbomy/java-design-pattern.git

  • 相关阅读:
    【AndroidStudio旧版本BUG问题】完美解决运行报错问题Invalid keystore format
    消息队列的模拟实现(二)
    xss跨站脚本攻击
    hadoop大数据原理与应用------初识Hadoop数据集
    记录 | docker权限原因导致service ssh start失败
    Linux面试题
    实现 表格 隔行变色 案例
    爬虫学习日记
    15.利用webpack搭建server本地服务
    【ESP32调试-快速入门】
  • 原文地址:https://blog.csdn.net/weixin_44143114/article/details/126533112