设计模式学习—备忘录模式

1、什么是备忘录模式?

​ Memento模式的目的是在不违反封装的情况下捕获和外部化对象的内部状态,以便稍后将对象恢复到这种状态。

2、场景分析

​ 让我们看一个例子。我们将创建一个包含两个双类型字段的类,并在其上运行一些数学操作。我们将为用户提供撤消操作。如果用户对某些操作后的结果不满意,可以调用undo操作,该操作将对象的状态恢复到最后保存的点。该示例还包含一个保存点机制,用户可以使用该机制保存对象的状态。我们还将提供各种撤消操作。一个简单的撤销操作就可以将对象状态恢复到先前的保存点。具有指定保存点的undo将恢复对象的特定状态,而undo all将删除对象的所有保存状态,并在创建对象时将对象恢复到初始化状态。

3、代码实现

public class Originator {
    private double x;
    private double y;
    private String lastUndoSavepoint;
    CareTaker careTaker;

    public Originator(double x, double y, CareTaker careTaker) {
        this.x = x;
        this.y = y;
        this.careTaker = careTaker;
        createSavepoint("INITIAL");
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void createSavepoint(String savepointName) {
        careTaker.saveMemento(new Memento(this.x, this.y), savepointName);
        lastUndoSavepoint = savepointName;
    }

    public void undo() {
        setOriginatorState(lastUndoSavepoint);
    }

    public void undo(String savepointName) {
        setOriginatorState(savepointName);
    }

    public void undoAll() {
        setOriginatorState("INITIAL");
        careTaker.clearSavepoints();
    }

    private void setOriginatorState(String savepointName) {
        Memento mem = careTaker.getMemento(savepointName);
        this.x = mem.getX();
        this.y = mem.getY();
    }

    @Override
    public String toString() {
        return "X: " + x + ", Y: " + y;
    }
}

​ 上面是Originator类,它的对象状态应该保存在内存中。该类包含两个双类型s字段x和y,还引用一个看守器。看管人用于保存和检索表示发起者对象状态的memento对象。在构造函数中,我们使用createSavepoint方法保存了对象的初始状态。此方法创建一个memento对象,并请求管理员处理该对象。我们使用了一个lastUndoSavepoint变量来存储最后保存的记忆体的键名,以便实现撤消操作。该类提供三种类型的撤销操作。没有任何参数的undo方法将恢复最后保存的状态,而具有保存点名称作为参数的undo将恢复使用特定保存点名称保存的状态。undoAll方法要求照顾者清除所有保存点并将其设置为初始状态(创建对象时的状态)。

public class Memento {
    private double x;
    private double y;

    public Memento(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}

Memento类用于存储发起者的状态,并由照料者存储。该类没有任何setter方法,它只用于获取对象的状态。

public class CareTaker {
    private final Map savepointStorage = new HashMap<>();

    public void saveMemento(Memento memento, String savepointName) {
        System.out.println("Saving state..." + savepointName);
        savepointStorage.put(savepointName, memento);
    }

    public Memento getMemento(String savepointName) {
        System.out.println("Undo at ..." + savepointName);
        return savepointStorage.get(savepointName);
    }

    public void clearSavepoints() {
        System.out.println("Clearing all save points...");
        savepointStorage.clear();
    }
}

上面的类是careTaker类,用于存储和提供请求的memento对象。该类包含用于保存memento对象的saveM emento方法、用于返回请求memento对象的getMemento方法和用于清除所有保存点并删除所有保存的memento对象的clearSavepoints方法。现在,让我们测试这个例子。

public class TestMementoPattern {
    public static void main(String[] args) {
        CareTaker careTaker = new CareTaker();
        Originator originator = new Originator(5, 10, careTaker);
        System.out.println("Default State: " + originator);
        originator.setX(originator.getY() * 51);
        System.out.println("State: " + originator);
        originator.createSavepoint("SAVE1");
        originator.setY(originator.getX() / 22);
        System.out.println("State: " + originator);
        originator.undo();
        System.out.println("State after undo: " + originator);
        originator.setX(Math.pow(originator.getX(), 3));
        originator.createSavepoint("SAVE2");
        System.out.println("State: " + originator);
        originator.setY(originator.getX() - 30);
        originator.createSavepoint("SAVE3");
        System.out.println("State: " + originator);
        originator.setY(originator.getX() / 22);
        originator.createSavepoint("SAVE4");
        System.out.println("State: " + originator);
        originator.undo("SAVE2");
        System.out.println("Retrieving at: " + originator);
        originator.undoAll();
        System.out.println("State after undo all: " + originator);
    }
}

4、何时使用备忘录

1、必须保存对象状态(部分)的快照,以便稍后恢复到该状态。

2、获取状态的直接接口将公开实现细节并打破对象的封装。

你可能感兴趣的:(设计模式学习—备忘录模式)