设计模式-备忘录模式

1.备忘录模式的定义及使用场景

备忘录模式是一种行为模式,该模式用于保存对象当前状态,并且在之后可以再次恢复到此状态。备忘录模式实现的方式需要保证被保存的对象状态不能被对象从外部访问,目的是为了保护好被保存的这些对象状态的完整性以及内部实现不向外暴露。

定义:

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

使用场景:

  • 需要保存一个对象在某一个时刻的状态或部分状态
  • 需要用一个接口来让其他对象得到这些状态,将会暴露对象的实现细节并破坏对象的封装性,一个对象不希望外界直接访问其内部状态,通过中间对象可以间接访问其内部状态

这里写图片描述

2. 备忘录模式的优缺点

2.1优点

  • 给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态
  • 实现了信息的封装,使得用户不需要关心状态的保持细节

2.2缺点

消耗资源,如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存

3.注意事项

  • 备忘录的生命期
    备忘录创建出来就要在“最近”的代码中使用,要主动管理它的生命周期,建立就要使用,不使用就要立刻删除其引用,等待垃圾回收器对它的回收处理
  • 备忘录的性能
    不要在频繁建立备份的场景中使用备忘录模式(比如一个for循环中),主要原因是一是控制不了备忘录建立的对象数量;而是大对象的建立是要消耗资源的,系统的性能需要考虑。

4. 备忘录模式的实现方式

public class Memento {
    //发起人的内部状态
    private String state="";

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}
public class Caretaker {
    //备忘录对象
    private  Memento memento;

    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
public class Originator {
    //内部状态
    private String state = "";

    public String getState() {
        return state;
    }

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

    //创建一个备忘录
    public Memento createMemento() {
        return new Memento(state);
    }

    //恢复一个备忘录
    public void restoreMemento(Memento memento) {
        this.setState(memento.getState());
    }
}
public class Test {

    public static void main(String args[]) {
        //定义出发起人
        Originator originator = new Originator();
        originator.setState("初始:1111111111111");
        //定义出备忘录管理员
        Caretaker caretaker = new Caretaker();
        System.out.println(originator.getState());
        //创建一个备忘录
        caretaker.setMemento(originator.createMemento());
        originator.setState("改变:22222222222222");
        System.out.println(originator.getState());
        originator.setState("恢复:restoreMemento");
        originator.restoreMemento(caretaker.getMemento());
        System.out.println(originator.getState());
    }
}

5. 备忘录模式在Android中的实际应用

在Android开发中,状态模式应用是Android中的状态保持,也就是里面的onSaveInstanceState和onRestoreInstanceState。当Activity不是正常方式退出,且Activity在随后的时间内被系统杀死之前会调用这两个方法让开发人员可以有机会存储Activity的相关信息,并且在下次放好Activity的时候恢复这些数据。
Activity:

 protected void onSaveInstanceState(Bundle outState) {
  outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState()); //保存当前窗口的视图树的状态
  Parcelable p = mFragments.saveAllState(); //存储Fragment的状态
  if (p != null) {
  outState.putParcelable(FRAGMENTS_TAG, p);
  }
  getApplication().dispatchActivitySaveInstanceState(this, outState);
  }

Window的实现为PhoneWindow:

 /** {@inheritDoc} */
  @Override
  public Bundle saveHierarchyState() {
  Bundle outState = new Bundle();
  if (mContentParent == null) {
  return outState;
  }
//通过SparseArray类来存储,这相当于一个key为整型的map
  SparseArray<Parcelable> states = new SparseArray<Parcelable>();
//mContentParent就是调用Activity的setContentView函数设置的内容视图,它是内容视图的根节点,在这里存储整棵树的结构
  mContentParent.saveHierarchyState(states);
//将视图树结构放到outState中
  outState.putSparseParcelableArray(VIEWS_TAG, states);

  // 保存当前界面中获取了焦点的View
  View focusedView = mContentParent.findFocus();
  if (focusedView != null) {
  if (focusedView.getId() != View.NO_ID) {
  outState.putInt(FOCUSED_ID_TAG, focusedView.getId());
  } else {
  if (false) {
  Log.d(TAG, "couldn't save which view has focus because the focused view "
  + focusedView + " has no id.");
  }
  }
  }

  // 存储整个面板的状态
  SparseArray<Parcelable> panelStates = new SparseArray<Parcelable>();
  savePanelState(panelStates);
  if (panelStates.size() > 0) {
  outState.putSparseParcelableArray(PANELS_TAG, panelStates);
  }

//存储actionBar的状态
  if (mDecorContentParent != null) {
  SparseArray<Parcelable> actionBarStates = new SparseArray<Parcelable>();
  mDecorContentParent.saveToolbarHierarchyState(actionBarStates);
  outState.putSparseParcelableArray(ACTION_BAR_TAG, actionBarStates);
  }

  return outState;
  }

