Template Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) template design pattern. By reading this tutorial, you will know how to develop a model for the template 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 Template.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it 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 a subclass named ConcreteClass.

- We need to make the concrete class 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 classes. To represent this, stereotype the `ConcreteClass` class 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 the ConcreteClass class. Click OK to confirm.

- There may be multiple primitive operations. To represent this, stereotype the `AbstractClass` class as PTN Members Creatable. Follow the same steps as you did for stereotyping the `ConcreteClass` to apply the PTN Members Creatable stereotype to `AbstractClass`.

Defining the 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 Template. Keep the file name as is. Click OK to proceed.

Applying a Design Pattern to a Class Diagram
In this section, we are going to apply the template pattern in modeling 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 Template from the list of patterns.

- In the bottom pane, rename AbstractClass and ConcreteClass to Shape and OvalShape, respectively.

- We need one more concrete class for a text shape. Click the + button next to ConcreteClass and select Clone... from the popup menu.

- Enter `1` as the number of classes to clone and click OK to confirm.

- Rename ConcreteClass2 to TextShape.

- Rename TemplateMethod and PrimitiveOperation1 to Render and IsTransparent, respectively.

- Click OK to apply the pattern to the diagram.
- Tidy up the diagram. Here is the result:
