Small talk on design patterns (11) - Template method pattern

Column introduction

Column address

link

Column introduction

It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
Insert image description here

template method pattern

This is a behavioral design pattern that is used to define the framework of an algorithm and defer the specific implementation of the algorithm to subclasses.

Role classification

Abstract Class

The abstract class defines a template method, which contains the framework of the algorithm and the calling sequence of a series of basic methods. Abstract classes can also define abstract methods, concrete methods and hook methods to delay concrete implementation or provide default implementation.

Concrete Class

Concrete subclasses inherit abstract classes and implement abstract methods and hook methods. Concrete subclasses are responsible for implementing the specific steps of the algorithm.

Abstract Method

Abstract methods are methods declared in an abstract class and implemented by concrete subclasses. Abstract methods are basic methods in template methods and are used to complete part of the algorithm.
Insert image description here

Concrete Method

Concrete methods are methods that have been implemented in abstract classes and can be called directly in template methods. Concrete methods are basic methods in template methods and are used to complete part of the algorithm.

Hook Method

Hook methods are methods with default implementation in abstract classes, and subclasses can choose whether to override them. Hook methods can be used to provide different behaviors at different stages of the algorithm.
Insert image description here

main idea

Fix the framework of the algorithm in an abstract class and defer the specific implementation to a concrete subclass. The abstract class defines a template method, which contains the framework of the algorithm and the calling sequence of a series of basic methods. Abstract classes can also define abstract methods, concrete methods and hook methods to delay concrete implementation or provide default implementation.

Java code implementation

// 抽象模板类
abstract class AbstractClass {
    
    
    // 模板方法,定义了算法的骨架
    public final void templateMethod() {
    
    
        step1();
        step2();
        step3();
    }

    // 基本方法1
    protected abstract void step1();

    // 基本方法2
    protected abstract void step2();

    // 基本方法3
    protected abstract void step3();
}

// 具体模板类A
class ConcreteClassA extends AbstractClass {
    
    
    @Override
    protected void step1() {
    
    
        System.out.println("ConcreteClassA: Step 1");
    }

    @Override
    protected void step2() {
    
    
        System.out.println("ConcreteClassA: Step 2");
    }

    @Override
    protected void step3() {
    
    
        System.out.println("ConcreteClassA: Step 3");
    }
}

// 具体模板类B
class ConcreteClassB extends AbstractClass {
    
    
    @Override
    protected void step1() {
    
    
        System.out.println("ConcreteClassB: Step 1");
    }

    @Override
    protected void step2() {
    
    
        System.out.println("ConcreteClassB: Step 2");
    }

    @Override
    protected void step3() {
    
    
        System.out.println("ConcreteClassB: Step 3");
    }
}

// 测试代码
public class Main {
    
    
    public static void main(String[] args) {
    
    
        AbstractClass classA = new ConcreteClassA();
        classA.templateMethod();

        System.out.println();

        AbstractClass classB = new ConcreteClassB();
        classB.templateMethod();
    }
}

output

ConcreteClassA: Step 1
ConcreteClassA: Step 2
ConcreteClassA: Step 3

ConcreteClassB: Step 1
ConcreteClassB: Step 2
ConcreteClassB: Step 3

analyze

In the above example, the abstract template class AbstractClass defines a template method templateMethod(), which defines the skeleton of an algorithm, which includes multiple basic methods step1(), step2(), and step3(). The concrete template classes ConcreteClassA and ConcreteClassB inherit from AbstractClass and implement basic methods.
Insert image description here

Summarize

The template method pattern is a simple but very practical design pattern. It provides a flexible and extensible algorithm design solution by fixing the framework of the algorithm in an abstract class and deferring the specific implementation to specific subclasses.

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/133467080