装饰者模式

转载 2015年07月09日 11:44:03

原文地址:http://www.oodesign.com/decorator-pattern.html


Decorator Pattern


Motivation

Extending an object�s functionality can be done statically (at compile time) by using inheritance however it might be necessary to extend an object�s functionality dynamically (at runtime) as an object is used.

Consider the typical example of a graphical window. To extend the functionality of the graphical window for example by adding a frame to the window, would require extending the window class to create a FramedWindow class. To create a framed window it is necessary to create an object of the FramedWindow class. However it would be impossible to start with a plain window and to extend its functionality at runtime to become a framed window.


Intent

  • The intent of this pattern is to add additional responsibilities dynamically to an object.

Implementation

The figure below shows a UML class diagram for the Decorator Pattern:
Decorator Pattern Implementation - UML Class Diagram The participants classes in the decorator pattern are:
  • Component - Interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent - Defines an object to which additional responsibilities can be added.
  • Decorator - Maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • Concrete Decorators - Concrete Decorators extend the functionality of the component by adding state or adding behavior.

Description

The decorator pattern applies when there is a need to dynamically add as well as remove responsibilities to a class, and when subclassing would be impossible due to the large number of subclasses that could result.


Applicability & Examples



Example - Extending capabilities of a Graphical Window at runtime

Decorator Pattern Example - UML Class Diagram

In Graphical User Interface toolkits windows behaviors can be added dynamically by using the decorator design pattern.


Specific problems and implementation


Graphical User Interface Frameworks

GUI toolkits use decoration pattern to add functionalities dynamically as explained before.


Related Patterns

  • Adapter Pattern - A decorator is different from an adapter in that a decorator changes object's responsibilities, while an adapter changes an object interface.
  • Composite Pattern - A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilities.

Consequences

  • Decoration is more convenient for adding functionalities to objects instead of entire classes at runtime. With decoration it is also possible to remove the added functionalities dynamically.
  • Decoration adds functionality to objects at runtime which would make debugging system functionality harder.

Known Uses:

  • GUI toolkits as has been previously explained.

装饰者模式Android实例

  • 2016年07月30日 09:58
  • 6.77MB
  • 下载

装饰者模式

  • 2014年06月09日 13:01
  • 5KB
  • 下载

装饰者模式之servlet过滤器(Filter)网页压缩

1.装饰者模式定义动态地给一个对象添加一些额外的职责。就增加功能来说, 装饰模式相比生成子类更为灵活。 ...

装饰者模式 - PHP版

  • 2014年05月21日 09:45
  • 2KB
  • 下载

具有装饰者的模式照片

  • 2009年06月14日 00:46
  • 1.78MB
  • 下载

装饰者模式(Decorator Pattern)(二):HeadFirst中咖啡添加调料

一、问题描述 为星巴兹咖啡连锁店设计饮料菜单,咖啡可加的调料有豆浆、牛奶、摩卡等。可在咖啡的基础上加入不同的调料,星巴兹会根据所加的调料收取不同的费用,要注意到,以后可能有新的调料被加入进来...

装饰者模式案例学习代码

  • 2013年03月03日 01:07
  • 2KB
  • 下载

装饰者模式源代码java

  • 2014年01月18日 21:59
  • 3KB
  • 下载

3.装饰者模式(Decorator Pattern)

在装饰者模式一章中,Head Fist一书主要是通过“星巴兹咖啡馆”的例子,来讲解优化代码,并将装饰者模式自然而然的融入进去。 本例粗略介绍:有一家名叫“星巴兹”的咖啡连锁店,因扩张速度过快,要...

设计模式-装饰者

  • 2015年11月07日 10:44
  • 4.44MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:装饰者模式
举报原因:
原因补充:

(最多只允许输入30个字)