转http://www.blogjava.net/liuwentao253/archive/2010/02/25/313927.html
9、MEMENTO
备忘录模式:又叫 Snapshot : 快照模式
(1) : 白箱备忘录模式的实现
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:11:54
* <p/>
* note: 备忘录(快照)角色
*/
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
}
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:12:27
* <p/>
* note: 负责人角色
*/
public class Caretaker {
private Memento memento;
/**
* 返回快照
* @return
*/
public Memento retrieveMemento() {
return this.memento;
}
/**
* 保存快照
* @param memento
*/
public void saveMemento(Memento memento) {
this.memento = memento;
}
}
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:11:41
* <p/>
* note: 发起人角色
*/
public class Originator {
private String state;
/**
* 创建一个快照
* @return
*/
public Memento createMemento() {
return new Memento(state);
}
/**
* 从快照中还原
* @param memento
*/
public void restoreMemento(Memento memento) {
this.state = memento.getState();
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
System.out.println("Current state = " + this.state);
}
}
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:12:36
* <p/>
* note: 客户端角色
*/
public class Client {
private static Originator originator = new Originator();
private static Caretaker caretaker = new Caretaker();
public static void main(String[] args) {
//发起人角色设置状态
originator.setState("On");
//负责人角色 保存快照
caretaker.saveMemento(originator.createMemento());
//发起人角色改变状态
originator.setState("Off");
//恢复发起人先前状态
originator.restoreMemento(caretaker.retrieveMemento() );
}
}
(2) : 黑箱备忘录模式的实现
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:39:24
* <p/>
* note: 窄接口 ,一个标识接口
*/
public interface MementoIF {
}
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:40:24
* <p/>
* note: 负责人角色
*
* 得到的备忘录角色是 窄接口,因此它得不到备忘录的具体内容
*/
public class Caretaker {
private MementoIF memento;
public MementoIF retrieveMemento() {
return this.memento;
}
public void saveMemento(MementoIF memento) {
this.memento = memento;
}
}
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:38:09
* <p/>
* note: 发起人角色
*
* 拥有一个 实现MementoIF 接口的 Memento 内部类
* 并且其内部都是 private的
*/
public class Originator {
public Originator() { }
private String state;
public MementoIF createMemento() {
return new Memento( this.state );
}
public void restoreMemento( MementoIF memento) {
Memento aMemento = (Memento) memento;
this.setState( aMemento.getState() );
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
System.out.println("state = " + state);
}
/**
* 一个内部类
* 里面所有的方法都是私有的
*/
class Memento implements MementoIF {
private String savedState;
private Memento(String someState) {
savedState = someState;
}
private void setState(String someState) {
savedState = someState;
}
private String getState() {
return savedState;
}
}
}
/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 16:40:41
* <p/>
* note:
*/
public class Client {
private static Originator o = new Originator();
private static Caretaker c = new Caretaker();
public static void main(String[] args) {
o.setState("On");
c.saveMemento( o.createMemento() );
o.setState("Off");
o.restoreMemento( c.retrieveMemento() );
}
}