【设计模式】备忘录模式

什么是备忘录模式

保存对象的某个状态并可以恢复到该状态

补充说明

例子很多,如回退 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丶的建议

posted @ 2016-02-27 09:02  风一样的码农  阅读(1778)  评论(5编辑  收藏  举报