设计模式(备忘录)

文章介绍了备忘录模式,如何通过保存对象状态快照实现撤销功能,同时讨论了其优点(简化原发器代码)和缺点(内存消耗、生命周期管理),并展示了在Java中具体应用的例子。
摘要由CSDN通过智能技术生成

备忘录模式(快照)

  平时所用到的撤销功能
  将一个对象的状态保存至一个临时容器中
  如需撤销时从容器中取出

优点

  • 在不破坏对象封装情况的前提下创建对象状态快照。
  • 通过让负责人维护原发器状态历史记录来简化原发器代码。

缺点

  • 过于频繁地创建备忘录, 程序将消耗大量内存。
  • 必须完整跟踪原发器的生命周期, 这样才能销毁弃用的备忘录。
  • 一些语言不能确保备忘录中的状态不被修改。
	static class MyLife {
        int age,money;
        boolean marry;

        public MyLife() {
            this.age = 0;
            this.marry = false;
            this.money = 0;
            System.out.println("角色新建完毕");
        }

        /*
         * 十年之后,模拟属性变化
         * */
        public void tenYearAfter() {
            this.age += 10;
            if (this.age >= 20) {
                this.marry = true;
                this.money = new Random().nextInt(1000000000);
            }
        }

        /*
        * 展示当前信息
        * */
        void showLife() {
            System.out.println(String.format(
                    "今年%d岁,%s,%s",
                    this.age,
                    this.marry ? "婚姻美满" : "尚未结婚",
                    this.marry ? "资产" + this.money + "元" : "还没工作"
            ));
        }

        /*
        * 保存节点,通过窄接口保护属性安全
        * */
        private Memento saveLifeNode() {
            return new TempLife(this.age, this.marry, this.money);
        }

        /*
        * 从备忘录中取出快照恢复属性*/
        private void regret(Memento memento) {
            TempLife tempLife = (TempLife) memento;
            this.age = tempLife.age;
            this.marry = tempLife.marry;
            this.money = tempLife.money;
        }

        /*
        * 将临时节点设为对象的内部类,以此保护属性安全
        * */
        class TempLife implements Memento {
            int age;
            boolean marry;
            int money;

            public TempLife(int age, boolean marry, int money) {
                this.age = age;
                this.marry = marry;
                this.money = money;
            }
        }
    }

窄接口,保护快照属性安全

    interface Memento {
    }

备忘录管理

    static class MementoCaretakers {
        Memento memento;
        public Memento getMemento() {
            return memento;
        }
        public void setMemento(Memento memento) {
            this.memento = memento;
        }
    }

结果

    public static void main(String[] args) {

        MyLife myLife = new MyLife();
        MementoCaretakers caretakers = new MementoCaretakers();

        // 保存节点状态, 快照
        caretakers.setMemento(myLife.saveLifeNode());

        // 十年之后
        myLife.tenYearAfter();
        myLife.showLife(); // 今年10岁,尚未结婚,还没工作

        // 再过十年
        myLife.tenYearAfter();
        myLife.showLife(); // 今年20岁,婚姻美满,资产12元

        // 从备忘录中获取初始状态重新开始
        myLife.regret(caretakers.getMemento());
        myLife.showLife();// 今年0岁,尚未结婚,还没工作
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值