• 备忘录模式


    一、定义

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

    二、用处和缺点

    用处:适用于功能比较复杂,但需要维护或记录属性历史的类。可以根据保存的memento信息还原到前一个状态。

    缺点:角色状态需要存储到另一个备忘录对象中,在资源消耗上,内存消耗较大。

    三、示例代码

    /*发起人类 orginator*/
    public class GameCharacter {
    
        private double bloodVolume;
        private double blueQuantity;
        private double experience;
    
        public double getBloodVolume() {
            return bloodVolume;
        }
    
        public void setBloodVolume(double bloodVolume) {
            this.bloodVolume = bloodVolume;
        }
    
        public double getBlueQuantity() {
            return blueQuantity;
        }
    
        public void setBlueQuantity(double blueQuantity) {
            this.blueQuantity = blueQuantity;
        }
    
        public double getExperience() {
            return experience;
        }
    
        public void setExperience(double experience) {
            this.experience = experience;
        }
    
        public void hitBoos() {
            this.bloodVolume = 0;
            this.blueQuantity = 0;
            this.experience = 0;
        }
    
        public void displayState() {
            System.out.println("bloodVolume:" + String.valueOf(bloodVolume));
            System.out.println("blueQuantity:" + String.valueOf(blueQuantity));
            System.out.println("experience:" + String.valueOf(experience));
            System.out.println("----------------------");
        }
    
        public void initCharacter(double bloodVolume, double blueQuantity, double experience) {
            this.bloodVolume = bloodVolume;
            this.blueQuantity = blueQuantity;
            this.experience = experience;
        }
    
        /*恢复备份*/
        public void recovery(StateStore stateStore) {
            this.bloodVolume = stateStore.getBloodVolume();
            this.blueQuantity = stateStore.getBlueQuantity();
            this.experience = stateStore.getExperience();
    
        }
    
        /*创建备份*/
        public StateStore backups() {
    
            StateStore stateStore = new StateStore(this.bloodVolume, this.blueQuantity, this.experience);
            return stateStore;
        }
    
    }
    
    /*备忘录类*/
    public class StateStore {
    
        private double bloodVolume;
        private double blueQuantity;
        private double experience;
    
        public StateStore(double bloodVolume, double blueQuantity, double experience) {
            this.bloodVolume = bloodVolume;
            this.blueQuantity = blueQuantity;
            this.experience = experience;
        }
    
        public double getBloodVolume() {
            return bloodVolume;
        }
    
    
        public double getBlueQuantity() {
            return blueQuantity;
        }
    
        public double getExperience() {
            return experience;
        }
    
    }
    
    /*备忘录管理类*/
    public class StorageManager {
    
        StateStore stateStore;
    
        public StateStore getStateStore() {
            return stateStore;
        }
    
        public void setStateStore(StateStore stateStore) {
            this.stateStore = stateStore;
        }
    }
    
    /*客户端*/
    public class Client {
    
        public static void main(String[] args) {
    
            GameCharacter yasuo=new GameCharacter();
            yasuo.initCharacter(100,100,100);
            yasuo.displayState();
    
            /*设置好备份*/
            StateStore stateStore=yasuo.backups();
    
            StorageManager storageManager=new StorageManager();
            storageManager.setStateStore(stateStore);
    
            yasuo.hitBoos();
    
            /*回滚到备份数据*/
            yasuo.recovery(storageManager.getStateStore());
    
            yasuo.displayState();
    
    
        }
    }
    
    
  • 相关阅读:
    工作需求----表单多选框checkbox交互
    工作需求——JQ小效果分享下
    JQ返回顶部代码分享~~~~
    css 分享之background-attachment 属性
    CSS3学习之分享下transition属性
    css 伪元素分享!!!
    phpcm v9 任意调用分页/phpcm v9首页调用分页不起作用或者乱码
    facebook分享不能显示图片链接问题
    Fatal error: Uncaught SoapFault exception:解决办法
    VUE项目 启动提示 npn ERRT nissing script: dev解决办法
  • 原文地址:https://www.cnblogs.com/yfy-/p/12216085.html
Copyright © 2020-2023  润新知