开发中常见的应用场景

  • 棋类游戏的悔棋

  • 普通软件中的撤销操作

  • 数据库中事务管理中的回滚操作

  • Photoshop软件中的历史记录

场景

  • 录入大批量人员资料.正在录入当前人资料时,发现上一个人录错了,此时需要恢复上一个人的资料,再进行修改

  • Word文档编辑时,电脑忽然司机或断电,再打开时,可以看到word提示你恢复到以前的文档

  • 管理系统中,公文撤回功能.公文发送出去后,想撤回来

核心

  • 就是保存某个对象内部状态的拷贝,这样以后就可以将该对象恢复到原先的状态

  • 结构

  • 源发器类Originator

  • 备忘录类Memento

  • 负责人类CareTake

QQ截图20161219104038.png

Code

/**
 * 源发器类
 * @author Matrix42
 *
 */
public class Emp {
    private String ename;
    private int age;
    private double salary;

    public Emp(String ename, int age, double salary) {
        super();
        this.ename = ename;
        this.age = age;
        this.salary = 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 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 Matrix42
 *
 */
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 Matrix42
 *
 */
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;
    }

}
public class Client {

    public static void main(String[] args) {
        CareTaker taker = new CareTaker();

        Emp emp = new Emp("Matrix42", 25, 3000);
        System.out.println("第一次创建对象:"+emp.getEname()+"---"+emp.getAge()+"---"+emp.getSalary());

        //备忘一次
        taker.setMemento(emp.memento());

        emp.setAge(38);
        emp.setSalary(9000);

        System.out.println("第二次打印:"+emp.getEname()+"---"+emp.getAge()+"---"+emp.getSalary());

        emp.recovery(taker.getMemento());

        System.out.println("第二次打印:"+emp.getEname()+"---"+emp.getAge()+"---"+emp.getSalary());
    }

}

负责人类

  • 负责保存备忘录对象

  • 可以通过增加容器来设置多个备忘点

备忘点较多时

  • 将备忘录压栈

  • 将多个备忘录对象,序列化和持久化