設計模式之備忘錄模式

這裏寫圖片描述

這裏寫圖片描述

代碼實現

/**
 * 源發器類
 * @author Administrator
 *
 */
public class Emp {
    private String ename;
    private int age;
    private double 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 Emp(String ename, int age, double salary) {
        super();
        this.ename = ename;
        this.age = age;
        this.salary = salary;
    }
    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 Administrator
 *
 */
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 Administrator
 *
 */
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;
    }
}

這裏寫圖片描述

多個備忘點

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章