在Android中用于保存Activity状态的onSaveInstanceState()和恢复Activity状态的onRestoreInstanceState(),

这样的算不算是一种备忘录模式呢?

1、定义:
在不破坏封装的情况下,捕获对象的内部状态,并在对象之外保存这个状态,这样以后就能够恢复以后保存的状态。

2、使用:
备忘录模式,比較适合用于功能复杂,可是须要维护和纪录历史的类,或者是须要保存一个或者是多个属性的类,

在未来某个时段须要时,将其还原到原来纪录的状态;

Originator能够依据保存的Memento还原到前一状态;

3、其它:

备忘录模式又称之为:快照模式(Snapshot Pattern)或Token模式,是对象的行为模式;

4、简单的demo:

首先是须要处理的对象数据:

package com.example.demo.Memento;
/*** 对象* @author qubian* @data 2015年6月20日* @email naibbian@163.com**/
public class Bean {private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public Memento createMemento(String name,String age){return new Memento(name, age);}public void restore(Memento memento){this.name=memento.getName();this.age= memento.getAge();}
}

备忘数据对象:

1、这个能够存储和被处理的对象一样的数据,也能够依据须要改动,设置自己的数据;

2、须要明白的功能不过为了存储和恢复被处理的对象。故当中的数据能够任意约定,

3、那么。问题来了。这个备份的数据,一般都是存储在内存中。用于恢复对象,那么假设将被处理的对象,序列化,或者是运用反射等技术用于存储和恢复,那么这样存储在磁盘中。这样是否有意义。或者是违背了这种设计模式呢?

4、也就是原来的问题。在Android中onSaveInstanceState中的数据。一般都是使用的Android的存储方式,是为了在Activity在内存中销毁后的恢复问题。那么备忘录模式中存储在内存的对象,在此时,似乎就没有什么意义了!

package com.example.demo.Memento;
/*** 备忘录 备忘数据* @author qubian* @data 2015年6月20日* @email naibbian@163.com**/
public class Memento {private String name;private String age;public Memento(String name,String age){this.name=name;this.age= age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}

备忘录管理者以及使用者:

package com.example.demo.Memento;
/*** 备忘录模式* 管理者 * @author qubian* @data 2015年6月20日* @email naibbian@163.com**/
public class MementoManager {private Memento memento;public Memento getMemento() {return memento;}public void setMemento(Memento memento) {this.memento = memento;}}package com.example.demo.Memento;public class UseMemento {public void use(){Bean bean  =new Bean();bean.setName("张三");bean.setAge("22");// 保存状态MementoManager manager  = new MementoManager();manager.setMemento(bean.createMemento(bean.getName(), bean.getAge()));// 改变状态bean.setAge("23");//恢复原来地状态bean.restore(manager.getMemento());}
}

在管理者当中,备忘数据对象Memento。能够放在管理者中统一管理;

在管理者中,也能够存在多种状态的Memento。上样例中。只存放了一个简单的状态;

5、在备忘录模式的定义中,是说。在此对象之外保存这个对象的状态,那么,假设这么说来。存在内存和磁盘中,然后处理后返回原来的对象数据,这样似乎也都是一种备忘录模式咯?!

6、Android:

1、那么假设这么说来。Activity 本身就用到了这种设计模式了。

2、在横竖屏切换的时候,线程Thread会又一次启动,这个问题是横竖屏切换的时候须要处理的。那么,我们在此须要也是须要考虑的这么模式。就是线程重新启动的时候。线程中的数据。我们也是肯定须要用到这个模式的,用来保存原来的数据。

3、在JNI 调用本地数据中的Canvas中的Save() 和Restore()这两个本地JNI 代码中是否也运用这种设计模式呢?!

public class Canvas {/*** Saves the current matrix and clip onto a private stack. Subsequent* calls to translate,scale,rotate,skew,concat or clipRect,clipPath* will all operate as usual, but when the balancing call to restore()* is made, those calls will be forgotten, and the settings that existed* before the save() will be reinstated.** @return The value to pass to restoreToCount() to balance this save()*/public native int save();/*** Based on saveFlags, can save the current matrix and clip onto a private* stack. Subsequent calls to translate,scale,rotate,skew,concat or* clipRect,clipPath will all operate as usual, but when the balancing* call to restore() is made, those calls will be forgotten, and the* settings that existed before the save() will be reinstated.** @param saveFlags flag bits that specify which parts of the Canvas state*                  to save/restore* @return The value to pass to restoreToCount() to balance this save()*/public native int save(int saveFlags);/*** This behaves the same as save(), but in addition it allocates an* offscreen bitmap. All drawing calls are directed there, and only when* the balancing call to restore() is made is that offscreen transfered to* the canvas (or the previous layer). Subsequent calls to translate,* scale, rotate, skew, concat or clipRect, clipPath all operate on this* copy. When the balancing call to restore() is made, this copy is* deleted and the previous matrix/clip state is restored.** @param bounds May be null. The maximum size the offscreen bitmap*               needs to be (in local coordinates)* @param paint  This is copied, and is applied to the offscreen when*               restore() is called.* @param saveFlags  see _SAVE_FLAG constants* @return       value to pass to restoreToCount() to balance this save()*/public int saveLayer(RectF bounds, Paint paint, int saveFlags) {return native_saveLayer(mNativeCanvas, bounds,paint != null ? paint.mNativePaint : 0,saveFlags);}/*** This call balances a previous call to save(), and is used to remove all* modifications to the matrix/clip state since the last save call. It is* an error to call restore() more times than save() was called.*/public native void restore();}

转载于:https://www.cnblogs.com/wzjhoutai/p/6781124.html

Android设计模式(十五)--备忘录模式相关推荐

