Template Patterns in Java Design Patterns

Table of contents

Introduction to Template Patterns

Example of Template Pattern

Advantages and disadvantages of template pattern

Summarize

Introduction to Template Patterns

Defining the skeleton of an algorithm in an operation, while deferring some steps to subclasses, template methods allow subclasses to redefine certain steps of the algorithm without changing the structure of the algorithm.

In layman’s terms: a framework (method) for an operation is defined in the parent class, a step in the framework is an [abstract method] for an operation is defined in the parent class, a step in the framework is an [abstract method] , and the specific steps are handed over to the subclass to implement, and the implementation details of each subclass are inconsistent. . It has been achieved that different subclasses perform a similar operation with inconsistent details.

Example of Template Pattern

[Class diagram] of the first case[]

The parent class defines the method of making fried chicken, and making a fried chicken requires breeding -> slaughtering -> frying. These three steps are defined as a method and wait for the subclass to [rewrite] , because different manufacturers make fried chicken in Every step is different.

The Java code is as follows:

fried chicken parent

/**
 * @author meat
 * @version 1.0
 * @date 2022/3/21 15:23
 * @description parent class of fried chicken
 */
public abstract class AbstractFriedChicken {

    /**
     * Define a set of steps to make fried chicken
     * */
    protected final void makeFriedChicken(){
        breed();
        slaughter();
        Fried();
    }

    // The breeding method waits for subclasses to implement 
    protected  abstract  void  breed () ;

    // Slaughter method waits for subclass to implement 
    protected  abstract  void  slaughter () ;

    // Fried method waits for subclass to implement 
    protected  abstract  void  Fried () ;

}

McDonald’s manufacturers

/**
 * @author meat
 * @version 1.0
 * @date 2022/3/21 15:27
 * @description McDonald's made fried chicken
 */
public class McDonald extends AbstractFriedChicken{

    /**
     * Farmed by McDonald's manufacturers
     * */
    @Override
    protected void breed() {
        System.out.println( "McDonald's chickens are fed 4 times a day" );
    }

    /**
     * McDonald's factory slaughter
     * */
    @Override
    protected void slaughter() {
        System.out.println( "McDonald's manual slaughter" );
    }

    /**
     * McDonald's factory fried
     * */
    @Override
    protected void Fried() {
        System.out.println( "McDonald's frying oil is changed every 2 days" );
    }
}

KFC manufacturers

/**
 * @author meat
 * @version 1.0
 * @date 2022/3/21 15:28
 * @description KFC factory made fried chicken
 */
public class KFC extends AbstractFriedChicken{

    /**
     * KFC factory farming
     * */
    @Override
    protected void breed() {
        System.out.println( "KFC chickens are fed twice a day" );
    }

    /**
     * KFC factory slaughter
     * */
    @Override
    protected void slaughter() {
        System.out.println( "KFC slaughtered by machine" );
    }

    /**
     * KFC factory fried
     * */
    @Override
    protected void Fried() {
        System.out.println( "KFC oil for frying is changed once a day" );
    }
}

Test class & console output

Advantages and disadvantages of template pattern

advantage:

  1. Common logic code extraction, code reuse
  2. The parent class controls the behavior, and the child class implements it
  3. Encapsulate the unchanged part, rewrite the variable part, easy to extend

Disadvantages: Every time a subclass comes, a set of subclass specifications must be defined, and the size of the project will become larger and larger.

Summarize

The concept of template mode is briefly introduced and an example is given to facilitate understanding. If you want to see the use of template mode in the framework source code, you can click the link below to jump.

Finally, if you have any questions, you can leave a message in the comment area. If this article is helpful to you, I hope to like + follow + favorite, your support is my biggest motivation, and I will continue to update the use of various frameworks and source code interpretation~!

Leave a Comment

Your email address will not be published. Required fields are marked *