首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

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

2013-03-10 
设计模式--结构模式--装饰模式--JavadecoratorintentAttachadditional responsibilities to an object dyn

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

decorator

intent

Attachadditional responsibilities to an object dynamically. Decorators provide aflexible alternative to subclassing for extending functionality

为一个类动态地增加一些功能,比通过继承更灵活

uml

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

participants

Componentdefinesthe interface for objects that can have responsibilities added to them dynamically.为其定义功能接口

ConcreteComponentdefines an object to which additional responsibilities can be attached功能具体实现

Decoratormaintainsa reference to a Component object and defines an interface that conforms toComponent's interface持有component引用,并定义功能接口

ConcreteDecoratoradd 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 分析
设计方式-结构模式-装饰模式-Java



通过观察FileInputStream构造函数,其不属于decorator。设计方式-结构模式-装饰模式-Java
JDK-Reader 分析
设计方式-结构模式-装饰模式-Java

热点排行