备忘录
2016-01-25 22:53
260 查看
/** * 负责人类 * 负责管理备忘录对象 * @author zhangjianbin * */ 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; } }
/** * 备忘录类 与源发器属性一样 * * @author zhangjianbin * */ public class EmpMeMento { private String ename; private int age; private double salary; /** * 传入源发器 * 作备份操作 * * @param emp */ public EmpMeMento(Emp emp) { this.ename = emp.getEname(); this.age = emp.getAge(); this.salary = emp.getSalary(); } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } 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; } }
/** * 源发器 * * @author zhangjianbin * */ public class Emp { private String ename; private int age; private double salary; //进行备忘操作,并返回备忘录对象 public EmpMeMento setEmpMeMento(){ return new EmpMeMento(this); } //进行数据恢复,恢复成制定备忘录对象的值 public void recovery(EmpMeMento mmt){ this.ename = mmt.getEname(); this.age = mmt.getAge(); this.salary = mmt.getSalary(); } public Emp(String ename, int age, double salary) { super(); this.ename = ename; this.age = age; this.salary = salary; } public Emp() { } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } 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; } }
public class Client { public static void main(String[] args) { /** * 备忘管理对象 */ CareTaker taker = new CareTaker(); /** * 源发器 * 将它设备的属性值复制到 EmpMeMento对象 */ Emp emp = new Emp("zhang",18,200); //第一次修改 /** * 备忘一次 * 将备忘对象复制到备忘管理对象 */ taker.setMemento(emp.setEmpMeMento()); System.err.println("第一次:"+emp.getEname()+"--"+emp.getAge()+"--"+emp.getSalary()); //第二次修改 emp.setAge(38); emp.setSalary(2000); //第二次备忘 taker.setMemento(emp.setEmpMeMento()); System.err.println("第二次:"+emp.getEname()+"--"+emp.getAge()+"--"+emp.getSalary()); //第三次修改 emp.setAge(100); /** * 恢复备忘对象 * 从备忘对象管理器中取出上一次备忘的对象,并将属性复制 到 源发器中的属性上 * 从而达到撤销的操作功能 */ emp.recovery(taker.getMemento()); //恢复到备忘录对象保存的状态 System.err.println("恢复后的状态:"+emp.getEname()+"--"+emp.getAge()+"--"+emp.getSalary()); } }
相关文章推荐
- SQLite数据插入异常
- 关于struts2的OGNL(二)
- JAVA 综合布局应用
- Android activity属性设置大全
- 2013年第四届蓝桥杯B组(C/C++)预赛题目及个人答案(欢迎指正)
- 阿岳之_DNS正反向解析库配置篇(一)
- spring--hibernate 事务
- JAVA 网格布局管理器
- 扩展欧几里得算法
- 在Xcode中使用Git进行源码版本控制
- 观察者模式
- serlvet的生命周期是怎样的...
- 自定义的servlet的 doGet,doPost是怎样被调用到的..
- Timestamp 使用
- JAVA 流式布局管理器
- 视图不可交互情况简记
- Get和post请求的区别
- 响应码 各自每段表示什么意思
- FZU 2150 Fire Game bfs
- testng.xml创建及解析