Context:

"Dynamic and multiple classification": The implementation of some facet of behavior may be extended during its object's lifetime.

Challenges:

  • To encapsulate the difference in behavior without modifying the subject. To make the extended functionality over the subject substitutable with the subject.
  • To practically replace an object base (setting it to an existing object).
  • To replace supercall by delegation.

Skill:

Participants:

Subject:

The Subject interface. Allows the Decorator to be substitutable for Concrete Subjects.

Concrete Subject:

The object to be decorated. (A passive participant.)

Decorator:

The set of default methods for interfacing for Subject.

Concrete Decorator:

The set of methods that enhance Subject behavior.

Signature:

Decorator is a concrete Subject and references (another) Subject, interfacing for it. Concrete Decorator derives from Decorator.

Notes:

  • A degenerate form without an abstract Decorator is possible but undesirable (see Recursive Object-Structure idiom).
  • In some cases, the decorator replaces (rather than enhances) subject functionality (e.g. Typedef replaces the "name" property of Type).

Used patterns and idioms:

Recursive Object-Structure idiom.

Used by:

(Typically:) parsing, coding and decoding algorithms.

Source:

GOF.

Scope:

General. (Preferably, languages where late binding is obligatory.)