Home » Blog » Software Development » Software Design Patterns in PlantUML » Design Patterns with PlantUML: Template Pattern

Design Patterns with PlantUML: Template Pattern

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>@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>

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:


@startuml
class AbstractClass {
templateMethod()
{abstract} primitiveOperation1()
{abstract} primitiveOperation2()
}
class ConcreteClass

ConcreteClass : primitiveOperation1()
ConcreteClass : primitiveOperation2()

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

Other Design Patterns

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

Template Pattern Design Patterns with PlantUML: Template Pattern - This post gives a brief overview about the Template Pattern. The post is part of a series about software design ... Read More
Facade Pattern Design Patterns with PlantUML: Facade Pattern - This post gives a brief overview about the Facade Pattern. The post is part of a series about software design ... Read More
Adapter Pattern Design Patterns with PlantUML: Adapter Pattern - This post gives a brief overview about the Adapter Pattern. The post is part of a series about software design ... Read More
Command Pattern Design Patterns with PlantUML: Command Pattern - This post gives a brief overview about the Command Pattern. The post is part of a series about software design ... Read More
Singleton Design Patterns with PlantUML: Singleton - This post gives a brief overview about the Singleton Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Abstract Factory Pattern - This post gives a brief overview about the Abstract Factory Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Factory Method Pattern - This post gives a brief overview about the Factory Method Pattern. The post is part of a series about software ... Read More
Decorator Pattern Design Patterns with PlantUML: Decorator Pattern - This post gives a brief overview about the Decorator Pattern. The post is part of a series about software design ... Read More
Observer Pattern Design Patterns with PlantUML: Observer Pattern - This post gives a brief overview about the Observer Pattern. The post is part of a series about software design ... Read More

Check Also

Facade Pattern

Design Patterns with PlantUML: Facade Pattern

This post gives a brief overview about the Facade Pattern. The post is part of …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

We use Cookies and similar technology to collect and analyse information about the users of this website. We use this information to enhance the content, advertising and other services available on the site. Please click ‘Accept cookies’ to consent to the use of this technology by petrockblock. You can manage your preferences at any time by visiting our Cookies Policy page.