Memento 用的很多,就是把状态简单保存起来,以后再恢复
参考:
http://www.jdon.com/designpatterns/memento.htm
放段代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package memento;
/**
*
* @author blacklaw
*/
public class Memento {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Human man = new Human();
HumanMemento memento;
man.name = "name_1";
man.displayState();//初始状态
memento = man.GetMemento();//保存状态
man.name = "name_2";//修改状态
man.displayState();
man.SetMemento(memento);//还原状态
man.displayState();
}
}
class Human{
public String name;
public void SetMemento(HumanMemento m){
this.name = m.name;
}
public HumanMemento GetMemento(){
return new HumanMemento(this);
}
public void displayState(){
System.out.println("Human name" + this.name);
}
}
class HumanMemento{
public String name;
HumanMemento(Human h){
this.name = h.name;
}
}