Java设计模式(7)—— 装饰器

Decorator

 

英文简单描述

Intent
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.

How to
The decorator conforms to the interface of the component it decorates
so that its presence is transparent to the component's clients.

Component
  defines the 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.
ConcreteDecorator
  adds responsibilities to the component.

Known cases
Java IO Stream
GUI

UML

 

好处:

(1)比静态的继承更灵活  [More flexibility than static inheritance]

(2)保持组件轻量级   [Keeping Component classes lightweight]

(3)和策略区别:

A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways of changing an object.

 

代码:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值