装饰者模式
来源:互联网 发布:sql修改约束条件 编辑:程序博客网 时间:2024/06/16 01:41
原文地址:http://www.oodesign.com/decorator-pattern.html
Decorator Pattern
Motivation
Extending an object�s functionality can be done statically (at compile time) by using inheritance however it might be necessary to extend an object�s functionality dynamically (at runtime) as an object is used.
Consider the typical example of a graphical window. To extend the functionality of the graphical window for example by adding a frame to the window, would require extending the window class to create a FramedWindow class. To create a framed window it is necessary to create an object of the FramedWindow class. However it would be impossible to start with a plain window and to extend its functionality at runtime to become a framed window.
Intent
- The intent of this pattern is to add additional responsibilities dynamically to an object.
Implementation
The figure below shows a UML class diagram for the Decorator Pattern:The participants classes in the decorator pattern are:
- Component - Interface for objects that can have responsibilities added to them dynamically.
- ConcreteComponent - Defines an object to which additional responsibilities can be added.
- Decorator - Maintains a reference to a Component object and defines an interface that conforms to Component's interface.
- Concrete Decorators - Concrete Decorators extend the functionality of the component by adding state or adding behavior.
Description
The decorator pattern applies when there is a need to dynamically add as well as remove responsibilities to a class, and when subclassing would be impossible due to the large number of subclasses that could result.
Applicability & Examples
Example - Extending capabilities of a Graphical Window at runtime
In Graphical User Interface toolkits windows behaviors can be added dynamically by using the decorator design pattern.
Specific problems and implementation
Graphical User Interface Frameworks
GUI toolkits use decoration pattern to add functionalities dynamically as explained before.
Related Patterns
- Adapter Pattern - A decorator is different from an adapter in that a decorator changes object's responsibilities, while an adapter changes an object interface.
- Composite Pattern - A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilities.
Consequences
- Decoration is more convenient for adding functionalities to objects instead of entire classes at runtime. With decoration it is also possible to remove the added functionalities dynamically.
- Decoration adds functionality to objects at runtime which would make debugging system functionality harder.
Known Uses:
- GUI toolkits as has been previously explained.
- 装饰者模式(Derector)
- 装饰者模式
- Decorator 装饰者模式
- 装饰者设计模式
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式(Decorator)
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式 DecoratorPattern
- 设计模式 - 装饰者
- 装饰者模式
- 装饰者模式
- 装饰者模式 - 2
- 装饰者模式
- 装饰者模式
- CGRectInset CGRectoffset UIEdgeInsetsInsetRect 这三个函数的使用
- stripslashes和addslashes的区别
- Android官方培训课程中文版
- 【编程习题★★★☆☆】计算岛屿的数量
- 极课 good
- 装饰者模式
- 判断当前一个物料被其他人锁定
- Android如何实现左边侧滑界面SlidingMenu
- 推荐系统-埋点
- IM收藏
- TextView/EditText字体阴影 ,自动换行,焦点获取,输入法回车键前往,自定义光标
- ios开发Keyboard Type及其截图
- Scala集合笔记
- CocoaPods详解之使用篇