package com.test; public class Memento { //constructor zhong store state; private String state; public Memento (String state) { this.state = state; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
package com.test;public class CareTaker {private Memento memento;public Memento getMemento() {return memento;}public void setMemento(Memento memento) {this.memento = memento;}}
public class Test {package com.test; public class Originator { private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } public Memento createMemento() { return new Memento(state); } public void useMemento(Memento m) { this.state = m.getState(); } public void showState() { System.out.println(state); } }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Originator o = new Originator();
o.setState("正在睡觉中。。。。。。");
CareTaker ca = new CareTaker();
ca.setMemento(o.createMemento());
o.setState("正在学习中。。。。。。");
o.showState();
o.useMemento(ca.getMemento());
o.showState();
}
}
备忘录模式
最新推荐文章于 2024-04-24 18:24:21 发布