备忘录模式

Posted yfy-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了备忘录模式相关的知识,希望对你有一定的参考价值。

一、定义

备忘录模式(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();


    }
}

以上是关于备忘录模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式备忘录模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

(十三)备忘录模式-代码实现

19-备忘录(Memento)模式Ruby实现

设计模式----备忘录模式

设计模式14:备忘录模式

设计模式之备忘录模式