This tutorial aims to guide you in understanding and applying the Gang of Four (GoF) template design pattern. Throughout this tutorial, you will learn how to create a UML class diagram for the template pattern and save it as a design pattern file for future reuse.

What is Template Design Pattern?

The Template Design Pattern is a behavioral design pattern that defines the basic steps or structure of an algorithm and allows subclasses to override some of those steps without changing the algorithm’s overall structure. In other words, it provides a way to create a blueprint or a template for a set of algorithms, which can be modified based on specific requirements.

The pattern is based on the idea of separating the algorithm’s overall structure from its individual steps or implementations. By doing so, it enables the developers to reuse the common parts of the algorithm, while still being able to modify the specific steps according to their needs.

In essence, the Template Design Pattern is used when we want to provide a skeleton or a framework for an algorithm, but allow its individual steps to be defined by the subclasses. This way, we can ensure that the overall algorithm’s structure remains unchanged, while still being able to customize its behavior as per specific requirements.

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Template.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as AbstractClass.
    create abstract class
  4. Right-click on AbstractClass, and select Model Element Properties > Abstract to set it as abstract.
    set class abstract
  5. Right-click on the AbstractClass class, and select Add > Operation from the popup menu.
    add oper to abstract class
  6. Name the operation TemplateMethod().
    template method added
  7. Create another operation PrimitiveOperation1().
    primitive oper added
  8. Right-click on PrimitiveOperation1, and select Model Element Properties > Abstract to set it as abstract.
    set primitive oper abstract
  9. Move the mouse cursor over the AbstractClass class, and drag out Generalization > Class to create subclasses ConcreteClass.
    concrete class created
  10. 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.
    realize abstract class
  11. 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.
    stereotype concrete class
  12. 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.
    set ptn cloneable
  13. 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.
    stereotype abstract class

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

Applying Design Pattern on Class Diagram

In this section, we are going to apply the template pattern in modeling a diagram editor.

  1. Create a new project Diagram Editor.
  2. Create a class diagram 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 Template from the list of patterns.
    select template pattern
  5. At the bottom pane, rename AbstractClass and ConcreteClass to Shape and OvalShape.
    rename classes
  6. 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.
    clone concrete class
  7. Enter 1 to be the number of classes to clone. Click OK to confirm.
    enter clone count
  8. Rename ConcreteClass2 to TextShape.
    rename concrete class2
  9. Rename TemplateMethod and PrimitiveOperation1 to Render and IsTransparent respectively.
    rename opers
  10. Click OK to apply the pattern to diagram.
  11. Tidy up the diagram. Here is the result:
    result

Leave a Comment

Your email address will not be published.