Template mode-behavioral

Creation type

1. Singleton design pattern
2. Factory design pattern
3. Builder design pattern
4. Prototype design pattern

Structure type

5. Agency design pattern
6, bridging design pattern
7, decoration design pattern
8, adapter design pattern
9, appearance design pattern
10, flyweight design pattern
11, combination design pattern

Behavioral

12. Template design mode

 

Introduction

The template method pattern defines an algorithm framework in a method and defers certain steps to subclass implementation. The template method pattern allows subclasses to redefine certain steps in the algorithm without changing the overall structure of the algorithm. The "algorithm" here can be understood as "business logic" in a broad sense, and does not specifically refer to the "algorithm" in data structures and algorithms. The algorithm framework here is the "template", and the method containing the algorithm framework is the "template method", which is the origin of the template method pattern.

How to learn

The principles and implementation of most design patterns are very simple, but the difficulty is to master the application scenarios and figure out what problems can be solved.

solved problem

The template mode is mainly used to solve the two problems of reuse and expansion.

scenes to be used

1. Multiple subclasses have common methods, and the logic is basically the same or similar;

2. For important and complex algorithms, the core algorithm can be designed as a template method, and the peripheral related and detailed functions are implemented by each subclass;

3. When refactoring, the template method pattern is a commonly used pattern. The same code is extracted into the parent class, and then its behavior is restricted through the hook function;

UML class diagram

AbstractTemplate: Abstract class, defines a set of algorithm framework

ConcreteClass1: specific implementation class 1

ConcreteClass2: specific implementation class 2

Simple example

Can also be seen as template code

public abstract class AbstractClass {
    public final void templateMethod() {
        //...
        method1();
        //...
        method2();
        //...
    }

    protected abstract void method1();
    protected abstract void method2();
}
public class ConcreteClass1 extends AbstractClass {
    @Override
    protected void method1() {
        //...
    }

    @Override
    protected void method2() {
        //...
    }
}
public class ConcreteClass2 extends AbstractClass {
    @Override
    protected void method1() {
        //...
    }

    @Override
    protected void method2() {
        //...
    }
}

use 

class Demo{
    public static void main(String[] args) {
        AbstractClass demo = ConcreteClass1();
        demo.templateMethod();
    }
}

to sum up

In the classic implementation of the template pattern, the template method is defined as final, which can avoid being overwritten by subclasses. The method that needs to be rewritten by the subclass is defined as abstract, and the subclass can be forced to implement it. However, in actual project development, the realization of the template mode is more flexible, and the above two points are not necessary.

The template mode has two major functions: reuse and extension. Among them, reuse means that all subclasses can reuse the code of the template method provided in the parent class. Extension means that the framework provides function extension points through the template mode, allowing users of the framework to customize the functions of the framework based on the extension points without modifying the framework source code.

Guess you like

Origin blog.csdn.net/ezconn/article/details/108812616