class Originator { private String state = ""; public String getState() { return state; } public void setState(String state) { this.state = state; } public Memento createMemento(){ return new Memento(this.state); } public void restoreMemento(Memento memento){ this.setState(memento.getState()); } } class Memento { private String state = ""; public Memento(String state){ this.state = state; } public String getState() { return state; } public void setState(String state) { this.state = state; } } class Caretaker { private Memento memento; public Memento getMemento(){ return memento; } public void setMemento(Memento memento){ this.memento = memento; } } public class Client { public static void main(String[] args){ Originator originator = new Originator(); originator.setState("状态1"); System.out.println("初始状态:"+originator.getState()); Caretaker caretaker = new Caretaker(); caretaker.setMemento(originator.createMemento()); originator.setState("状态2"); System.out.println("改变后状态:"+originator.getState()); originator.restoreMemento(caretaker.getMemento()); System.out.println("恢复后状态:"+originator.getState()); } }
多状态多备份备忘录
通用代码演示的例子中,Originator类只有一个state变量需要备份,而通常情况下,发起人角色通常是一个javaBean,对象中需要备份的变量不止一个,需要备份的状态也不止一个,这就是多状态多备份备忘录。实现备忘录的方法很多,备忘录模式有很多变形和处理方式,像通用代码那样的方式一般不会用到,多数情况下的备忘录模式,是多状态多备份的。其实实现多状态多备份也很简单,最常用的方法是,我们在Memento中增加一个Map容器来存储所有的状态,在Caretaker类中同样使用一个Map容器才存储所有的备份。下面我们给出一个多状态多备份的例子:
class Originator {
private String state1 = "";
private String state2 = "";
private String state3 = "";
public String getState1() {
return state1;
}
public void setState1(String state1) {
this.state1 = state1;
}
public String getState2() {
return state2;
}
public void setState2(String state2) {
this.state2 = state2;
}
public String getState3() {
return state3;
}
public void setState3(String state3) {
this.state3 = state3;
}
public Memento createMemento(){
return new Memento(BeanUtils.backupProp(this));
}
public void restoreMemento(Memento memento){
BeanUtils.restoreProp(this, memento.getStateMap());
}
public String toString(){
return "state1="+state1+"state2="+state2+"state3="+state3;
}
}
class Memento {
private Map<String, Object> stateMap;
public Memento(Map<String, Object> map){
this.stateMap = map;
}
public Map<String, Object> getStateMap() {
return stateMap;
}
public void setStateMap(Map<String, Object> stateMap) {
this.stateMap = stateMap;
}
}
class BeanUtils {
public static Map<String, Object> backupProp(Object bean){
Map<String, Object> result = new HashMap<String, Object>();
try{
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor des: descriptors){
String fieldName = des.getName();
Method getter = des.getReadMethod();
Object fieldValue = getter.invoke(bean, new Object[]{});
if(!fieldName.equalsIgnoreCase("class")){
result.put(fieldName, fieldValue);
}
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void restoreProp(Object bean, Map<String, Object> propMap){
try {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor des: descriptors){
String fieldName = des.getName();
if(propMap.containsKey(fieldName)){
Method setter = des.getWriteMethod();
setter.invoke(bean, new Object[]{propMap.get(fieldName)});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Caretaker {
private Map<String, Memento> memMap = new HashMap<String, Memento>();
public Memento getMemento(String index){
return memMap.get(index);
}
public void setMemento(String index, Memento memento){
this.memMap.put(index, memento);
}
}
class Client {
public static void main(String[] args){
Originator ori = new Originator();
Caretaker caretaker = new Caretaker();
ori.setState1("中国");
ori.setState2("强盛");
ori.setState3("繁荣");
System.out.println("===初始化状态===\n"+ori);
caretaker.setMemento("001",ori.createMemento());
ori.setState1("软件");
ori.setState2("架构");
ori.setState3("优秀");
System.out.println("===修改后状态===\n"+ori);
ori.restoreMemento(caretaker.getMemento("001"));
System.out.println("===恢复后状态===\n"+ori);
}
}