Intent

Provide an abstract definition for a method or a class and redefine its behavior later or on the fly without changing its structure.

Where to use & benefits

Structure


Participants

AbstractClass  (ShapeDecorator)
defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm and implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.

ConcreteClass  (ColorDecorator)
implements the primitive operations ot carry out subclass-specific steps of the algorithm

Example

In the demonstration application, ShapeDecorator class allows the subclass ColorDecorator to define the decorateShape operation, which is used to decorate the shape. But other operations like drawing shape, etc. are carried out by the ShapeDecorator class.

Source Code Links