原版设计模式之装饰器(Decorator)

Intent (定义)

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality(动态的赋予对象额外的职责。装饰者们扩展对象的功能,而不需要将对象子类化。

Also Known As (别名)

Wrapper

Motivation (更多释义)

以图形绘画工具为例。
有时,希望将额外的功能添加到单个对象,而不是整个类中。
例如,希望给任何用户界面都添加边框,滚动这些行为。
有一个选择是使用继承,继承边框和滚动的抽象,并为每个界面都编写边框和滚动的代码,但这个是不灵活,而且客户端也无法控制何时,如何去用边框和滚动的子类来装饰图形界面。

A more flexible approach is to enclose the component in another object that adds the border. (更灵活的方案是将组件封装到另一个添加边框的对象中。
The enclosing object is called a decorator.(这个封装对象被称为装饰者
The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component’s clients.(装饰器和被装饰的组件方法一致,因此对客户端来说是透明的。
The decorator forwards requests to the component and may
perform additional actions (such as drawing a border) before or after forwarding.(装饰器将请求转发给组件,也可以在转发之前或者之后做一些额外的动作【例如,绘制边框】
Transparency lets you nest decorators recursively, thereby allowing an unlimited number of added responsibilities.(因为对客户端透明,所以就可以递归的嵌套装饰者,因此也可以无限的添加一系列责任。
在这里插入图片描述
The following object diagram shows how to compose a TextView object with BorderDecorator and ScrollDecorator objects to produce a bordered, scrollable text view:(如下展示了如何用BorderDecorator和ScrollDecorator对象构成TextView对象,从而可以展示一个有边框,可滚动的文本界面。
The ScrollDecorator and BorderDecorator classes are subclasses of Decorator, an abstract class for visual components that decorate other visual components.(ScrollDecorator和BorderDecorator类是Decorator的子类,Decorator是用来装饰其他视觉组件的抽象视觉组件类。
在这里插入图片描述
VisualComponent is the abstract class for visual objects. It defines their drawing and event handling interface. Note how the Decorator class simply forwards draw requests to its component, and how Decorator subclasses can extend this operation.(VisualComponent是视觉对象的抽象类。定义了图形和事件处理接口。注意,Decorator类如何将绘制请求转发给其他组件,Decorator的子类又是如何扩展这些操作的。
Decorator subclasses are free to add operations for specific functionality.(装饰器子类可以自由的为特定功能添加操作。
For example, ScrollDecorator’s ScrollTo operation lets other objects scroll the interface if they know there happens to be a ScrollDecorator object in the interface.(例如,在界面中利用ScrollDecorator类,ScrollDecorator的滚动操作能让其他对象滚动界面。
The important aspect of this pattern is that it lets decorators appear anywhere a VisualComponent can. That way clients generally can’t tell the difference between a decorated component and an undecorated one, and so they don’t depend at all on the decoration.(这个模式最重要的方面就是,装饰者们可以出现在任何VisualComponent能出现的地方。这意味着客户端不知道有装饰者的组件和无装饰者组件的区别,也就不依赖任何装饰者。
参考代码

Applicability (适用点)

  • to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.(动态且透明化的向独立对象添加职责,并且不影响其他对象。
  • for responsibilities that can be withdrawn.(可撤销的职责
  • when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing(当扩展子类是不切实际的。有时需要大量独立的扩展,并且会产生大量子类来支持每种组合,或者类定义是隐藏的,或者根本无法进行子类化。

Structure (结构)

在这里插入图片描述

Participants (类说明)

  • Component (VisualComponent) (组件
    • defines the interface for objects that can have responsibilities added to them dynamically.(为动态添加功能的对象定义接口
  • ConcreteComponent (TextView) (具体组件
    • 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对象,并定义一个和Component方法功能一样的方法。
  • ConcreteDecorator (BorderDecorator, ScrollDecorator) (具体装饰者
    • adds responsibilities to the component.(给组件添加额外的功能

Collaborations (约定)

Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.(Decorator将请求转发给Component对象。在转发前后可以添加额外的操作。)

其中一个流案例:
在这里插入图片描述

Related Patterns (相关模式)

Adapter (157): A decorator is different from an adapter in that a decorator only changes an object’s responsibilities, not its interface; an adapter will give an object a completely new interface.(装饰者和适配者的区别在于,装饰者仅修改对象的功能,而不是方法。适配者将给对象一个全新的方法。【两者实现的接口不同导致的】

Composite (183): A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilities—it isn’t intended for object aggregation.(装饰者可以被看作是只有一个组件的简易版组合模式。但是装饰者添加额外的功能,而不是用于对象聚合。

Strategy (349): 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.(装饰者可以改变对象的外部,策略模式可以改变内部。这是两种可互相替换改变对象的方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值