1 备忘录模式
2 实现代码
import java.util.ArrayList;
import java.util.List;
class Memento{
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}
class Originator{
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento saveMemo(){
return new Memento(state);
}
public void getMemo(Memento memento){
state = memento.getState();
}
}
class Admin{
private List<Memento> mementos = new ArrayList<>();
public void addMemo(Memento memento){
mementos.add(memento);
}
public Memento getMemo(int index){
return mementos.get(index);
}
}
public class Test{
public static void main(String[] args){
Originator originator = new Originator();
Admin admin = new Admin();
originator.setState("first");
admin.addMemo(originator.saveMemo());
originator.setState("second");
admin.addMemo(originator.saveMemo());
originator.getMemo(admin.getMemo(0));
System.out.println(originator.getState());
}
}