Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Template.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as AbstractClass.
- Right-click on AbstractClass, and select Model Element Properties > Abstract to set it as abstract.
- Right-click on the AbstractClass class, and select Add > Operation from the popup menu.
- Name the operation TemplateMethod().
- Create another operation PrimitiveOperation1().
- Right-click on PrimitiveOperation1, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the AbstractClass class, and drag out Generalization > Class to create subclasses ConcreteClass.
- We need to make the concrete classes inherit operations from the abstract class. Right-click on ConcreteClass and select Related Elements > Realize all Interfaces from the popup menu.
- In practice, there may be multiple concrete strategies. To represent this, stereotype the class ConcreteClass, as PTN Cloneable. Right-click on ConcreteClass and select Stereotypes > Stereotypes... from the popup menu.
- In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to ConcreteClass class. Click OK to confirm.
- There may be multiple primitive operations. To represent this, stereotype the class AbstractClass as PTN Members Creatable. Repeat steps 11 and 12 to stereotype AbstractClass as PTN Members Creatable.
- 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 Template. Keep the file name as is. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we are going to apply the template pattern in modeling a 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 Template from the list of patterns.
- At the bottom pane, rename AbstractClass and ConcreteClass to Shape and OvalShape.
- We need to have one more concrete class for text shape, click on the + button next to ConcreteClass and select Clone... from the popup menu.
- Enter 1 to be the number of classes to clone. Click OK to confirm.
- Rename ConcreteClass2 to TextShape.
- Rename TemplateMethod and PrimitiveOperation1 to Render and IsTransparent respectively.
- Click OK to apply the pattern to diagram.
- Tidy up the diagram. Here is the result: