美文网首页Java
Java设计模式----备忘录模式

Java设计模式----备忘录模式

作者: GaaraZ | 来源:发表于2018-07-12 18:11 被阅读12次

    定义

    在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。

    结构

    备忘录模式
    • Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可以使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态。
    • Memetno(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问回到先前状态所需的所有数据。
    • Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。

    简单实现

    package memento;
    
    /**
     * 发起人
     */
    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;
        }
    }
    
    package memento;
    
    /**
     * 备忘录
     */
    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;
        }
    }
    
    package memento;
    
    /**
     * 管理者
     */
    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 memento;
    
    public class Client {
        public static void main(String[] args) {
            CareTaker taker = new CareTaker();
    
            Emp emp = new Emp("张三",20,2000);
            System.out.println("1.姓名:"+emp.getName()+";  年龄:"+emp.getAge()+";  薪水:"+emp.getSalary());
    
            taker.setEmpMemento(emp.memento());     // 备份
            emp.setName("李四");
            emp.setAge(30);
            emp.setSalary(6000);
            System.out.println("2.姓名:"+emp.getName()+";  年龄:"+emp.getAge()+";  薪水:"+emp.getSalary());
    
            emp.recovery(taker.getEmpMemento());      // 恢复
            System.out.println("3.姓名:"+emp.getName()+";  年龄:"+emp.getAge()+";  薪水:"+emp.getSalary());
        }
    }
    
    输出:
    1.姓名:张三;  年龄:20;  薪水:2000.0
    2.姓名:李四;  年龄:30;  薪水:6000.0
    3.姓名:张三;  年龄:20;  薪水:2000.0
    

    备忘点较多时

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

    小结

    备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。

    相关文章

      网友评论

        本文标题:Java设计模式----备忘录模式

        本文链接:https://www.haomeiwen.com/subject/vverpftx.html