Decorator

From CSSEMediaWiki
Revision as of 22:30, 3 September 2008 by Dominic Winkler (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Decorator pattern makes it possible to add new/additional behavior/functionality to an existing class dynamically. This works by adding a decorator class, which has the same interface as the original (decorated) class. The decorator class wraps the original class which is usually achieved by passing the original object as a parameter to the constructor of the decorator when it is created.

The decorator pattern is an alternative to subclassing. While subclasssing adds behaviour at compile time, the decorator pattern can provide new funtionality at runtime.

Contents

Typical Usage

Participating Classes

Component

Defines the abstract interface of the decorated object which can have added funtionality dynamically.

ConcreteComponent

Defines an object to which additional behaviour can be attached.

Decorator

Maintains a reference to the Component and defines a interface which complies to the Components interface.

ConcreteDecorator

Adds new functionality to the Component

See also

Personal tools