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 ...
随机推荐
- 818D - Multicolored Cars
818D - Multicolored Cars 题意 在 1 到 n 时刻,有 n 量有颜色的车通过,用数字表示颜色,Alice 选择一个颜色A,要求 Bob 选择一个颜色B,使得对于任意时刻 cn ...
- Hibernate使用Criteria去重distinct+分页
写在前面: 最近在项目中使用了Criteria的分页查询,当查询的数据没有重复的记录还好,但是当数据有关联并出现重复记录的时候,就要去重,那么就会出现查询的记录数与实际的不一致的问题.这里也记录一下解 ...
- Android开发之onCreate
最近看了一些开发人员做的APP开发,虽然都实现了比较完美的功能,不过很多地方很是牵强!我觉得:作为一个合格软件工程师,最重要的不是你写过多少代码,而是你研究过多少代码!所以,我更倡导多研究系统原本定制 ...
- linux 之创建文件命令
1.vi vi 1.txt 会直接创建并打开一个文件1.txt 2.touch touch的作用是更改一个文件或目录的时间.touch 2.txt 如果2.txt不存在,则创建空文件2.txt 3.e ...
- oracle 被另一个用户锁定
于是我就直接在上面改字段,在点打钩(记入改变)的时候提示,记录被另一个用户锁住,一开始还以为整个表被锁住了,后来发现,仅仅是这个字段不能改变,其他的字段可以. 网上找了资料,发现是:当多个用户并发地存 ...
- apache 限制某些目录不能访问通过rewrite实现
通过deny allow访问控制肯定是可以实现的单个目录,但是这个必须指定准确的目录,如果有很多个目录,但是都包含某个名字,比如bbs.1.com/1/tmp/123.htmlbbs.1.com/2/ ...
- crossapp的屏幕适配
1.分辨率是的某个尺寸大小的屏幕里的像素点数ppi 2.crossapp茶用iphone4为基准比例值为1 3.其它分辨率设备的换算dp = px * 320/ 屏幕PPI 4.crossapp里点. ...
- JS方面重点摘要(二)
1.函数声明与函数表达式 (1)变量声明会置顶提前,但赋值仍在原地方(2)函数声明同变量声明一样会提前:但是,函数表达式没有提前,就相当于平时的变量赋值(3)函数声明会覆盖变量声明,但不会覆盖变量赋值 ...
- reduceByKey和groupByKey的区别
先来看一下在PairRDDFunctions.scala文件中reduceByKey和groupByKey的源码 /** * Merge the values for each key using a ...
- How to support both ipv4 and ipv6 address for JAVA code.
IPv6 have colon character, for example FF:00::EEIf concatenate URL String, IPv6 URL will like: http: ...