This is the 18th day of my participation in the First Challenge 2022

A template model

Basic concepts of template patterns

Template Method Pattern mainly refers to defining the algorithm framework in an operation and deferring some steps of the algorithm to a subclass so that the subclass can redefine certain steps of the algorithm without changing the structure of the algorithm. The template method pattern is a behavioral pattern during class creation.

The template pattern essentially encapsulates a fixed process that contains fixed steps that can be implemented by different subclasses to produce different results for the fixed process. The template pattern is primarily a class inheritance mechanism, which is essentially an abstract encapsulation process.

Application scenarios of the template pattern

  • When the overall steps of the algorithm are very fixed, but some parts of the algorithm are mutable, the template method pattern can be used to abstract the mutable parts out for subclass implementation.

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

  • When you need to control the extension of a subclass, the template method calls hook operations only at certain points, allowing extension only at those points.

  • The entire Spring module uses template methods such as beanfactory.getbean (1,2,3,4)

  • The JdbcTemplate template allows us to inherit in extensions

  • The RedisTemplate template allows us to inherit in extensions

Generic way to write a template pattern

Typically, the template method pattern contains two roles

  • Abstract Class: An Abstract template Class that defines the flow or framework of an algorithm

  • Concrete Class: A Concrete implementation Class that implements a specific step in an algorithmic process or framework.

The following is an example of a cook cooking to better demonstrate the common writing of the template pattern

  • CookTemplate abstract Template class
public  abstract class CookTemplate {

    public void  cook(a){
        heating();
        addFood();
        addSalt();
        stirFry();
        end();
    }
    private void  heating(a){
        System.out.println("Fire...");
    }

    public abstract void addFood(a);

    public abstract void addSalt(a);

    public void stirFry(a){
        System.out.println("Stir...");
    }

    public void end(a){
        System.out.println("Out of the pan"); }}Copy the code
  • AutoCookMachine (concretely implementing classes in abstract templates)
public class AutoCookMachine  extends CookTemplate{
    @Override
    public void addFood(a) {
        System.out.println("Cabbage");
    }

    @Override
    public void addSalt(a) {
        System.out.println("Salt"); }}Copy the code
  • UML diagrams

It can be seen from the above code and UML structure diagram that a CookTemplate abstract class is defined, and then the Cook method is defined in the abstract class, which contains all steps of cooking, such as heating firing method, adding dishes, adding pigments and other methods, and the concrete implementation class of AutoCookMachine is defined. It inherits the CookTemplate template and overwrites the addFood and addSalt methods so that different methods in the template can be implemented through different implementation classes.

Advantages and disadvantages of the template pattern

advantages

  • The template pattern encapsulates the immutable and extends the mutable. It encapsulates the algorithm which is considered to be the invariant part into the parent class, and inherits the algorithm of the variable part by the subclass, which is convenient for the subclass to continue to expand.

  • The template pattern extracts common parts of the code from the parent class for easy code reuse.

  • Some methods are implemented by subclasses, so subclasses can be extended to add functionality in accordance with the open closed principle.

disadvantages

  • The need to define a subclass for each different implementation leads to an increase in the number of classes, a larger system, and a more abstract design, indirectly increasing the complexity of the system implementation.

  • Abstract methods in the parent class are implemented by the subclass, and the results of the execution of the subclass affect the results of the parent class, leading to a reverse control structure that makes code more difficult to read.

  • Due to the disadvantages of inheritance, if the parent class adds a new abstract method, all subclasses need to change it.