天天看点

设计模式--结构模式--装饰模式--JavadecoratorintentumlparticipantsConsequence效果JDK-InputStream 分析JDK-Reader 分析

decorator

intent

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

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

uml

设计模式--结构模式--装饰模式--JavadecoratorintentumlparticipantsConsequence效果JDK-InputStream 分析JDK-Reader 分析

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

maintains a reference to a Component object and defines an interface that conforms to Component'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模式使得你可以改变对象的内核。这是改变对象的两种途径。 Proxy模式:控制访问被代理对象。

JDK-InputStream 分析

设计模式--结构模式--装饰模式--JavadecoratorintentumlparticipantsConsequence效果JDK-InputStream 分析JDK-Reader 分析

通过观察FileInputStream构造函数,其不属于decorator。

设计模式--结构模式--装饰模式--JavadecoratorintentumlparticipantsConsequence效果JDK-InputStream 分析JDK-Reader 分析

JDK-Reader 分析

设计模式--结构模式--装饰模式--JavadecoratorintentumlparticipantsConsequence效果JDK-InputStream 分析JDK-Reader 分析