java23中设计模式之备忘录模式
Posted 周无极
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java23中设计模式之备忘录模式相关的知识,希望对你有一定的参考价值。
package com.bdqn.memento; /** * 源发器类 * @author OU * */ 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() { } 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; } @Override public String toString() { return "Emp [ename=" + ename + ", age=" + age + ", salary=" + salary + "]"; } }
package com.bdqn.memento; 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; } }
package com.bdqn.memento; import java.util.ArrayList; import java.util.List; /** * 负责人 类 * * 负责管理备忘录对象 * * @author OU * */ public class CareTaker { private EmpMemento empMemento; //可以保存很多备份的点 private List<EmpMemento> list=new ArrayList<EmpMemento>(); public EmpMemento getEmpMemento() { return empMemento; } public void setEmpMemento(EmpMemento empMemento) { this.empMemento = empMemento; } }
package com.bdqn.memento; public class Client { public static void main(String[] args) { CareTaker taker=new CareTaker(); Emp emp=new Emp("周无极",18,900); System.out.println("第一次打印对象"+emp); taker.setEmpMemento(emp.memento());//备份一次 emp.setAge(38); emp.setEname("小欧"); emp.setSalary(1000); System.out.println("第二次打印对象"+emp); emp.recovery(taker.getEmpMemento()); System.out.println("第三次打印对象"+emp); } }
以上是关于java23中设计模式之备忘录模式的主要内容,如果未能解决你的问题,请参考以下文章