Android设计模式(三)--装饰模式

来源:互联网 发布:淘宝客服写简历 编辑:程序博客网 时间:2024/05/22 16:48

1、定义:

Attach additional responsibilities to an object dynamically keeping the same interface. 
Decoators provide a flexible alternative to subclassing for extending functionality.

在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。



2、装饰模式,本质就是拓展,不改变原有的代码结构,利用setComponent()进行对象的封装,
这样如何使用这个对象就与对象的具体实现隔离开来,每个装饰对象只关心自己的功能,不需要关心是如何添加到这个对象链中。


3、为已有功能动态添加更多功能的方式


4、动态的给对象添加一些额外的职责;


5、比拓展继承与实现,更加灵活!


6、把核心功能与装饰功能分离开来!



一般情况下,我们普通的写法:

package com.example.demo.decorator;/** * 装饰模式 * 普通类 * @author qubian * @data 2015年6月3日 * @email naibbian@163.com * */public abstract class UserInfo {public abstract String getName() ;}

package com.example.demo.decorator;/** * 普通的实现 * @author qubian * @data 2015年6月3日 * @email naibbian@163.com * */public class UserInfoImp extends UserInfo{@Overridepublic String getName() {return "UserInfoImp";}}

现在开始拓展了;

package com.example.demo.decorator;/** * 在不改变父类的情况下, * 进行相应的拓展 * @author qubian * @data 2015年6月3日 * @email naibbian@163.com * */public abstract class Decorator extends UserInfo{private UserInfo pattern;public void SetComponent(UserInfo p){pattern = p;}@Overridepublic String getName() {StringBuilder name= new StringBuilder();if (pattern!=null) {name.append(pattern.getName());}return name.toString();}}

拓展的实现:
package com.example.demo.decorator;/** * 拓展实现类 * @author qubian * @data 2015年6月3日 * @email naibbian@163.com * */public class DecoratorImp extends Decorator{@Overridepublic String getName() {StringBuilder sb = new StringBuilder();sb.append(super.getName());sb.append("DecoratorImp");return sb.toString();}}

具体使用:

package com.example.demo.decorator;import android.util.Log;/** * 使用 * @author qubian * @data 2015年6月3日 * @email naibbian@163.com * */public class UseDecorator {public static String TAG="UseDecorator";public void toUserDecorator(){//普通的使用UserInfo dp = new UserInfoImp();Log.i(TAG, dp.getName());//以下情况使用Decorator模式//1. 需要扩展一个类的功能,或给一个类添加附加职责。//2. 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。//3. 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。//4. 当不能采用生成子类的方法进行扩充时。//一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。//另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。DecoratorImp d = new DecoratorImp();d.SetComponent(dp);Log.i(TAG, d.getName());}}

在Android framework 中,装饰模式也是运用广泛;

参考了网上的一些资料;


1、对于  Service Application Activity 均继承自  ContextWrapper ,而 ContextWrapper 实际上是对 Context 的装饰;

2、是对WindowDecorator 是对Window的装饰,不过,暂时对此没有相应的研究,先写上,以后研究;


public class ContextWrapper extends Context {    Context mBase;    public ContextWrapper(Context base) {        mBase = base;    }        /**     * Set the base context for this ContextWrapper.  All calls will then be     * delegated to the base context.  Throws     * IllegalStateException if a base context has already been set.     *      * @param base The new base context for this wrapper.     */    protected void attachBaseContext(Context base) {        if (mBase != null) {            throw new IllegalStateException("Base context already set");        }        mBase = base;    }    /**     * @return the base context as set by the constructor or setBaseContext     */    public Context getBaseContext() {        return mBase;    }    @Override    public AssetManager getAssets() {        return mBase.getAssets();    }    @Override    public Resources getResources()    {        return mBase.getResources();    }    @Override    public PackageManager getPackageManager() {        return mBase.getPackageManager();    }    @Override    public ContentResolver getContentResolver() {        return mBase.getContentResolver();    }    @Override    public Looper getMainLooper() {        return mBase.getMainLooper();    }        @Override    public Context getApplicationContext() {        return mBase.getApplicationContext();    }        @Override    public void setTheme(int resid) {        mBase.setTheme(resid);    }      }







1 0