編輯:關於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{
@Override
public 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;
}
@Override
public 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{
@Override
public 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);
}
}
android開發步步為營之34:四大組件之ContentProvider
ContentProvider,從字面意義上理解,內容提供者,這個類目的就是一個橋梁的作用,讓一個應用的數據(SQLiteDatabase, SharedPreferen
Android組件DrawerLayout仿網易新聞v4.4側滑菜單
概述 今天這篇博客將記錄一些關於DrawerLayout的基本用法,我想關於DrawerLayou
接入微信分享過程的喜和淚
背景故事:4月份從公司回到學校,要開始著手做大四的畢業設計。然而畢設的其中一個功能模塊便是——心情分享模塊,在記錄心情的同時可以把心情分享到朋友圈
Android自定義View之繼承TextView繪制背景
本文實例為大家分享了TextView繪制背景的方法,供大家參考,具體內容如下效果:實現流程:1.初始化:對畫筆進行設置mPaintIn = new Paint();mPa