Template Method Patterns for Design Patterns

Keywords: Java Design Pattern


Template Method A schema is defined as follows: Defines the algorithm skeleton in an operation and delays some steps of the algorithm to a subclass so that the subclass can redefine some specific steps of the algorithm without changing the structure of the algorithm. That is, an abstract class openly defines the way/template to execute its method. Its subclasses can override the method implementation as needed, but the call will be abstractThe way defined in the class. This type of design pattern is behavioral.

Intention: Defines the skeleton of an algorithm in an operation and delays some steps to a subclass. Template methods allow subclasses to redefine certain steps of the algorithm without changing the structure of an algorithm.

The main solution: Some methods are generic, but this method has been rewritten in each subclass.

When to use: There are some common methods.

How to solve: Abstract these general algorithms.

Key Code: In an abstract class implementation, other steps are implemented in a subclass.

Examples of applications: 1. When building a house, the foundations, lines and water pipes are all the same. Only in the later stage of the building can there be differences such as closets and fences.

1. It encapsulates the invariant part and extends the variable part. It encapsulates the algorithm that is considered the invariant part into the parent class, and implements the variable part algorithm by inheriting from the subclass so that the subclass can continue to expand.
2. It extracts some common code from the parent class to facilitate code reuse.
3. Some methods are implemented by subclasses, so subclasses can be extended to add corresponding functions, which conform to the open and close principle.

1. Each different implementation requires a subclass to be defined, which will result in an increase in the number of classes, a larger system, and a more abstract design, indirectly increasing the complexity of system implementation.
2. Abstract methods in a parent class are implemented by subclasses, and the results of subclass execution affect the results of the parent class, which leads to a reverse control structure that makes code reading more difficult.
3. Due to the shortcomings of inheritance relationship itself, if the parent class adds a new abstract method, all subclasses will be changed again.

Use scenarios: 1. Methods shared by multiple subclasses with the same logic. 2. Important, complex methods can be considered as template methods.

Note: To prevent malicious operations, the general template methods are added with final keywords.

Template Method Pattern Structure

1) Abstract Class/Template (Abstract Class)

An abstract template class responsible for outlining and skeleton an algorithm. It consists of a template method and several basic methods. These methods are defined as follows.
(1) Template method: Defines the framework of the algorithm and calls the basic methods it contains in some order.
(2) Basic method: It is a step in the whole algorithm and contains the following types.

Abstract method: declared in an abstract class and implemented by a specific subclass.
Specific method: Implemented in an abstract class, inherited or overridden in a specific subclass.
Hook method: Implemented in abstract classes, including logical methods for judgment and empty methods that require subclass overrides.
2) Specific subclasses/implementations (Concrete Class)

Specific implementation classes that implement the abstract methods and hook methods defined in the abstract class are a component step of top-level logic.
Take the game as an example here. Every time you enter a different game, there are common initialization games, starting and ending the game.

public abstract class Game {
   abstract void initialize();
   abstract void startPlay();
   abstract void endPlay();
   public final void play(){
      //Initialize the game
      //Start the game
      //End Game

Create entity classes for these classes:

public class Cricket extends Game {
   void endPlay() {
      System.out.println("Cricket Game Finished!");
   void initialize() {
      System.out.println("Cricket Game Initialized! Start playing.");
   void startPlay() {
      System.out.println("Cricket Game Started. Enjoy the game!");
public class Football extends Game {
   void endPlay() {
      System.out.println("Football Game Finished!");
   void initialize() {
      System.out.println("Football Game Initialized! Start playing.");
   void startPlay() {
      System.out.println("Football Game Started. Enjoy the game!");
public class TemplatePatternDemo {
   public static void main(String[] args) {
      Game game = new Cricket();
      game = new Football();

The output is as follows:

Cricket Game Initialized! Start playing.
Cricket Game Started. Enjoy the game!
Cricket Game Finished!

Football Game Initialized! Start playing.
Football Game Started. Enjoy the game!
Football Game Finished!

Posted by IceD on Wed, 29 Sep 2021 09:36:54 -0700