Usage analysis of Java design pattern template method pattern

This article mainly introduces the usage analysis of Java design pattern template method pattern (template). It is introduced in great detail through example code, which has certain reference value for everyone's study or work. Friends in need can refer to it

preface:

There are many fixed processes in our development. Many of these processes have fixed steps. For example, the processes of obtaining connections and closing connections in JDBC are fixed and unchanged. Only setting parameters and parsing result sets change. These are "adjusted" according to different entity objects. For this process with fixed algorithm, there are fixed steps, The template method pattern is born when there are non fixed steps.

Template method pattern (template) definition:

Template method mode, also known as template mode, refers to that the parent class defines a multi-step algorithm skeleton, many of which are implemented in the parent class. Some have different implementations according to different subclasses. These "uncertain" implementation steps are defined as abstract methods and handed over to subclasses for implementation. The core of template pattern is to redefine some steps of the algorithm without changing the structure of the algorithm. It belongs to behavioral design pattern.

Widely used, such as baseexecutor in mybatis; Jdbctemplate in spring; Spring integrates hibernate template in Hibernate, spring integrates various MQ templates, mongodbtemplate and so on

Roles in schema:

Abstract class: it implements the template method and defines the skeleton of the algorithm.

Concrete: it implements the abstract placement in the abstract class, making the algorithm defined by the parent class more complete.

Code implementation:

1: define the process with abstract classes. For example, we have a process at work every day: power on -- > write code (different programmers may write different codes -- > power off; in the whole process, power on and power off are the same, but the step of writing code is different. Java programmers write java code and PHP programmers write PHP code; we first define the process with an abstract parent class:

public abstract class Work {

  //定义算法步凑流程
  public void workDay(){

    //1:上班开机
    openComputer();
    //2:搬砖:写代码
    coding();
    //3:下班关机     closeComputer();
  }

  //开机
  private final void openComputer() {
    System.out.println("到达公司,开机");
  }

  //写代码
  protected abstract void coding();

  //关机
  private final void closeComputer() {
    System.out.println("下班,关机");
  }

}

2: Concrete implementation

Java programmer

public class JavaProgrammer extends Work {

  @Override
  protected void coding() {
    System.out.println("Java程序员打开Idea,写Java代码。。。");
  }
}

PHP Programmers

public class PHPProgrammer extends Work {

  @Override
  protected void coding() {
    System.out.println("PHP程序员打开Zend Studio,写PHP代码。。。");
  }
}

3: Use

public class TemplateMethodDemo {

  public static void main(String[] args) {
    Work javaProgrammer = new JavaProgrammer();
    javaProgrammer.workDay();
    System.out.println("================");
    Work PHPProgrammer = new PHPProgrammer();
    PHPProgrammer.workDay();
  }
}

Operation results

Embodiment of template mode in source code:

1: We use the service method in httpservlet every day. The service method defines the calling process. According to the different methods called by the client, if else logic judges to call different methods, such as doget, dopost, dodelete, etc. to realize restful calls. For the specific implementation of doget and dopost methods, we can rewrite them in our own defined servlet.

2: There are also some classic and practical applications in the mybatis framework, such as the baseexecutor class, which is a basic SQL execution class that implements most of the SQL execution logic, and then teaches several methods to subclasses for customization. The source code is as follows:

Methods such as doupdate(), doflushstatements(), doquery(), doquerycursor() are implemented by subclasses. What subclasses does baseexecutor have? Let's take a look at his class diagram:

We can look at the different implementations of each subclass.

Advantages and disadvantages of template mode:

Advantages:

1: using the template pattern, you can put the code with the same processing logic into the abstract parent class, which improves the reusability of the code.

2: put different logic into different subclasses, add new behavior through subclass expansion, and improve the scalability of the code.

3: write the unchanged behavior in the parent class, remove the repeated code of the child class, and provide a good code reuse platform, which conforms to the opening and closing principle.

4: the company's senior engineers or architects can use the template pattern to define the process in the early stage of the project and let other coding engineers implement the specific process.

Disadvantages:

1: each abstract class needs at least one subclass to implement, resulting in an increase in the number of classes.

2: the increase in the number of classes indirectly increases the complexity of the system.

3: because of the shortcomings of inheritance relationship, if a new abstract method is added to the parent class, all subclasses must be implemented.

Code link: https://gitee.com/ganganbobo/gps-parent

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>