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.

October 8, 2009
Views: 43,077
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Decorator.
    new diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it Component.
    new component class
  4. Right-click on Component and select Model Element Properties > Abstract to set it as abstract.
    set component abstract
  5. Right-click on the Component class and select Add > Operation from the popup menu.
    add operation
  6. Name the operation Operation().
  7. Right-click on Operation and select Model Element Properties > Abstract to set it as abstract.
    set oper abstract
  8. 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.
    concrete component decorator created
  9. ConcreteComponent should inherit the operations from Component. Select ConcreteComponent, right-click on it, and select Related Elements > Realize all Interfaces from the popup menu.
    realizeInterface
  10. Decorator is an abstract class. Right-click on the Decorator class and select Model Element Properties > Abstract to set it as abstract.
    set decorator abstract
  11. 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.
    realize decorator
  12. Move the mouse cursor over the Decorator class and drag out Aggregation > Class to Component. Name the Component's role "component."
    create association between decorator and component
  13. Move the mouse cursor over the Decorator class and drag out Generalization > Class to create a subclass named ConcreteDecorator.
  14. We will make ConcreteDecorator implement the decorator operation. Select Operation in Decorator.
    select oper
  15. Press the Ctrl key and drag to ConcreteDecorator.
    concrete decorator
  16. 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.
    stereotype concrete decorator
  17. 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.
    set members creatable
    Up to now, the diagram should look like this:
    set concrete decorator members creatable

Defining a Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    define design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name as Decorator. Keep the file name as is and click OK to proceed.
    name decorator

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.

  1. Create a new project named Diagram Editor.
  2. Create a class diagram named Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Decorator from the list of patterns.
    select decorator
  5. Click on Component in the overview.
    select component
  6. Rename Component to Shape in the bottom pane, and the Operation operation to Draw.
    rename component
  7. Select ConcreteComponent in the overview and rename it to LineShape, and its Operation operation to Draw in the bottom pane.
    rename concrete component
  8. Select Decorator in the overview and rename it to ShapeDecorator, and its Operation operation to Draw in the bottom pane.
    rename decorator
  9. Select ConcreteDecorator in the overview and rename it to BorderedShape, and its Operation operation to Draw in the bottom pane.
    rename concrete decorator
  10. We need several operations for additional behaviors in BorderedShape. Click the + button in the bottom pane and select New Operation... from the popup menu.
    new oper
  11. In the Operation Specification dialog box, enter RenderBorder as the operation name. Click OK to confirm.
    render border
  12. Click OK to apply the pattern to the diagram. This is the final diagram:
    result