在saveHierarchyState函数中,主要是存储了与当前UI、ActionBar相关的View状态。mContentParent就是我们通过Activity的setContentView函数设置的内容视图,他是这个内容视图的根节点。mContentParent是一个ViewGroup对象,但是saveHierachyState是在父类View中

 public void saveHierarchyState(SparseArray<Parcelable> container) {
  dispatchSaveInstanceState(container);
  }
  protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
  if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
  mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
  Parcelable state = onSaveInstanceState();
  if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
  throw new IllegalStateException(
  "Derived class did not call super.onSaveInstanceState()");
  }
  if (state != null) {
  // Log.i("View", "Freezing #" + Integer.toHexString(mID)
  // + ": " + state);
  container.put(mID, state); //将自身状态放到container中 key 为id value为自身状态
  }
  }
  }
//View类默认的存储的状态为空
 protected Parcelable onSaveInstanceState() {
  mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
  if (mStartActivityRequestWho != null) {
  BaseSavedState state = new BaseSavedState(AbsSavedState.EMPTY_STATE);
  state.mStartActivityRequestWhoSaved = mStartActivityRequestWho;
  return state;
  }
  return BaseSavedState.EMPTY_STATE;
  }

恢复数据的调用过程如下,基本流程与保存类似
Activity:

  protected void onRestoreInstanceState(Bundle savedInstanceState) {
  if (mWindow != null) {
  Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
  if (windowState != null) {
  mWindow.restoreHierarchyState(windowState);
  }
  }
  }

PhoneWindow:

 /** {@inheritDoc} */
  @Override
  public void restoreHierarchyState(Bundle savedInstanceState) {
  if (mContentParent == null) {
  return;
  }

  SparseArray<Parcelable> savedStates
  = savedInstanceState.getSparseParcelableArray(VIEWS_TAG);
  if (savedStates != null) {
  mContentParent.restoreHierarchyState(savedStates);
  }

  // restore the focused view
  int focusedViewId = savedInstanceState.getInt(FOCUSED_ID_TAG, View.NO_ID);
  if (focusedViewId != View.NO_ID) {
  View needsFocus = mContentParent.findViewById(focusedViewId);
  if (needsFocus != null) {
  needsFocus.requestFocus();
  } else {
  Log.w(TAG,
  "Previously focused view reported id " + focusedViewId
  + " during save, but can't be found during restore.");
  }
  }

  // restore the panels
  SparseArray<Parcelable> panelStates = savedInstanceState.getSparseParcelableArray(PANELS_TAG);
  if (panelStates != null) {
  restorePanelState(panelStates);
  }

  if (mDecorContentParent != null) {
  SparseArray<Parcelable> actionBarStates =
  savedInstanceState.getSparseParcelableArray(ACTION_BAR_TAG);
  if (actionBarStates != null) {
  doPendingInvalidatePanelMenu();
  mDecorContentParent.restoreToolbarHierarchyState(actionBarStates);
  } else {
  Log.w(TAG, "Missing saved instance states for action bar views! " +
  "State will not be restored.");
  }
  }
  }

View:

public void restoreHierarchyState(SparseArray<Parcelable> container) {
  dispatchRestoreInstanceState(container);
  }
  /**
  * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
  * state for this view and its children. May be overridden to modify how restoring
  * happens to a view's children; for example, some views may want to not store state
  * for their children.
  *
  * @param container The SparseArray which holds previously saved state.
  *
  * @see #dispatchSaveInstanceState(android.util.SparseArray)
  * @see #restoreHierarchyState(android.util.SparseArray)
  * @see #onRestoreInstanceState(android.os.Parcelable)
  */
  protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
  if (mID != NO_ID) {
  Parcelable state = container.get(mID);
  if (state != null) {
  // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
  // + ": " + state);
  mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
  onRestoreInstanceState(state);
  if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
  throw new IllegalStateException(
  "Derived class did not call super.onRestoreInstanceState()");
  }
  }
  }
  }
  protected void onRestoreInstanceState(Parcelable state) {
  mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
  if (state != null && !(state instanceof AbsSavedState)) {
  throw new IllegalArgumentException("Wrong state class, expecting View State but "
  + "received " + state.getClass().toString() + " instead. This usually happens "
  + "when two views of different type have the same id in the same hierarchy. "
  + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
  + "other views do not use the same id.");
  }
  if (state != null && state instanceof BaseSavedState) {
  mStartActivityRequestWho = ((BaseSavedState) state).mStartActivityRequestWhoSaved;
  }
  }

