Decorator Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) decorator design pattern. By reading this tutorial, you will know how to develop a model for the decorator pattern, and how to apply it in practice.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Decorator.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it Component.

- Right-click on Component and select Model Element Properties > Abstract to set it as abstract.

- Right-click on the Component class and select Add > Operation from the popup menu.

- Name the operation Operation().
- Right-click on Operation and select Model Element Properties > Abstract to set it as abstract.

- Move the mouse cursor over the Component class and drag out Generalization > Class to create a subclass named ConcreteComponent. Repeat this step to create another subclass named Decorator.

- ConcreteComponent should inherit the operations from Component. Select ConcreteComponent, right-click on it, and select Related Elements > Realize all Interfaces from the popup menu.

- Decorator is an abstract class. Right-click on the Decorator class and select Model Element Properties > Abstract to set it as abstract.

- The decorator should also inherit the operations from Component. Select Decorator, right-click on it, and select Related Elements > Realize all Interfaces from the popup menu.

- Move the mouse cursor over the Decorator class and drag out Aggregation > Class to Component. Name the Component's role "component."

- Move the mouse cursor over the Decorator class and drag out Generalization > Class to create a subclass named ConcreteDecorator.
- We will make ConcreteDecorator implement the decorator operation. Select Operation in Decorator.

- Press the Ctrl key and drag to ConcreteDecorator.

- In practice, there may be added behaviors in concrete decorators. To represent this, stereotype the ConcreteDecorator class as PTN Members Creatable. Right-click on ConcreteDecorator and select Stereotypes > Stereotypes... from the popup menu.

-
In the Stereotypes tab of the Class Specification dialog box, select PTN Members Creatable and click > to assign it to the ConcreteDecorator class. Click OK to confirm.
Up to now, the diagram should look like this:
Defining a Pattern
- Select all classes on the class diagram.

- Right-click on the selection and select Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Decorator. Keep the file name as is and click OK to proceed.

Applying a Design Pattern to a Class Diagram
In this section, we will apply the decorator pattern to model a domain model of a diagram editor.
- Create a new project named Diagram Editor.
- Create a class diagram named Domain Model.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Decorator from the list of patterns.

- Click on Component in the overview.

- Rename Component to Shape in the bottom pane, and the Operation operation to Draw.

- Select ConcreteComponent in the overview and rename it to LineShape, and its Operation operation to Draw in the bottom pane.

- Select Decorator in the overview and rename it to ShapeDecorator, and its Operation operation to Draw in the bottom pane.

- Select ConcreteDecorator in the overview and rename it to BorderedShape, and its Operation operation to Draw in the bottom pane.

- We need several operations for additional behaviors in BorderedShape. Click the + button in the bottom pane and select New Operation... from the popup menu.

- In the Operation Specification dialog box, enter RenderBorder as the operation name. Click OK to confirm.

- Click OK to apply the pattern to the diagram. This is the final diagram:
