行为模式:Memento(备忘录)

The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.
Classic examples of the memento pattern include the seed of a pseudorandom number generator (it will always produce the same sequence thereafter when initialized with the seed state) and the state in a finite state machine.


import java.util.List;
import java.util.ArrayList;
class Originator {
    private String state;
    // The class could also contain additional data that is not part of the
    // state saved in the memento.
 
    public void set(String state) {
        System.out.println("Originator: Setting state to " + state);
        this.state = state;
    }
 
    public Memento saveToMemento() {
        System.out.println("Originator: Saving to Memento.");
        return new Memento(state);
    }
 
    public void restoreFromMemento(Memento memento) {
        state = memento.getSavedState();
        System.out.println("Originator: State after restoring from Memento: " + state);
    }
 
    public static class Memento {
        private final String state;
 
        public Memento(String stateToSave) {
            state = stateToSave;
        }
 
        public String getSavedState() {
            return state;
        }
    }
}
 
class Caretaker {
    public static void main(String[] args) {
        List<Originator.Memento> savedStates = new ArrayList<Originator.Memento>();
 
        Originator originator = new Originator();
        originator.set("State1");
        originator.set("State2");
        savedStates.add(originator.saveToMemento());
        originator.set("State3");
        // We can request multiple mementos, and choose which one to roll back to.
        savedStates.add(originator.saveToMemento());
        originator.set("State4");
 
        originator.restoreFromMemento(savedStates.get(1));   
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
备忘录模式是一种行为型设计模式,它允许在不破坏封装性的前提下捕获对象的内部状态,并在对象之外保存这个状态,以便在以后的某个时候将其恢复到原先的状态。 备忘录模式由三个主要角色组成: 1. 发起人(Originator):负责创建一个备忘录,并记录和恢复自身的内部状态。 2. 备忘录Memento):负责存储发起人对象的内部状态。 3. 管理者(Caretaker):负责保存备忘录,并可以对其进行管理,但不知道备忘录的具体内容。 该模式的工作流程如下: 1. 发起人创建一个备忘录,将自己的状态保存在备忘录中。 2. 发起人可以根据需要在任何时候保存自己的状态。 3. 管理者将备忘录保存起来,可维护多个备忘录。 4. 在需要恢复状态的时候,可以通过管理者获取相应的备忘录,并将发起人的状态恢复到备忘录中保存的状态。 备忘录模式的优点是: - 发起人对象与备忘录对象分离,实现了信息的封装和隐藏。 - 备忘录对象存储了发起人对象的历史状态,可以实现撤销和恢复操作。 - 备忘录可以进行存储和管理,可进行序列化和持久化操作。 备忘录模式通常适用于以下情况: - 需要保存和恢复对象的状态,并且封装对象的状态对于其他对象来说是不可见的。 - 需要实现撤销和恢复功能。 - 需要保存和恢复部分或全部对象状态的场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值