设计模式之备忘录模式

备忘录模式基本介绍

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

备忘录模式原理类图

image

1.originator:对象(需要保存状态的对象)
2.Memento:备忘录对象,负责保存好记录,即Originator内部状态
3.Caretaker:守护者对象,负责保存多个备忘录对象,使用集合管理,提高效率
如果希望保存多个originator对象的不同时间的状态也可以,只需要用HashMap<String,集合>

游戏角色恢复状态实例

游戏角色有攻击力和防御力,在大战BOSS前保存自身状态,大战之后攻击和防御力下降,从备忘录状态恢复到大战前的状态
image
代码实现

package com.cedric.memento.game;

public class Memento {

    // 攻击力
    private int vit;
    // 防御力
    private int def;

    public Memento(int vit, int def) {

    }

    public int getVit() {
        return vit;
    }

    public void setVit(int vit) {
        this.vit = vit;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }
}
package com.cedric.memento.game;

import java.util.ArrayList;
import java.util.HashMap;

public class Caretaker {

    // 如果只保存一次状态
    private Memento memento;
    // 对GameRole保存多次状态
   // private ArrayList<Memento> mementos;
    // 对多个游戏角色保存状态
    //private HashMap<String,ArrayList<Memento>> rolesMementos;


    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
package com.cedric.memento.game;

public class GameRole {

    private int vit;
    private int def;

    // 创建Memento,根据当前状态得到Memento
    public Memento createMemento(){
        return new Memento(vit,def);
    }

    // 从备忘录对象恢复GameRole的状态
    public void recoverGameRoleFromMemento(Memento memento){
        this.vit = memento.getVit();
        this.def = memento.getDef();
    }

    // 显示当前游戏状态
    public void display(){
        System.out.println("角色当前攻击力为:" + this.vit + "当前防御力为:" + this.def);
    }

    public int getVit() {
        return vit;
    }

    public void setVit(int vit) {
        this.vit = vit;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }
}
package com.cedric.memento.game;


public class Client {
    public static void main(String[] args) {
        // 创建游戏角色
        GameRole gameRole = new GameRole();
        gameRole.setVit(100);
        gameRole.setDef(100);

        System.out.println("战前状态");
        gameRole.display();

        // 把当前状态保存到caretaker
        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(gameRole.createMemento());

        System.out.println("大战中状态");
        gameRole.setVit(20);
        gameRole.setDef(20);

        gameRole.display();

        System.out.println("战后恢复到大战前");
        gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
        System.out.println("恢复后状态");
        gameRole.display();

    }
}

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

1.给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便的回到某个历史状态
2.实现类信息的封装,使得用户不需要关心状态的保存细节
3.如果类的成员变量过多,势必会占用比较大的资源,而且每一次保证都会消耗一定的内存,需要注意
4.适用的应用场景:1.后悔药  2.打游戏时的存档 3.ctrl + z  4.数据库的事务管理
5.为了节约内存,备忘录模式可以和原型模式配合使用
posted @ 2021-10-14 19:56  guided  阅读(43)  评论(0)    收藏  举报