在这个过程中,Activity扮演了Caretaker角色,负责存储、恢复UI的状态信息;Activity、Fragement、View、ViewGroup等对象为Originator角色,也就是需要存储状态的对象;Memento则是由Bundle类扮演。

Activity在停止之前会根据Activity的退出情景来选择是否需要存储状态,在重新启动该Activity时会判断ActivityClientRecord对象中是否存储了Activity的状态,如果含有状态则调用Activity的onRestoreInstanceState函数,从而使得Activity的UI效果与上次保持一致,这样一来,就保证了在非正常退出Activity时不会丢失数据的情况,很好地提升了用户体验。

设计模式之备忘录 和 状态模式精讲 19.1 场景问题 19.1.1 开发仿真系统 考虑这样一个仿真应用,功能是:模拟运行针对某个具体问题的多个解决方案,记录运行过程的各种数据,在模拟运行完成过后,好对这多个解决方案进行比较和评价,从而选定最优的解决方案。 这种仿真系统,在很多领域都有应用,比如:工作流系统,对同一问题制定多个流程,然后通过仿真运行,最后来确定最优的流程做为解决方案;在工业设计和制造领域,仿真系统的应用就更广泛了。 由于都是解决同一个具体的问题,这多个解决方案并不是完全不一样的,假定它们的前半部分运行是完全一样的,只是在后半部分采用了不同的解决方案,后半部分需要使用前半部分运行所产生的数据。 由于要模拟运行多个解决方案,而且最后要根据运行结果来进行评价,这就意味着每个方案的后半部分的初始数据应该是一样,也就是说在运行每个方案后半部分之前,要保证数据都是由前半部分运行所产生的数据,当然,咱们这里并不具体的去深入到底有哪些解决方案,也不去深入到底有哪些状态数据,这里只是示意一下。 那么,这样的系统该如何实现呢?尤其是每个方案运行需要的初始数据应该一样,要如何来保证呢? 19.1.2 不用模式的解决方案 要保证初始数据的一致,实现思路也很简单: 首先模拟运行流程第一个阶段,得到后阶段各个方案运行需要的数据,并把数据保存下来,以备后用 每次在模拟运行某一个方案之前,用保存的数据去重新设置模拟运行流程的对象,这样运行后面不同的方案时,对于这些方案,初始数据就是一样的了 根据上面的思路,来写出仿真运行的示意代码,示例代码如下: /** * 模拟运行流程A,只是一个示意,代指某个具体流程 */ public class FlowAMock { /** * 流程名称,不需要外部存储的状态数据 */ private String flowName; /** * 示意,代指某个中间结果,需要外部存储的状态数据 */ private int tempResult; /** * 示意,代指某个中间结果,需要外部存储的状态数据 */ private String tempState; /** * 构造方法,传入流程名称 * @param flowName 流程名称 */ public FlowAMock(String flowName){ this.flowName = flowName; } public String getTempState() { return tempState; } public void setTempState(String tempState) { this.tempState = tempState; } public int getTempResult() { return tempResult; } public void setTempResult(int tempResult) { this.tempResult = tempResult; } /** * 示意,运行流程的第一个阶段 */ public void runPhaseOne(){ //在这个阶段,可能产生了中间结果,示意一下 tempResult = 3; tempState = "PhaseOne"; } /** * 示意,按照方案一来运行流程后半部分 */ public void schema1(){ //示意,需要使用第一个阶段产生的数据 this.tempState += ",Schema1"; System.out.println(this.tempState + " : now run "+tempResult); this.tempResult += 11; } /** * 示意,按照方案二来运行流程后半部分 */ public void schema2(){ //示意,需要使用第一个阶段产生的数据 this.tempState += ",Schema2"; System.out.println(this.tempState + " : now run "+tempResult); this.tempResult += 22; } } (2)看看如何使用这个模拟流程的对象,写个客户端来测试一下。示例代码如下: public class Client { public static void main(String[] args) { // 创建模拟运行流程的对象 FlowAMock mock = new FlowAMock("TestFlow"); //运行流程的第一个阶段 mock.runPhaseOne(); //得到第一个阶段运行所产生的数据,后面要用 int tempResult = mock.getTempResult(); String tempState = mock.getTempState(); //按照方案一来运行流程后半部分 mock.schema1(); //把第一个阶段运行所产生的数据重新设置回去 mock.setTempResult(tempResult); mock.setTempState(tempState); //按照方案二来运行流程后半部分 mock.schema2(); } } 运行结果如下: PhaseOne,Schema1 : now run 3 PhaseOne,Schema2 : now run 3
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值