Android设计模式(十五)--备忘录模式
在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();
}
Android设计模式(十五)--备忘录模式的更多相关文章
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- 重学 Java 设计模式:实战备忘录模式「模拟互联网系统上线过程中,配置文件回滚场景」
作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 实现不了是研发的借口? 实现不了,有时候是功能复杂度较高难以实 ...
- 【转】设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成 ...
- 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)
设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...
- 设计模式 ( 十九 ) 模板方法模式Template method(类行为型)
设计模式 ( 十九 ) 模板方法模式Template method(类行为型) 1.概述 在面向对象开发过程中,通常我们会遇到这样的一个问题:我们知道一个算法所需的关键步骤,并确定了这些步骤的执行 ...
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...
- C#设计模式之二十二备忘录模式(Memeto Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就 ...
- C#设计模式之二十二备忘录模式(Memento Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就是对某个对象的状态进行保 ...
- Android设计模式(五岁以下儿童)--简单工厂模式
1.面试的时候问这个问题: 在ListView 的item小程序.很多不同的显示风格.或者是,为了更好地维护,不同的样式,应该怎么做? 我一下就想到的是工厂的模式,利用project,编写ViewFa ...
随机推荐
- Matlab梯度下降及正规方程实现多变量的线性回归
如果需要代做算法,可以联系我...博客右侧有联系方式. 一.相关概念 1.梯度下降 由于Z= X*theta - y是列向量,所以Z'*Z就是平方和连加,就是2范数:如果Z是矩阵呢,那么Z'*Z的对角 ...
- 【Linux】CentOS7上安装google谷歌浏览器
1.首先进入根目录,然后进入etc/yum.repos.d目录下,创建google-chrome.repo文件 cd / cd etc/yum.repos.d vim google-chrome.re ...
- django admin后台编辑页面 显示数据公式
先用django markdownx进行实时预览, 然后在 /Library/Python/2.7/site-packages/django/contrib/admin 这个目录下,加入 <sc ...
- python 实现创建文件夹和创建日志文件
一.实现创建文件夹和日志 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: nulige import os import datetime ...
- Mybatis通用分页
分页分为真分页和假分页,而 MyBatis 本身没有提供基于数据库方言的分页功能,而是基于 JDBC 的游标分页,很容易出现性能问题.网上提供的一个解决方案感觉还不错,是基于 MyBatis 本身的插 ...
- debug模式下dlgdata.cpp line 43 断言失败
我在VC6下显示Line 43, Line 624行失败 网上有Line 40行猜测是其他版本 运行程序出错,定位如下: HWND CDataExchange::PrepareCtrl(int nID ...
- SpringMVC文件上传的配置
记述一下步骤以备查. 准备工作: 需要把Jakarta Commons FileUpload及Jakarta Commons io的包放lib里. 我这边的包是: commons-fileupload ...
- Linux服务器安全登录设置
在日常运维工作中,对加固服务器的安全设置是一个机器重要的环境.比较推荐的做法是:1)严格限制ssh登陆(参考:Linux系统下的ssh使用(依据个人经验总结)): 修改ssh默认监听端口 ...
- Laravel之HTTP相应
一.基本相应示例 1.返回简单字符串 Route::get('/', function () { return 'Hello World'; }); 给定的字符串会被框架自动转化为 HTTP 响应 2 ...
- 虚拟机只有IPv6,没有ipv4
1.修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none HWADDR=00:0C:29:20: ...