• 设计模式——备忘录模式


    备忘录模式是什么?

    在不破坏封装性的前提下,捕获一个对的内部状态,并在该对象之外保持这个状态。这样以后就可将该对象恢复到原先保持的状态

    备忘录模式解决什么问题?

    如游戏打Boss失败时回档,定义一个游戏角色

    
    public class GameRole {
        private int vitality;
        private int attack;
        private int defense;
        public int getVitality() {
            return vitality;
        }
        public void setVitality(int vitality) {
            this.vitality = vitality;
        }
        public int getAttack() {
            return attack;
        }
        public void setAttack(int attack) {
            this.attack = attack;
        }
        public int getDefense() {
            return defense;
        }
        public void setDefense(int defense) {
            this.defense = defense;
        }
        public void displayState() {
            System.out.println("角色当前状态:");
            System.out.println("体力:" + vitality);
            System.out.println("攻击力:" + attack);
            System.out.println("防御力:" + defense);
        }
        public void getInitState() {
            vitality = 100;
            attack = 100;
            defense = 100;
        }
        public void fight() {
            vitality = 0;
            attack = 0;
            defense = 0;
        }
    }
    
    • 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

    在挑战Boss前,复制一份当前的状态

    GameRole role = new GameRole();
    role.getInitState();
    role.displayState();
    
    GameRole backup = new GameRole();
    backup.setVitality(role.getVitality());
    backup.setAttack(role.getAttack());
    backup.setDefense(role.getDefense());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    当挑战Boss失败后,通过此前的复制还原状态

    role.fight();
    role.displayState();
    
    role.setVitality(backup.getVitality());
    role.setAttack(backup.getAttack());
    role.setDefense(backup.getDefense());
    role.displayState();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • problem1:需要对属性一个个进行备份
    • problem2:当属性增加或修改时,需要对代码修改

    备忘录模式实现

    创建备忘录,添加要保存的数据

    public class RoleStateMemento {
        private int vitality;
        private int attack;
        private int defense;
    
        public RoleStateMemento(int vitality, int attack, int defense) {
            this.vitality = vitality;
            this.attack = attack;
            this.defense = defense;
        }
    
        public int getVitality() {
            return vitality;
        }
    
        public void setVitality(int vitality) {
            this.vitality = vitality;
        }
    
        public int getAttack() {
            return attack;
        }
    
        public void setAttack(int attack) {
            this.attack = attack;
        }
    
        public int getDefense() {
            return defense;
        }
    
        public void setDefense(int defense) {
            this.defense = defense;
        }
    }
    
    • 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

    修改GameRole类,添加备份/还原方法

    public class GameRole {
        private int vitality;
        private int attack;
        private int defense;
    
        public int getVitality() {
            return vitality;
        }
    
        public void setVitality(int vitality) {
            this.vitality = vitality;
        }
    
        public int getAttack() {
            return attack;
        }
    
        public void setAttack(int attack) {
            this.attack = attack;
        }
    
        public int getDefense() {
            return defense;
        }
    
        public void setDefense(int defense) {
            this.defense = defense;
        }
    
        public void displayState() {
            System.out.println("角色当前状态:");
            System.out.println("体力:" + vitality);
            System.out.println("攻击力:" + attack);
            System.out.println("防御力:" + defense);
        }
    
        public void getInitState() {
            vitality = 100;
            attack = 100;
            defense = 100;
        }
    
        public void fight() {
            vitality = 0;
            attack = 0;
            defense = 0;
        }
    
        public RoleStateMemento saveState() {
            return new RoleStateMemento(vitality, attack, defense);
        }
    
        public void recoveryState(RoleStateMemento memento) {
            vitality = memento.getVitality();
            attack = memento.getAttack();
            defense = memento.getDefense();
        }
    }
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    创建它们之间的管理类

    public class RoleStateCaretaker {
        private RoleStateMemento mMemento;
    
        public RoleStateMemento getMemento() {
            return mMemento;
        }
    
        public void setMemento(RoleStateMemento memento) {
            mMemento = memento;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    备份/还原代码如下

    GameRole role = new GameRole();
    role.getInitState();
    role.displayState();
    
    RoleStateCaretaker stateCaretaker = new RoleStateCaretaker();
    stateCaretaker.setMemento(role.saveState());
    
    role.fight();
    role.displayState();
    
    role.recoveryState(stateCaretaker.getMemento());
    role.displayState();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    ESP32C3 PWM输出
    区块链跨链技术
    OLED显示模块的工作原理、结构特点、应用领域和发展趋势
    dp应试进阶:最大和子数组(区分:“序列”与“数组”,序列可以跳跃,数组必须连续)
    Linux监控
    每天一个设计模式之单例模式(六种写法)
    微信小程序-双滑块组件
    世界前沿技术发展报告2023《世界航天技术发展报告》(五)太空探索技术
    Win11快捷复制粘贴不能用怎么办?Win11快捷复制粘贴不能用
    一图说清在哪里查看阿里云已购资源包
  • 原文地址:https://blog.csdn.net/qq_35258036/article/details/132992972