3 - Template Method
Tags: flutter, dart, oop, programming
Published: 2021-02-01 09:48:01

Previously in the series, I have analysed one of the most common and most useful design patterns available — Adapter. In this article, I would like to analyse and implement one of the behavioural design patterns — Template Method.

## Table of Contents

* What is the Template Method design pattern?
* Analysis
* Implementation
* Your Contribution

## What is the Template Method design pattern?

The **Template Method** is a **behavioural** design pattern, which intention in the [GoF book](https://en.wikipedia.org/wiki/Design_Patterns) is described like this:

> Define the skeleton of an algorithm in an operation, deferring some steps to sub­classes. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

The Template method is a fundamental technique for code reuse. Let’s say you have an algorithm which consists of multiple steps:

1. Reading the data from some kind of data source;
2. Processing it;
3. Providing the calculated results.

In one case, you want to get the data from a third-party API, process it and provide the results in the console window. In another case, you would like to read the data from a file in your local disk drive, process it and send the formatted result document via e-mail. Despite the differences between these two cases, these algorithms share the same structure — reading, processing the data and providing the results. That’s where the Template Method design pattern is useful — when you need to allow changing the details of each step or certain steps of the algorithm while enforcing the structure and order of the steps themselves. The question is, how to define this kind of template?

## Analysis

The class diagram below shows the general structure of the Template Method design pattern.

![Template Method Class Diagram](https://dev-to-uploads.s3.amazonaws.com/i/299uc58br1nixcw55acb.png)

* *AbstractClass* — contains a *templateMethod()* operation defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in *AbstractClass* or those of other objects.
* *ConcreteClass* — relies on *AbstractClass* to implement the invariant steps of the algorithm.

### Template Method operation types

There are different kinds of operations which are called by the template method:

* *Primitive operations* — abstract operations that **must** be implemented by subclasses; concrete operations that provide a default implementation and **can** be redefined by subclasses if necessary.
* *Final operations* — concrete operations that **can not** be overridden by subclasses.
* *Hook operations* — concrete operations which provide default behaviour that subclasses **can** extend if necessary. A hook operation often does nothing by default.
* *Template method* itself **can be** declared as final so that it could not be overridden by subclasses.

*Note: Template methods need to specify which operations are hooks and which are the abstract operations. To do that, you can identify the operations that should be overridden by adding a prefix to their names e.g. the MacApp framework prefixes template method names with “Do-”.*

### The Hollywood Principle

![The Hollywood Principle](https://dev-to-uploads.s3.amazonaws.com/i/2hxvrmpoy5rgu5gc5o76.png)

The Hollywood Principle is simply stated as:

> Don’t call us, we’ll call you!

How this statement is related to the Template Method? The base class of the Template Method is considered a high-level component — clients or concrete implementations of the algorithm should depend on it. The subclasses of the template are the low-level components — they don’t call anything themselves and are only called by the high-level template method. This describes the relationship between high-level and low-level components — abstractions should not depend on details, but the details should depend on abstractions (Dependency Inversion principle, the letter **D** in [SOLID](https://en.wikipedia.org/wiki/SOLID) principles). Thus, the Template Method design pattern could state that *low-level components don’t call us, we call them*!

### Applicability

The Template Method design pattern should be used when we want to implement the invariant parts of the algorithm once and let the derived classes implement the steps which may vary. Also, the pattern is useful when we want to extract the common logic among classes to avoid the code duplication (DRY principle — **D**on’t **R**epeat **Y**ourself).

## Implementation

![Let's Do This Thing](https://dev-to-uploads.s3.amazonaws.com/i/1part6g1rvbw0uj9fhz7.gif)

Let’s say, in the Flutter application we want to create an algorithm which calculates the [BMI](https://en.wikipedia.org/wiki/Body_mass_index) (body mass index) of students. The algorithm retrieves students’ data, applies students filtering (if necessary), calculates their BMI and provides the results. Also, we want to retrieve students information from different data sources and we want to have a separate algorithm which includes only teenage students for the calculations. As you can see, the structure of the algorithm is general, but the implementation details (algorithm steps) vary among different implementations. To implement this algorithm, we would use the Template method design pattern.

### Class diagram

The class diagram below shows the implementation of the Template Method design pattern.

![Template Method Implementation Class Diagram](https://dev-to-uploads.s3.amazonaws.com/i/uburpqnvsxvfz1g85ebe.png)

The main class in the diagram is *StudentsBmiCalculator*. Its primary purpose is to define a template of the BMI calculation algorithm which returns a list of *Student* objects (with the calculated BMI for each student) as a result via *calculateBmiAndReturnStudentList()* method. This abstract class is used as a template (base class) for the concrete implementations of the students’ BMI calculation algorithm — *StudentsXmlBmiCalculator*, *StudentsJsonBmiCalculator* and *TeenageStudentsJsonBmiCalculator*. *StudentsXmlBmiCalculator* uses the *XmlStudentsApi* to retrieve students information as an XML string and returns it as a list of Student objects via the overridden *getStudentsData()* method. Both of the other two implementations (*StudentsJsonBmiCalculator* and *TeenageStudentsJsonBmiCalculator*) uses the *JsonStudentsApi* to retrieve students information in JSON format and returns the parsed data via the overridden *getStudentsData()* method. However, *TeenageStudentsJsonBmiCalculator* additionally reimplements (overrides) the *doStudentsFiltering()* hook method to filter out not teenage students before calculating the BMI values. *StudentsSection* UI widget uses the *StudentsBmiCalculator* abstraction to retrieve and represent the calculated results in *TemplateMethodExample* widget.

*Note: it would make more sense to extract the XML or JSON parsing logic from the getStudentsData() method in the concrete calculation classes to a separate class using the Adapter design pattern, but for the demonstration purposes of the Template Method pattern and to show that the implementation of getStudentsData() may differ among the derived classes of the StudentsXmlBmiCalculator, I have decided to leave logic as it is now.*

### StudentsBmiCalculator

An abstract (template) class for the BMI calculation algorithm.

![students_bmi_calculator.dart](https://dev-to-uploads.s3.amazonaws.com/i/q78l41fcc6yfwllpvyac.png)

The algorithm consists of several steps:

1. Retrieve students data — *getStudentsData()*;
2. Do students filtering (if needed) — *doStudentsFiltering()*;
3. Calculate the BMI for each student — *_calculateStudentsBmi()*;
4. Return students data — return *studentList*.

The first step is mandatory and should be implemented in each concrete implementation of the students BMI calculator — that is, the method *getStudentsData()* is abstract and must be overridden in the derived class (primitive operation). Students filtering step is optional, yet it could be overridden in the derived class. For this reason, *doStudentsFiltering()* method has a default implementation which does not change the workflow of the algorithm by default (*hook* operation). Other steps are defined in the algorithm’s template itself, are common for all implementations and could not be changed (*final* operations).

### StudentsXmlBmiCalculator

A concrete implementation of the BMI calculation algorithm which uses *XmlStudentsApi* to retrieve data and implements the *getStudentsData()* method.

![students_xml_bmi_calculator.dart](https://dev-to-uploads.s3.amazonaws.com/i/8i7vg17amzchsimsc9br.png)

### StudentsJsonBmiCalculator

A concrete implementation of the BMI calculation algorithm which uses *JsonStudentsApi* to retrieve data and implements the *getStudentsData()* method.

![students_json_bmi_calculator.dart](https://dev-to-uploads.s3.amazonaws.com/i/qohv8puri4t9f26cmtoo.png)

### TeenageStudentsJsonBmiCalculator

A concrete implementation of the BMI calculation algorithm which uses *JsonStudentsApi* to retrieve data and implements the *getStudentsData()* method.

![teenage_students_json_bmi_calculator.dart](https://dev-to-uploads.s3.amazonaws.com/i/jons4au2jhy0geehhhzt.png)

Additionally, the *doStudentsFiltering()* hook method is overridden to filter out not teenage students.

### Student

A simple class to store the student’s information.

![student.dart](https://dev-to-uploads.s3.amazonaws.com/i/34is80m354hi0shm8qgt.png)

### JsonStudentsApi

A fake API which returns students’ information as JSON string.

![json_students_api.dart](https://dev-to-uploads.s3.amazonaws.com/i/cin0qc3jo5a3ec22s3ai.png)

### XmlStudentsApi

A fake API which returns students’ information as an XML string.

![xml_students_api.dart](https://dev-to-uploads.s3.amazonaws.com/i/jm6l3rcbxh3tq1z8a2ks.png)

### Example

First of all, a markdown file is prepared and provided as a pattern’s description:

![Template Method Markdown](https://dev-to-uploads.s3.amazonaws.com/i/w29bziq5f4p85usai6xv.gif)

The example itself uses *StudentsSection* component which requires a specific BMI calculator of type *StudentsBmiCalculator* to be provided via a constructor. For this example, we inject three different implementations of BMI calculator (*StudentsXmlBmiCalculator*, *StudentsJsonBmiCalculator* and *TeenageStudentsJsonBmiCalculator*) which extend the same template (base class) — *StudentsBmiCalculator* — to three different *StudentsSection* widgets.

![template_method_example.dart](https://dev-to-uploads.s3.amazonaws.com/i/yugwolacqo30xwit7osm.png)

*StudentsSection* uses the injected BMI calculator of type *StudentsBmiCalculator*. The widget does not care about the specific implementation of the BMI calculator as long as it uses (extends) the same template (base class). This lets us provide different students’ BMI calculation algorithms/implementations without making any changes to the UI code.

![students_section.dart](https://dev-to-uploads.s3.amazonaws.com/i/elbnlv92iv1dskd8i9wf.png)

The final result of the Template Method’s implementation looks like this:

![Template Method Example](https://dev-to-uploads.s3.amazonaws.com/i/k4kb5umr3vrqa25jwyh2.gif)

All of the code changes for the Template Method design pattern and its example implementation could be found [here](https://github.com/mkobuolys/flutter-design-patterns/pull/4).

## Your Contribution

💖 or 🦄 this article to show your support and motivate me to write better!
💬 Leave a response to this article by providing your insights, comments or wishes for the next topic.
📢 Share this article with your friends, colleagues in social media.
➕ Follow me on dev.to or any other social media platform.
⭐ Star the [Github](https://github.com/mkobuolys/flutter-design-patterns) repository.