21.备忘录模式
2015-09-04 20:59
323 查看
核心
-就是保存某个对象内部状态的拷贝,这样以后就可以将该对象回复到原先的状态。
结构:
源发器类Originator
备忘录类Memento
负责人类CareTaker
开发中常见的应用场景
-棋类游戏中的,悔棋
-普通软件中的,撤销操作
-数据库软件中的,事务管理中的,回滚操作
-Photoshop软件中的,历史记录
/**
* 源发器类(员工)
*
* @author Administrator
*
*/
public class Emp {
private String ename;
private int age;
private double salary;
//进行备忘录操作,并返回备忘录对象
public EmpMemento memento() {
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 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;
}
}
-就是保存某个对象内部状态的拷贝,这样以后就可以将该对象回复到原先的状态。
结构:
源发器类Originator
备忘录类Memento
负责人类CareTaker
开发中常见的应用场景
-棋类游戏中的,悔棋
-普通软件中的,撤销操作
-数据库软件中的,事务管理中的,回滚操作
-Photoshop软件中的,历史记录
/**
* 源发器类(员工)
*
* @author Administrator
*
*/
public class Emp {
private String ename;
private int age;
private double salary;
//进行备忘录操作,并返回备忘录对象
public EmpMemento memento() {
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 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 Administrator * */ public class EmpMemento { private String ename; private int age; private double salary; public EmpMemento(Emp e) { this.ename = e.getEname(); this.age = e.getAge(); this.salary = e.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 Administrator * */ public class CareTaker { private EmpMemento memento; public EmpMemento getMemento() { return memento; } public void setMemento(EmpMemento memento) { this.memento = memento; } }
public class Client { public static void main(String[] args) { CareTaker taker = new CareTaker(); Emp emp = new Emp("melon",23,7000); System.out.println("第一次创建对象:" + emp.getEname() + ",工资:" + emp.getSalary()+",年龄:" + emp.getAge()); taker.setMemento(emp.memento()); emp.setAge(25); emp.setEname("melon2"); emp.setSalary(12000); System.out.println("改变的对象:" + emp.getEname() + ",工资:" + emp.getSalary()+",年龄:" + emp.getAge()); emp.recovery(taker.getMemento()); System.out.println("恢复后的对象:" + emp.getEname() + ",工资:" + emp.getSalary()+",年龄:" + emp.getAge()); } }
相关文章推荐
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
- bash环境变量
- 读透一本书(转)
- Android实战简易教程-第五十三枪(通过实现OnScrollListener接口实现上拉加载更多功能)
- Flex与服务器交互之四(使用RemoteObject +BlazeDS+TomCat同Java交互及开发环境配置)
- 1055. The World's Richest (25)
- [置顶] Caffe学习笔记
- 算法导论——矩阵链乘法
- 前向型神经网络之BPNN(附源码)
- Coursera机器学习课程笔记(2) Linear Regression
- C++如何取得int型的最大最小值
- 插入排序(二)
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- Caffe学习:pycaffe接口配置
- Flex与服务器交互之三(使用WebService组件同服务器WebService通讯)
- 反锯齿渲染技术--SMAA
- Windows下搭建IOS开发环境(二)
- android中的一些小技巧
- Neo4j基本用法
- 插入排序(二)