This post gives a brief overview about the Template Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.

The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Template Pattern?

According to Wikipedia, the Template Pattern has two main parts, and typically uses object-oriented programming:

  • The “template method”, generally implemented as a base class (possibly an abstract class), which contains shared code and parts of the overall algorithm which are invariant. The template ensures that the overarching algorithm is always followed. In this class, “variant” portions are given a default implementation, or none at all.
  • Concrete implementations of the abstract class, which fill in the empty or “variant” parts of the “template” with specific algorithms that vary from implementation to implementation.

At run-time, a concrete class is instantiated. A main method inherited from the base class is called, which then may call other methods defined by both the base class and subclasses. This performs the overall algorithm in the same steps every time, but the details of some steps depend on which subclass was instantiated.

UML Diagram

The following diagram shows the Template Method Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:</p><p>@startuml<br /> class AbstractClass {<br /> templateMethod()<br /> {abstract} primitiveOperation1()<br /> {abstract} primitiveOperation2()<br /> }<br /> class ConcreteClass</p><p>ConcreteClass : primitiveOperation1()<br /> ConcreteClass : primitiveOperation2()</p><p>AbstractClass <|.. ConcreteClass</p><p>note left of AbstractClass<br /> The AbstractClass<br /> contains the template<br /> method.<br /> end note</p><p>note left of ConcreteClass<br /> There may be many<br /> ConcreteClasses, each<br /> implementing the full set of<br /> operations required by the<br /> template method.<br /> end note</p><p>note right of ConcreteClass<br /> The ConcreteClass implements<br /> the abstract operations,<br /> which are called when the<br /> templateMethod() needs them.<br /> end note</p><p>note right of AbstractClass::templateMethod<br /> The template method makes use of the<br /> primitiveOperations to implement an<br /> algorithm. It is decoupled from the actual<br /> implementation of the operations<br /> “”primitiveOperation1()””<br /> “”primitiveOperation2()””<br /> end note<br /> @enduml</p><p>

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:

class AbstractClass {
{abstract} primitiveOperation1()
{abstract} primitiveOperation2()
class ConcreteClass

ConcreteClass : primitiveOperation1()
ConcreteClass : primitiveOperation2()

AbstractClass &amp;amp;amp;lt;|.. ConcreteClass

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]