您的位置:首页 > 其它

备忘录

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());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: