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();

}






原文地址:https://www.cnblogs.com/wzjhoutai/p/6781124.html