设计模式:备忘录模式

  平时遇到word文档编辑忽然电脑断电时,当我们再次打开时,原来的记录状态还在。

ps软件的历史记录等,我们可以使用该记录恢复之前的动作。这些就是备忘录模式的场景

备忘录模式就是提供一个保存对象状态的功能,这样以后就可以将该对象恢复到原来的状态

结构:

  源发器类Originator: 负责创建一个备忘录Memento,用以记录当前时刻自身的内部状态,并可使用备忘录恢复内部状态。

  备忘录类Memento:负责存储Originator对象的内部状态

  负责人类CareTake:管理备忘录类

 

/**
 * 源发器类
 * 
 */
public class Emp {
    //对象状态
    private String name;
    private int age;
    private double salary;

    public Emp(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    //创建备忘录对象,进行备忘操作
    public EmpMemento empMemento() {
        return new EmpMemento(this);
    }

    //恢复数据
    public void recovery(EmpMemento 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;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
}

  

/**
 * 备忘录类
 */
public class EmpMemento {
    private String name;
    private int age;
    private double salary;

    public EmpMemento(Emp e) {
        this.name = e.getName();
        this.age = e.getAge();
        this.salary = e.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;
    }
}

  

/**
 * 负责人类
 * 管理备忘录对象
 */
public class CareTaker {
    //list ...or
    private EmpMemento memento;

    public EmpMemento getMemento() {
        return memento;
    }

    public void setMemento(EmpMemento memento) {
        this.memento = memento;
    }
}

  这里如果要记录保存多次(多个备忘点),可以将属性EmpMemento用容器包装。

public class Client {
    public static void main(String[] args) {
        CareTaker taker = new CareTaker();
      //第一次创建对象
        Emp emp = new Emp("test", 18, 1000);
        System.out.println(emp);
      //保存一次状态 taker.setMemento(emp.empMemento()); emp.setAge(99); System.out.println(emp);       //恢复状态 emp.recovery(taker.getMemento()); System.out.println(emp); } }

  

Emp{name='test', age=18, salary=1000.0}
Emp{name='test', age=99, salary=1000.0}
Emp{name='test', age=18, salary=1000.0}

Process finished with exit code 0

 恢复数据成功,即保存状态时成功的。

开发中常用:

  棋牌类游戏,悔棋操作

  软件的撤销步骤

  事务管理回滚等

 

posted @ 2019-01-22 21:02  小阿Q的博客  阅读(248)  评论(0编辑  收藏  举报