当前位置:编程学习 > JAVA >>

设计模式--结构模式--装饰模式--Java

decorator
 
 
intent
Attachadditional responsibilities to an object dynamically. Decorators provide aflexible alternative to subclassing for extending functionality
为一个类动态地增加一些功能,比通过继承更灵活
uml
participants
Component
definesthe interface for objects that can have responsibilities added to them dynamically.
为其定义功能接口
 
ConcreteComponent
defines an object to which additional responsibilities can be attached
功能具体实现
Decorator
maintainsa reference to a Component object and defines an interface that conforms toComponent's interface
持有component引用,并定义功能接口
ConcreteDecorator
add sresponsibilities to the component.
为component添加实现功能
 
Consequence效果
More flexibility than static inheritance.
比静态继承更灵活
 
Lots of little objects. A design that uses Decorator often results in systems composed of lots of little objects that all look alike.
需要较少的对象。会导致有大量较小的对象。
 
相关模式
•Ad a p t e r模式:D e c o r a t o r模式不同于A d a p t e r模式,因为装饰仅改变对象的职责而不改变它的接口;而适配器将给对象一个全新的接口。
•Co m p o s i t e模式:可以将装饰视为一个退化的、仅有一个组件的组合。然而,装饰仅给对象添加一些额外的职责—它的目的不在于对象聚集。
•St r a t e g y模式:用一个装饰你可以改变对象的外表;而S t r a t e g y模式使得你可以改变对象的内核。这是改变对象的两种途径。
 
JDK-InputStream 分析
 
 
 
 
通过观察FileInputStream构造函数,其不属于decorator。
JDK-Reader 分析
 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,