Android設計模式--裝飾模式
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); } }