设计模式(九)备忘录模式


更多设计模式文章请阅读:

设计模式专栏

1.定义:

备忘录模式属于行为型模式,它通过在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。

2.简单描述:

备忘录模式用户保存对象的状态,并且以后恰当时间可以再次恢复到此状态。即可以保存和获取该对象的状态。

3.UML建模图

在这里插入图片描述

UML角色解读:

  • Memoto:备忘录角色,用于保存Originator中的状态
  • Originator:负责创建备忘录,可以记录和恢复自身内部维护的状态对象
  • Caretaker:负责存储备忘录,不对备忘录的内容操作和访问,只能将对象传递给其他对象。

4.简单示例:

备忘录模式以游戏中最为常见,比如玩游戏玩到什么进度,下次进入游戏的时候回直接从上次退出的进度中继续玩。就以该示例为示范,在代码中是如何实现。

  • Memoto对象
/**
 * 备忘录类
 */
public class Memoto {

    /**
     * 用户ID
     */
    public String userId;
    /**
     * 游戏进度
     */
    public int progress;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }
}
  • Caretaker对象
/**
 * 负责Memoto类的存储
 */
public class Caretaker {
    private Memoto mMemoto;

    /**
     * 保存状态
     * @param memoto 备忘录对象
     */
    public void saveMemoto(Memoto memoto){
        this.mMemoto=memoto;
    }

    /**
     * 获取状态对象
     * @return
     */
    public Memoto getMemoto(){
        return mMemoto;
    }

}
  • Originator对象
/**
 * 需要备忘的角色
 */
public class LolGame {

    private String userId;
    private int progress = 0;
    private String monemy;
    private Memoto mMemoto;

    public void play() {

        userId = getUserId();
        ++progress;
        System.out.println("开始玩游戏了...");
        System.out.println("--- userId: " + userId + " --- progress: " +progress+ " ---");

    }

    /**
     * 退出游戏
     */
    public void quit(){
        System.out.println("退出游戏了...");
    }


    /**
     * 创建备忘录对象
     *
     * @return
     */
    public Memoto createMemoto() {
        mMemoto = new Memoto();
        mMemoto.userId = userId;
        mMemoto.progress = progress;
        return mMemoto;
    }

    /**
     * 恢复备忘录
     */
    public void restoreMemoto(Memoto memoto) {
        this.userId = memoto.userId;
        this.progress = memoto.progress;
        System.out.println("----恢复备忘录----");
    }


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

}
  • 游戏类
/**
 * 玩家玩游戏
 */
public class Client {
    public static void main(String[] args) {
        LolGame player = new LolGame();
        player.setUserId("onex");
        player.play();

        Caretaker caretaker =new Caretaker();
        caretaker.saveMemoto(player.createMemoto());

        player.quit();

        player = new LolGame();

        player.restoreMemoto(caretaker.getMemoto());
        player.play();

    }
}
  • 运行结果:
开始玩游戏了...
--- userId: onex --- progress: 1 ---
退出游戏了...
----恢复备忘录----
开始玩游戏了...
--- userId: onex --- progress: 2 ---

Process finished with exit code 0

5.Android中的备忘录设计模式:

在Android开发中,状态模式应用是Activity中的状态保存,即onSaveInstanceState和OnRestoreInstanceState实现ViewGroup和View的状态的保存和恢复

保存Activity的对象即View的状态

 protected void onSaveInstanceState(Bundle outState) {
        outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());

        outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);
        Parcelable p = mFragments.saveAllState();
        if (p != null) {
            outState.putParcelable(FRAGMENTS_TAG, p);
        }
        if (mAutoFillResetNeeded) {
            outState.putBoolean(AUTOFILL_RESET_NEEDED, true);
            getAutofillManager().onSaveInstanceState(outState);
        }
        getApplication().dispatchActivitySaveInstanceState(this, outState);
    }

恢复View(ViewGroup)的状态

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

即Bundle为Memoto对象,Activity为Caretaker对象,View(ViewGroup)为Originator

6.备忘录优缺点:

优点
给对象提供状态可恢复的机制,使得对象方便恢复到历史的状态
实现了信息的封装,使得用户不用关心状态的保存细节

缺点
消耗资源,如果类的成员变量很多及保需要保存的状态太多,会消耗一定的内存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值