一、场景
录入大批人员资料,当正在录入当前人资料时,发现上一个人录错了
此时需要恢复上一个人的资料,再进行修改。
Word文档编辑时,忽然电脑死机或断电,再打开时可以看到word
提示你恢复到以前的文档。
管理系统中,公文撤回功能。公文发送出去后,想撤回来。
二、核心
保存某个对象内部状态的拷贝,这样以后就可以将该对象恢复到原先的状态。
结构:
一源发器类originator
一备忘录类Memento
一负责人类CareTak
三、示例
package com.lgd.memento;
/** * 源发器类 * @author liguodong * */
public class Emp {
private String name;
private int age;
private double salary;
//进行备忘操作,并返回备忘录对象。
public EmpMemento memento(){
return new EmpMemento(this);
}
//进行数据恢复,恢复成指定备忘录的值
public void recovery(EmpMemento mmt){
this.name = mmt.getName();
this.age = mmt.getAge();
this.salary = mmt.getSalary();
}
public Emp() {
}
public Emp(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
package com.lgd.memento;
/** * 备忘录类 * @author liguodong * */
public class EmpMemento {
private String name;
private int age;
private double salary;
public EmpMemento(Emp e) {
this.name = e.getName();
this.age = e.getAge();
this.salary = e.getSalary();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
package com.lgd.memento;
import java.util.ArrayList;
import java.util.List;
/** * 负责人类 * 负责管理备忘录对象 * @author liguodong * */
public class CareTaker {
//备忘一次
private EmpMemento memento;
//如果备忘多次,可以通过容器,存储很多的备忘
//private List<EmpMemento> list = new ArrayList<EmpMemento>();
public EmpMemento getMemento() {
return memento;
}
public void setMemento(EmpMemento memento) {
this.memento = memento;
}
}
package com.lgd.memento;
public class Client {
public static void main(String[] args) {
CareTaker taker = new CareTaker();
Emp emp = new Emp("周杰伦",33,10000);
System.out.println("第一次打印对象:"+emp.getName()+"---"+emp.getAge()+"---"+emp.getSalary());
taker.setMemento(emp.memento());//备忘一次
emp.setAge(38);
emp.setName("范冰冰");
emp.setSalary(20000);
System.out.println("第二次打印对象:"+emp.getName()+"---"+emp.getAge()+"---"+emp.getSalary());
emp.recovery(taker.getMemento());//恢复到备忘录对象保存的状态
System.out.println("第三次打印对象:"+emp.getName()+"---"+emp.getAge()+"---"+emp.getSalary());
}
}
运行结果:
第一次打印对象:周杰伦—33—10000.0
第二次打印对象:范冰冰—38—20000.0
第三次打印对象:周杰伦—33—10000.0
四、其他
负责人类一负责保存好的备忘录对象。
可以通过增加容器,设置多个“备忘点”
public class CareTaker {
//如果备忘多次,可以通过容器,存储很多的备忘
//private List<EmpMemento> list = new ArrayList<EmpMemento>();
public List<EmpMemento> getList() {
return list;
}
public void setList(List<EmpMemento> list) {
this.list = list;
}
}
也可以将备忘录压栈
public class CareTaker{
private Memento memento;
private Stack<Memento> stack = new Stack<Memento>();
}
备忘点较多时,放内存不合适,可以将多个备忘录对象序列化和持久化。
五、开发场景
棋牌游戏中的悔棋
普通软件中的撤销操作
数据库软件中的事务管理中的回滚操作
PhotoShop软件中的历史记录
原文链接:https://blog.csdn.net/scgaliguodong123_/article/details/45361545
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~