标签:round lan int nbsp new target alt back ati
参考文档:
https://www.cnblogs.com/chenssy/p/3341526.html
定义:
在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。
uml类图:
模式组成:
优点:
缺点:
应用场景:
备忘录模式vs克隆:
举个栗子:
定义原发器
class Originator { private String state; public Originator(String s) { this.state = s; } public Memento createMemento() { System.out.println("备份啦备份啦,备份的进度是 " + state); return new Memento(state); } public void rollback(Memento me) { this.setState(me.getState()); } public String getState() { return state; } public void setState(String state) { this.state = state; } }
定义备忘录
class Memento { private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } public Memento(String state) { this.state = state; } }
负责人调用
public static void main(String[] args) { Originator ori = new Originator("111"); System.out.println("我现在的进度是 :" + ori.getState()); Memento me = ori.createMemento(); ori.setState("222"); System.out.println("更新啦更新啦,现在的进度是 " + ori.getState()); ori.rollback(me); System.out.println("回滚啦回滚啦,现在的进度是 " + ori.getState()); }
输出
标签:round lan int nbsp new target alt back ati
原文地址:http://www.cnblogs.com/amei0/p/7976461.html