Template method pattern of design pattern 4

background

In development, you must have encountered such a scenario: you know the key steps and execution order of a method, but the specific steps are not clear. What would you do?

For example, if you get up in the morning and go to the company, we break down the steps:

  1. Get up and wash

  2. Eat breakfast

  3. Taking public transport

  4. To the company

We have determined this step, but the details in different people may be different. For example, Xiao Ming eats bread for breakfast, Xiao Zhang eats noodles. Xiaomei takes the subway and Xiaohua takes the bus.

For another example, we usually use resume templates to write resumes. Different people write different content and have the same templates.

The template method pattern is defined as follows:

Define an algorithm skeleton in operation, and delay some steps of the algorithm to the subclass, so that the subclass can redefine some specific steps of the algorithm without changing the structure of the algorithm. It is a kind of behavioral pattern.

This definition can be seen in the cloud, let's code directly to demonstrate.

Before writing code, we need to understand that there are several elements needed to implement the template method pattern:

  • Abstract class: To define the basic skeleton of an algorithm, a template method and multiple basic methods are required

  • Concrete class: implement the abstract methods defined in the abstract class

So what is a template method? The template method defines the algorithm skeleton and contains the execution order of the algorithm.

The basic method is a step in the algorithm that needs to be implemented or rewritten. Used for expansion. The structure is as follows:

Code implementation of template method

First create an abstract class:

public abstract class AbstractClass {
    //模板方法
    public void templateMethod() {
        specificMethod();
        abstractMethod1();
        abstractMethod2();
    }
    //具体方法
    public void specificMethod() {
        System.out.println("抽象类中的具体方法被调用...");
    }
    public abstract void abstractMethod1(); //抽象方法1
    public abstract void abstractMethod2(); //抽象方法2
}

This defines template methods, concrete methods, and abstract methods. The template method contains concrete methods and abstract methods.

Specific subcategories:

public class ConcreteClass extends AbstractClass {
    @Override
    public void abstractMethod1() {
        System.out.println("抽象方法1的实现被调用...");
    }

    @Override
    public void abstractMethod2() {
        System.out.println("抽象方法2的实现被调用...");
    }
}

This subclass inherits the abstract class, which implements abstract methods.

The test code is as follows:

public class TemplateMethodPattern {
    @Test
    public void test() {
        AbstractClass tm = new ConcreteClass();
        tm.templateMethod();
    }
}

We call the template method and get the following information:

抽象类中的具体方法被调用...
抽象方法1的实现被调用...
抽象方法2的实现被调用...

Thinking about the template method mode

From the above code, we find that templateMethod()the overall steps of the template method are fixed, and the content in the concrete method and abstract method is changed. We pass the abstract method and the concrete method, we can modify the content through inheritance.

When multiple subclasses have common behaviors, they can be extracted and concentrated into a common parent class to avoid code duplication. First, identify the differences in the existing code and separate the differences into new operations. Finally, replace these different codes with a template method that calls these new operations.

I think the template method has the following advantages:

  • The unchanging part of the algorithm is encapsulated into the parent class to achieve, and the variable part is extended through inheritance.

  • Extracting similar methods as public is also easy to maintain.

  • The parent class controls the behavior, and the child class implements extension.

So when you are designing important and complex algorithms, you can design the core algorithm as a template method, and the surrounding detailed functions are implemented by each subclass.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/108989994