Decorator pattern allows adding behavior to an object dynamically without affecting the behavior of other objects from the same class (wikipedia).
In this lesson, it is implemented in Parham.
In this example, it is assumed that decorator pattern is used to add decorators to some drawing objects. It deals with two types of objects: Views and Decorators.
A View is a concrete object. A decorator is a decoration to a concrete object. The pattern allows that an arbitrary number of decorators are added to a concerte object.
The decorator pattern is implemented in the DecoratorPattern component. It has two interface classes: View and Decorator. The component expects that Decorator provides a specificDraw method and View provides a View method.
The implementation defines an internal class named Component as the union of View and Decorator. It has an abstract draw method which should be implemented by its sub classes.
Every Decorator has a pointer to a Component. To draw a decorator its draw method should be called. In this method, initially the draw of the decorator component is called and then its specificDarw is called.
As a usage of the above component it is used to add ScrollDecorator and BorderDecorator to Window and TextView classes.
It is done in the App component that parts of it are provided in the front panel. It defines View as the union of Window and TextView, and Decorator as the union of ScrollDecorator and BorderDecorator. It then declares an instance
of DecoratorPattern as follows.
The complete source of the sample can be downloaded from the link provided at the beginning of this example. It has the following files.
View = Window | TextView;
Decorator = ScrollDecorator | BorderDecorator;
extern void Decorator.specificDraw ();
extern void View.draw ();
Component = View | Decorator;
void Component.draw ();
Decorator -> Component component;
Decorator (View view)
component = view;
Decorator (Decorator decorator)
component = decorator;
void Decorator.draw ()