(行为型模式)备忘录模式

114 阅读1分钟

简介

备忘录模式就是保存某个对象内部状态的拷贝,这样以后就可以将该对象恢复到原先的状态(Word文档编辑时,忽然电脑死机或断电,再打开时,可以看到word 提示你恢复到以前的文档)

角色

  • 源发器类Originator
  • 备忘录类Memento
  • 负责人类CareTaker

实现

源发器

public class Originator {

    private String name;
    private int age;
    private double salary;

    /**
     * 备忘一次
     * @return
     */
    public Memento memento() {
        return new Memento(this);
    }

    /**
     * @return void
     * @MethodName recovery
     * @Descrition 恢复数据
     * @Param [memento]
     */
    public void recovery(Memento memento) {
        this.name = memento.getName();
        this.age = memento.getAge();
        this.salary = memento.getSalary();
    }


    public Originator() {
    }

    public Originator(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;
    }

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

备忘录

public class Memento {

    private String name;
    private int age;
    private double salary;

    /**
     * 创建时将源发器类的信息传入
     * @param originator
     */
    public Memento(Originator originator) {
        this.name = originator.getName();
        this.age = originator.getAge();
        this.salary = originator.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 {
    /**
     * 存储备忘录对象
     */
    private Map<String, Memento> map = new HashMap<>();

    /**
     * 保存备忘
     * @param msg
     * @param memento
     */
    public void setMemento(String msg, Memento memento) {
        map.put(msg, memento);
    }

    /**
     * 获得备忘
     * @param msg
     * @return
     */
    public Memento getMemento(String msg) {
        return map.get(msg);
    }
}

测试

public class Demo {

    public static void main(String[] args) {
        CareTaker ct = new CareTaker();
        //初始化原始数据
        Originator originator = new Originator("张三",18,500);
        System.out.println("原始数据:" + originator.toString());

        originator.setName("李四");
        System.out.println("第一次修改后数据:" + originator.toString());
        //进行备忘
        ct.setMemento("第一次修改", originator.memento());
        originator.setName("王五");
        originator.setAge(20);
        System.out.println("第一次修改后数据:" + originator.toString());
        //进行备忘
        ct.setMemento("第二次修改", originator.memento());
        originator.recovery(ct.getMemento("第一次修改"));
        System.out.println("恢复后的数据:"+originator.toString());
    }
}

结果

UqUp0qqTbC.jpg

常用场景

  • 普通软件中的,撤销操作
  • Photoshop软件中的,历史记录