GOF 23 设计模式之 备忘录模式(Memento)

来源:互联网 发布:汽车设计软件三维 编辑:程序博客网 时间:2024/05/01 21:14

场景

  • 录入大批人员资料。正在录入当前人资料时,发现上一个人录错了,此时需要恢复上一个人的资料,再进行修改。
  • Word文档编辑时,突然电脑死机或断电,再打开时,可以看到word提示你恢复到以前的文档
  • 管理系统中,公文撤回功能。公文发送出去后,想撤回来。

核心

  • 就是保存某个对象内部状态的拷贝,这样以后就可以将该对象恢复到原先的状态
  • 结构
    • 源发器类Originator
    • 备忘录类Memento
    • 负责人类CareTaker :可以通过List或者栈来保存多个备忘
      这里写图片描述

开发中的场景

  • 数据库管理软件中,事务管理中的回滚操作
  • 普通软件中的撤销操作

代码

  • Emp
package com.coderbean.memento;/** * Created by Chang on 15/10/5. * 源发器类 */public class Emp {    private String name;    private int age;    private double salary;    //进行备忘录操作,并返回备忘录对象    public EmpMemento memento(){        return new EmpMemento(this);    }    //进行数据恢复    public void recovery(EmpMemento empMemento){        this.name = empMemento.getName();        this.age = empMemento.getAge();        this.salary = empMemento.getSalary();    }    public Emp(String name, int age, double salary) {        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;    }}
  • EmpMemento
package com.coderbean.memento;/** * 备忘录类 * Created by Chang on 15/10/5. */public class EmpMemento {    private String name;    private int age;    private double salary;    public EmpMemento(Emp emp) {        this.name = emp.getName();        this.age = emp.getAge();        this.salary = emp.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;    }}
  • CareTaker
package com.coderbean.memento;/** * 负责人类 * Created by Chang on 15/10/5. */public class CareTaker {    private EmpMemento memento;    public EmpMemento getMemento() {        return memento;    }    public void setMemento(EmpMemento memento) {        this.memento = memento;    }}
  • Client
package com.coderbean.memento;/** * Created by Chang on 15/10/5. */public class Client {    public static void main(String[] args) {        CareTaker taker = new CareTaker();        Emp emp = new Emp("张博",18,10000);        System.out.println("第一次创建对象--"+emp.getName()+"----"+emp.getAge()+"----"+emp.getSalary());        taker.setMemento(emp.memento());        emp.setName("Tom");        emp.setAge(80);        emp.setSalary(18);        System.out.println("第二次创建对象--"+emp.getName()+"----"+emp.getAge()+"----"+emp.getSalary());        emp.recovery(taker.getMemento());        System.out.println("第三次创建对象--"+emp.getName()+"----"+emp.getAge()+"----"+emp.getSalary());    }}
0 0