  1. 设计模式之外观模式php,php设计模式(十五)外观模式

    外观模式又叫门面模式: 现在都是模块化开发了: 开发中很多时候都是在使用各种扩展包: 或者在外观模式中我们叫做子系统: 外观模式的作用就是减少子系统之间的耦合: 降低子系统的使用难度: 我们举个栗子: ...

  2. 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...

  3. 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)

    设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的有用 ...

  4. 从王者荣耀看设计模式(二十二.备忘录模式)

    从王者荣耀看设计模式 一.简介 点击王者荣耀商店法术的装备栏,存在有"贤者的庇护"这一装备.在对战中,当玩家处于死亡状态时,会触发此装备的效果,能够让英雄重新复活. ┬┴┬┌─ ● ...

  5. Java进阶篇设计模式之十二 ---- 备忘录模式和状态模式

    前言 在上一篇中我们学习了行为型模式的策略模式(Strategy Pattern)和模板模式(Template Pattern).本篇则来学习下行为型模式的两个模式,备忘录模式(Memento Pat ...

  6. Net设计模式实例之备忘录模式(Memento Pattern)

    一.备忘录模式简介(Brief Introduction) 备忘录模式(Memento Pattern),在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以就该对 ...

  7. IOS设计模式之四(备忘录模式,命令模式)

    本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq) ...

  8. 设计模式读书笔记-----备忘录模式

    个人比较喜欢玩单机游戏,什么仙剑.古剑.鬼泣.使命召唤.三国无双等等一系列的游戏我都玩过(现在期待凡人修仙传),对于这些游戏除了剧情好.场面大.爽快之外,还可以随时存档,等到下次想玩了又可以从刚开始的 ...

  9. Java描述设计模式(24):备忘录模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 常见的视频播放软件都具备这样一个功能:假设在播放视频西游记,如果这时候切换播放视频红楼梦,当再次切回播放西游记时, ...

  10. java备忘录模式应用场景_Java描述设计模式(24):备忘录模式

    一.生活场景 1.场景描述 常见的视频播放软件都具备这样一个功能:假设在播放视频西游记,如果这时候切换播放视频红楼梦,当再次切回播放西游记时,视频会从上次切走的时间点继续播放.下面基于备忘录设计模式来 ...

最新文章

  1. Answer:关于C#连续赋值的面试题
  2. python小程序-【Python精华】100个Python练手小程序
  3. 等待指定时间后自动跳转或关闭当前页面
  4. Pytorch基础知识整理(六)参数初始化
  5. 在js中if条件为null/undefined/0/NaN/表达式时,统统被解释为false,此外均为true
  6. 我们该如何学习机器学习中的数学
  7. Junit 4.x 单元测试,参数化测试,套件测试 实例
  8. 计算机维修技能训练,计算机维护维修技能训练指导书【参考】.doc
  9. 博为峰Java技术文章 ——JavaSE Swing 如何使用进度条组件JProgressBarⅡ
  10. Github html文件在线预览方法
  11. 多台云服务器中Redis的主从复制
  12. 【1024送书】21届校招薪资提前爆料!大厂全部上涨!美团时薪或成最香!
  13. html滑动验证,html5移动端按住滑块拖动验证代码
  14. html单元格下拉菜单怎么做,Excel 2013如何制作下拉菜单?(excel下拉菜单怎么做?)...
  15. 【计算机基础】HTTP 超文本传输协议
  16. linux命令行打开写字板,在Linux操作系统中使用手写板
  17. mixin(公共样式定义)
  18. 浅析MySQL死锁检测
  19. 关键词搜图、截图小助手——有了它妈妈再也不用担心我数据集啦
  20. Linux内核编译出错

热门文章

  1. Java成神之路——重入锁、公平非公平锁、自旋锁、读写锁
  2. oracle 获取字段某个字段的下标,go获取字符串特定下标中间的字符,正则查找和替换字符串中特定字符...
  3. csnd ftp服务器端java_数据包取证总结 - osc_r6zeu2c7的个人空间 - OSCHINA - 中文开源技术交流社区...
  4. mobilenet cpu 加速_(300223)半导体+芯片概念,净利润增长408%!加速上涨!
  5. shell脚本执行命令错误处理
  6. lisp抛物线插值_抛物线插值法
  7. mybatisplus查询今天的数据_springboot集成mybatisPlus
  8. 【kudu】未解决 Flink 读取 kudu KuduReaderIterator not found it may have expired
  9. 【Elasticsearch】实施现代电子商务搜索
  10. Spring : Factories这个是什么