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.

October 27, 2009
Views: 59,016
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Template.
    New diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it AbstractClass.
    Create AbstractClass
  4. Right-click on AbstractClass and select Model Element Properties > Abstract to set it as abstract.
    Set class as abstract
  5. Right-click on the AbstractClass class and select Add > Operation from the popup menu.
    Add operation to AbstractClass
  6. Name the operation `TemplateMethod()`.
    TemplateMethod added
  7. Create another operation, `PrimitiveOperation1()`.
    Primitive operation added
  8. Right-click on `PrimitiveOperation1()` and select Model Element Properties > Abstract to set it as abstract.
    Set primitive operation as abstract
  9. Move the mouse cursor over the AbstractClass class and drag out Generalization > Class to create a subclass named ConcreteClass.
    ConcreteClass created
  10. 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.
    Realize all interfaces
  11. 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.
    Stereotype ConcreteClass
  12. 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.
    Set PTN Cloneable stereotype
  13. 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`.
    Stereotype AbstractClass

Defining the 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 menu
  3. In the Define Design Pattern dialog box, specify the pattern name as Template. Keep the file name as is. Click OK to proceed.
    Name pattern

Applying a Design Pattern to a Class Diagram

In this section, we are going to apply the template pattern in modeling 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 menu
  4. In the Design Pattern dialog box, select Template from the list of patterns.
    Select Template pattern
  5. In the bottom pane, rename AbstractClass and ConcreteClass to Shape and OvalShape, respectively.
    Rename classes
  6. We need one more concrete class for a text shape. Click the + button next to ConcreteClass and select Clone... from the popup menu.
    Clone ConcreteClass
  7. Enter `1` as the number of classes to clone and click OK to confirm.
    Enter clone count
  8. Rename ConcreteClass2 to TextShape.
    Rename ConcreteClass2
  9. Rename TemplateMethod and PrimitiveOperation1 to Render and IsTransparent, respectively.
    Rename operations
  10. Click OK to apply the pattern to the diagram.
  11. Tidy up the diagram. Here is the result:
    Resulting diagram