设计模式_19_备忘录模式

基本介绍.
  1. 备忘录模式(Memento Pattern) 在不破坏封装性的前提下,捕获-一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态
  2. 可以这里理解备忘录模式:现实生活中的备忘录是用来记录某些要去做的事情,或者是记录已经达成的共同意见的事情,以防忘记了。而在软件层面,备忘录模式有着相同的含义,备忘录对象主要用来记录一个对象的某种状态,或者某些数据,当要做回退时,可以从备忘录对象里获取原来的数据进行恢复操作
  3. 备忘录模式属于行为型模式


    image.png

例子:
需求:游戏角色状态恢复问题
游戏角色有攻击力和防御力。在大战Boss前保存自身的状态(攻击力和防御力),当大战boss后攻击力和防御力下滑,从备忘录对象恢复到大战前的状态。

实现:

package mementor;

public class GameRole {
    
    private int attack;
    private int defence;
    
    
    public Memento saveStateMemento() {
        return new Memento(attack ,defence);
    }
    
    public void getStateFromMemento(Memento memento) {
        attack = memento.getAttack();
        defence = memento.getDefence();
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getDefence() {
        return defence;
    }

    public void setDefence(int defence) {
        this.defence = defence;
    }

}

public class Memento {
    
    private int attack;
    private int defence;
    
    public Memento(int attack, int defence) {
        super();
        this.attack = attack;
        this.defence = defence;
    }
    public int getAttack() {
        return attack;
    }
    public int getDefence() {
        return defence;
    }   
}

public class Caretaker {

    private HashMap<String, ArrayList<Memento>> map = new HashMap<>();
    
    public void add(String name,Memento memento) {
        if (!map.containsKey(name)) {
            map.put(name, new ArrayList<>());
        }
        map.get(name).add(memento);
    }
    
    public Memento get(String name,int index) {
        return map.get(name).get(index);
    }
}

public class Client {

    public static void main(String[] args) {
        
        GameRole gameRole1 = new GameRole();
        Caretaker caretaker = new Caretaker();
        
        gameRole1.setAttack(100);   
        caretaker.add(gameRole1.toString(),gameRole1.saveStateMemento());
        System.out.println("当前状态:"+gameRole1.getAttack());
        
        gameRole1.setAttack(88);            
        caretaker.add(gameRole1.toString(),gameRole1.saveStateMemento());
        System.out.println("当前状态:"+gameRole1.getAttack());
        
        gameRole1.getStateFromMemento(caretaker.get(gameRole1.toString(),0));
        System.out.println("恢复后状态:"+gameRole1.getAttack());
        System.out.println(gameRole1.toString());
        
        GameRole gameRole2 = new GameRole();
        
        gameRole2.setAttack(10);    
        caretaker.add(gameRole2.toString(),gameRole2.saveStateMemento());
        System.out.println("当前状态:"+gameRole2.getAttack());
        
        gameRole2.setAttack(68);            
        caretaker.add(gameRole2.toString(),gameRole2.saveStateMemento());
        System.out.println("当前状态:"+gameRole2.getAttack());
        
        gameRole2.getStateFromMemento(caretaker.get(gameRole2.toString(),0));
        System.out.println("恢复后状态:"+gameRole2.getAttack());
        System.out.println(gameRole2.toString());
    }
}

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

  1. 给用户提供了- -种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史
  2. 实现了信息的封装,使得用户不需要关心状态的保存细节
  3. 如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存,这个需要注意
  4. 适用的应用场景: 1、后悔药。2、 打游戏时的存档。3、 Windows 里的ctri + z。4、IE中的后退。4、 数据库的事务管理
  5. 为了节约内存,备忘录模式可以和原型模式配合使用
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容