Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Decorator.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as 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 ConcreteComponent. Repeat this step to create another subclass Decorator.
- ConcreteComponent should inherit the operations from Component. Select ConcreteComponent, right-click on 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.
- Decorator should also inherit the operations from Component. Select Decorator, right-click on 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 as component.
- Move the mouse cursor over the Decorator class, and drag out Generalization > Class to create a subclass ConcreteDecorator.
- We shall make ConcreteDecorator implements 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 class ConcreteDecorator 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 Component class. Click OK to confirm.
Up to now, the diagram should look like this:
Defining 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 Decorator. Keep the file name as it. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we are going to apply the decorator pattern to model a domain model of diagram editor.
- Create a new project Diagram Editor.
- Create a class diagram 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 at the bottom pane, and operation Operation to Draw.
- Select ConcreteComponent in overview, and rename it to LineShape, and its operation Operation to Draw at the bottom pane.
- Select Decorator in overview, and rename it to ShapeDecorator, and its operation Operation to Draw at the bottom pane.
- Select ConcreteDecorator in overview, and rename it to BorderedShape, and its operation Operation to Draw at the bottom pane.
- We need several operations for additional behaviors in BorderedDecorator. Click on the + button at the bottom pane and select New Operation... from the popup menu.
- In the Operation Specification dialog box, enter RenderBorder as operation name. Click OK to confirm.
- Click OK to apply the pattern to diagram. This is the final diagram: