Definition of the template method pattern

A framework that defines an algorithm in an operation, deferring some steps to subclasses. Allows subclasses to redefine specific steps of an algorithm without changing its structure.

In layman’s terms, you put the same methods of a subclass into its abstract superclass

The class diagram is as follows:

 

AbstractClass is called abstract templates, and its methods fall into two categories:

  1. Basic methods, also called basic operations, are methods implemented by subclasses and called in template methods
  2. Template method, there can be one or several, is generally a specific method, that is, a framework, to achieve the basic method scheduling, complete fixed logic, general template method with the final keyword, not allowed to be overwritten, to prevent malicious operations

ConcreteClass1 and ConcreteClass2 are concrete templates that implement one or more abstract methods defined by the superclass. The basic methods defined by the superclass are implemented in subclasses.

Abstract template class implementation code is as follows:

 

Concrete classes implement basic methods.

Application of the template method pattern

Advantages of the template method pattern

  1. Encapsulate immutable parts and extend mutable parts. An algorithm that is considered an immutable part is encapsulated into a superclass implementation, while mutable parts can be extended through inheritance
  2. Extract common parts of code for easy maintenance. Imagine if we didn’t extract the parent class, and the maintainer had to look around for similar code to fix a defect
  3. Behavior has parent class control, child class implementation. The base method is implemented by subclasses, so subclasses can be extended to add functionality in accordance with the open closed principle

Disadvantages of the template method pattern

According to our design conventions, abstract classes are responsible for declaring the most abstract and general food properties and methods, while implementation classes accomplish the concrete food properties and methods. However, the template method pattern is reversed. Abstract classes define partial abstract methods, which are implemented by subclasses, and the results of the execution of the subclass affect the results of the parent class, that is, the subclass affects the parent class. This can make code reading difficult in complex projects, and also make beginners uncomfortable

Usage scenarios for the template method pattern

  1. Multiple subclasses have public methods, and the logic is basically the same
  2. For important and complex algorithms, the core algorithm can be designed as a template method, and the surrounding details are implemented by each subclass
  3. In refactoring, the template method pattern is a frequently used pattern that extracts the same code into a parent class and then constrains its behavior with hook functions

An extension of the template method pattern

The AbstractClass abstraction class above is modified as follows:

 

Subclasses implement the isRun() method. Different implementations of subclasses can affect the execution of common code

That is to say, external conditions change, affecting the suffocating of the template method. The return value of isRun in our abstract class affects the result of the template method, which is called the hook method.

The template method pattern calls base methods within template methods in a certain order and rule.


Using the template method pattern is a very easy pattern to extend