design_model(19)memento

1.备忘录模式

用于备份与还原,也叫快照模式

2.实例

public class Emp {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public EmpCopy memento() {
        return new EmpCopy(this);
    }

    public void recovery(EmpCopy empc) {
        this.age = empc.getAge();
    }

}

public class EmpCopy {
    private int age;

    public EmpCopy(Emp emp) {
        super();
        this.age = emp.getAge();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

public class Client {
    public static void main(String[] args) {
        Emp emp = new Emp();
        emp.setAge(20);
        EmpCopy memento = emp.memento();
        System.out.println(emp.getAge());
        emp.setAge(30);
        System.out.println(emp.getAge());
        emp.recovery(memento);
        System.out.println(emp.getAge());
    }
}

 

posted @ 2018-09-01 19:39  fatale  阅读(97)  评论(0编辑  收藏  举报