在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设计模式(十五)--备忘录模式的更多相关文章

  1. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

  2. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...

  3. 重学 Java 设计模式:实战备忘录模式「模拟互联网系统上线过程中,配置文件回滚场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 实现不了是研发的借口? 实现不了,有时候是功能复杂度较高难以实 ...

  4. 【转】设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

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

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

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

  6. 设计模式 ( 十九 ) 模板方法模式Template method(类行为型)

      设计模式 ( 十九 ) 模板方法模式Template method(类行为型) 1.概述 在面向对象开发过程中,通常我们会遇到这样的一个问题:我们知道一个算法所需的关键步骤,并确定了这些步骤的执行 ...

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

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

  8. C#设计模式之二十二备忘录模式(Memeto Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就 ...

  9. C#设计模式之二十二备忘录模式(Memento Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就是对某个对象的状态进行保 ...

  10. Android设计模式(五岁以下儿童)--简单工厂模式

    1.面试的时候问这个问题: 在ListView 的item小程序.很多不同的显示风格.或者是,为了更好地维护,不同的样式,应该怎么做? 我一下就想到的是工厂的模式,利用project,编写ViewFa ...

随机推荐

  1. hdu5884(多叉哈夫曼树)

    hdu5884 题意 给出 n 个数,每次选择不超过 k 个数合并(删掉这些数,加入这些数的和),花费为合并的这些数的和,要求最后只剩下一个数,问 k 最小取多少. 分析 二分 k,合并数的时候可以按 ...

  2. Codevs1026 SEARCH(逃跑的拉尔夫 )(BFS)

    SEARCH 时间限制: 1 Sec  内存限制: 128 MB提交: 11  解决: 4[提交][状态][讨论版] 题目描述 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警 ...

  3. MySQL判断中文字符的方法(转)

    准备: 2.1.环境 MySQL mysql> SHOW VARIABLES LIKE "%version%"; +-------------------------+--- ...

  4. linux 内核学习

    http://www.cnblogs.com/tolimit/category/697314.html

  5. tiny4412 串口驱动分析四 --- 修改默认的串口输出

    作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...

  6. 【Mybatis】未封装返回结果的字段自己返回值的问题

    在spring boot中使用mybatis过程中,发现有个实体的时间字段未在mapper方法执行完的封装结果中进行封装,但是却有值返回. 如下展示问题: 实体如下: package com.sxd. ...

  7. MySQL不能启动 Can't start server : Bind on unix socket: Permission denied

    转载博客地址:http://www.linuxidc.com/Linux/2010-04/25709.htm MySQL服务器突然不能启动,查看最后的启动日志如下: 080825 09:38:04 m ...

  8. Java:网络编程之登陆服务器

    1. 客服端:浏览器(telnet) 服务端:自定义 2. 客服端:浏览器 服务端:TomCat服务器 3. 客服端:自定义 服务端:TomCat服务器   //例子如下: import java.n ...

  9. 面试题:如何在不使用临时变量temp的情况下交换两个整数的值?

    利用一个小技巧,一个整数a在异或另一个整数b两次以后所得的值还是整数a. 具体的过程我们可以自己找两个整数以二进制的形式自己在纸上画一下他们的异或过程.(异或的运算符号为"^") ...

  10. Git历险记(一)

    [编者按]作为分布式版本控制系统的重要代表——Git已经为越来越多的人所认识,它相对于我们熟悉的CVS.SVN甚至同时分布式控制系统的 Mercurial,有哪些优势和不足呢.这次InfoQ中文站有幸 ...