Android与设计模式——装饰者(Decorator)模式
2014-11-28 19:04
288 查看
在阎宏博士的《JAVA与模式》一书中开头是这样描述装饰(Decorator)模式的:
装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
装饰模式的结构
装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。(上文来源于网络)
装饰模式的类图如下:
在装饰模式中的角色有:
● 抽象构件(Context)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
● 具体构件(ContextImpl)角色:定义一个将要接收附加责任的类。
● 装饰(ContextWrapper)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
● 具体装饰(Activity/Service/Application)角色:负责给构件对象“贴上”附加的责任。
ContextImpl是抽象类Context的具体实现,ContextWrapper及所有其子类对象持有的Context均是ContextImpl对象。以Activity的Context为例,Activity创建是在ActivityThread中,Activity创建时:
createBaseContextForActivity()方法返回ContextImpl对象后,通过activity.attach()将ContextImpl对象与Activity关联起来。
装饰模式和代理模式的区别:
装饰模式:以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案;
代理模式:给一个对象提供一个代理对象,并有代理对象来控制对原有对象的引用;
装饰模式应该为所装饰的对象增强功能;代理模式对代理的对象施加控制,并不提供对象本身的增强功能。
二者的实现机制确实是一样的,可以看到他们的实例代码重复是很多的。但就语义上说,这两者的功能是相反的,模式的一个重要作用是简化其他程序员对你程序的理解,
你在一个地方写装饰,大家就知道这是在增加功能,你写代理,大家就知道是在限制,虽然代码很可能相同,但如果你都叫他们装饰,别人会很迷惑的。(转)
未完待续,有不对的地方,请指正。
装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。
装饰模式的结构
装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。(上文来源于网络)
装饰模式的类图如下:
在装饰模式中的角色有:
● 抽象构件(Context)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
● 具体构件(ContextImpl)角色:定义一个将要接收附加责任的类。
● 装饰(ContextWrapper)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
● 具体装饰(Activity/Service/Application)角色:负责给构件对象“贴上”附加的责任。
ContextImpl是抽象类Context的具体实现,ContextWrapper及所有其子类对象持有的Context均是ContextImpl对象。以Activity的Context为例,Activity创建是在ActivityThread中,Activity创建时:
public final class ActivityThread {
....... private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { ...... if (activity != null) { Context appContext = createBaseContextForActivity(r, activity); CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager()); Configuration config = new Configuration(mCompatConfiguration); if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity " + r.activityInfo.name + " with config " + config); activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstances, config);
....... }
........ }
private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) { ContextImpl appContext = new ContextImpl(); appContext.init(r.packageInfo, r.token, this); appContext.setOuterContext(activity); // For debugging purposes, if the activity's package name contains the value of // the "debug.use-second-display" system property as a substring, then show // its content on a secondary display if there is one. Context baseContext = appContext; String pkgName = SystemProperties.get("debug.second-display.pkg"); if (pkgName != null && !pkgName.isEmpty() && r.packageInfo.mPackageName.contains(pkgName)) { DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance(); for (int displayId : dm.getDisplayIds()) { if (displayId != Display.DEFAULT_DISPLAY) { Display display = dm.getRealDisplay(displayId, r.token); baseContext = appContext.createDisplayContext(display); break; } } } return baseContext; } }
createBaseContextForActivity()方法返回ContextImpl对象后,通过activity.attach()将ContextImpl对象与Activity关联起来。
装饰模式和代理模式的区别:
装饰模式:以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案;
代理模式:给一个对象提供一个代理对象,并有代理对象来控制对原有对象的引用;
装饰模式应该为所装饰的对象增强功能;代理模式对代理的对象施加控制,并不提供对象本身的增强功能。
二者的实现机制确实是一样的,可以看到他们的实例代码重复是很多的。但就语义上说,这两者的功能是相反的,模式的一个重要作用是简化其他程序员对你程序的理解,
你在一个地方写装饰,大家就知道这是在增加功能,你写代理,大家就知道是在限制,虽然代码很可能相同,但如果你都叫他们装饰,别人会很迷惑的。(转)
未完待续,有不对的地方,请指正。
相关文章推荐
- Android设计模式之一个例子让你彻底明白装饰者模式(Decorator Pattern)
- Android设计模式之装饰者模式(Decorator Pattern)
- Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)
- 设计模式-装饰者模式(Decorator)理解和在Android中的应用
- 设计模式随笔系列:来杯咖啡-装饰者模式(Decorator)[转]
- 设计模式之C++实现(三)---装饰者模式(Decorator)
- Android 设计模式:(三)装饰者模式 —— 装饰对象
- Android 设计模式:(三)装饰者模式 —— 装饰对象
- Net设计模式实例之装饰者模式(Decorator Pattern)
- 设计模式笔记之装饰者模式(Decorator Pattern)
- 设计模式——装饰者模式(Decorator)
- 设计模式笔记3:装饰者模式(Decorator Pattern)
- 设计模式示例二 Decorator(装饰者)
- 设计模式循序渐进(2)装饰者模式 Decorator
- (C++)设计模式------装饰者模式 decorator
- 设计模式学习之装饰者模式(Decorator)
- 设计模式随笔系列:来杯咖啡-装饰者模式(Decorator)[转]
- 深入浅出设计模式-003:装饰者模式(Decorator Pattern)
- Android 设计模式:(三)装饰者模式 —— 装饰对象
- Java设计模式:装饰者模式(Decorator)