文章出處

什么是備忘錄模式

保存對象的某個狀態并可以恢復到該狀態

補充說明

例子很多,如回退 ctri + z,回滾,ps恢復到操作歷史的某一刻等等。。。

角色

備忘錄角色:存儲狀態

發起人角色:創建備忘錄,并利用備忘錄存儲自己的狀態

負責人:管理備忘錄

客戶端

例子,JAVA實現

例子描述:顯示一個對象的歷史狀態

備忘錄角色:存儲發起人的狀態

package dp.memento;

public class Memento {
  private String state;
  private float x;
  private float y;

  public Memento(String state, float x, float y) {
    this.state = state;
    this.x = x;
    this.y = y;
  }

  
  /**
   * getState.
   * @return the state
   */
  public String getState() {
    return state;
  }

  
  /**
   * setState.
   * @param state the state to set
   */
  public void setState(String state) {
    this.state = state;
  }

  
  /**
   * getX.
   * @return the x
   */
  public float getX() {
    return x;
  }

  
  /**
   * setX.
   * @param x the x to set
   */
  public void setX(float x) {
    this.x = x;
  }

  
  /**
   * getY.
   * @return the y
   */
  public float getY() {
    return y;
  }

  
  /**
   * setY.
   * @param y the y to set
   */
  public void setY(float y) {
    this.y = y;
  }

}

發起人角色,希望存儲自己的歷史狀態

package dp.memento;

public class Originator {

  private String state;
  private float x;
  private float y;

  public String getState() {
    return state;
  }

  public void setState(String state) {
    this.state = state;
  }

  public Memento saveToMemento() {
    return new Memento(state, x, y);
  }

  public void restoreFromMemento(Memento memento) {
    this.state = memento.getState();
    this.x = memento.getX();
    this.y = memento.getY();
  }

  
  /**
   * getX.
   * @return the x
   */
  public float getX() {
    return x;
  }

  
  /**
   * setX.
   * @param x the x to set
   */
  public void setX(float x) {
    this.x = x;
  }

  
  /**
   * getY.
   * @return the y
   */
  public float getY() {
    return y;
  }

  
  /**
   * setY.
   * @param y the y to set
   */
  public void setY(float y) {
    this.y = y;
  }
}

備忘錄管理者(保存發起人的歷史狀態記錄)

package dp.memento;

public class Originator {

  private String state;
  private float x;
  private float y;

  public String getState() {
    return state;
  }

  public void setState(String state) {
    this.state = state;
  }

  public Memento saveToMemento() {
    return new Memento(state, x, y);
  }

  public void restoreFromMemento(Memento memento) {
    this.state = memento.getState();
    this.x = memento.getX();
    this.y = memento.getY();
  }

  
  /**
   * getX.
   * @return the x
   */
  public float getX() {
    return x;
  }

  
  /**
   * setX.
   * @param x the x to set
   */
  public void setX(float x) {
    this.x = x;
  }

  
  /**
   * getY.
   * @return the y
   */
  public float getY() {
    return y;
  }

  
  /**
   * setY.
   * @param y the y to set
   */
  public void setY(float y) {
    this.y = y;
  }
}

客戶端Main

package dp.memento;

public class Main {

  public static void main(String[] args) {
    Originator originator = new Originator();     //發起人
    MementoMgt mementoMgt = new MementoMgt();     //備忘錄管理,負責存儲歷史狀態

    originator.setState("2017-01-01");
    originator.setX(1.4f);
    originator.setY(5.4f);
    mementoMgt.add(originator.saveToMemento());    //記錄狀態
    
    originator.setState("2017-04-03");
    originator.setX(44.4f);
    originator.setY(52.4f);
    mementoMgt.add(originator.saveToMemento());    //記錄狀態
    
    originator.setState("2017-06-01");
    originator.setX(231.4f);
    originator.setY(555.4f);
    mementoMgt.add(originator.saveToMemento());    //記錄狀態
    
    originator.setState("2017-06-22");
    originator.setX(132.4f);
    originator.setY(53.4f);
    mementoMgt.add(originator.saveToMemento());    //記錄狀態

    System.out.println("狀態歷史:");
    for (Memento m : mementoMgt.getMementoList()) {
      System.out.println(m.getState() + ": " + m.getX() + ", " + m.getY());
    }

    System.out.println("當前狀態:");
    System.out.println(originator.getState() + ": " + originator.getX() + ", " + originator.getY());
    
    originator.restoreFromMemento(mementoMgt.getByState("2017-04-03")); //恢復到指定狀態--2017-04-03
    System.out.println("恢復后的狀態:");
    System.out.println(originator.getState() + ": " + originator.getX() + ", " + originator.getY());
  }
}

結果打印:

狀態歷史:
2017-01-01: 1.4, 5.4
2017-04-03: 44.4, 52.4
2017-06-01: 231.4, 555.4
2017-06-22: 132.4, 53.4
當前狀態:
2017-06-22: 132.4, 53.4
恢復后的狀態:
2017-04-03: 44.4, 52.4

最后更新:2017.06.22,之前的寫的有問題,已修改,感謝@ dracularking、@ yayale丶的建議


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()