Design pattern - template method pattern

Template method pattern

1. Template method mode

In Template Pattern, an abstract class publicly defines the method / template for executing its method. Its subclass can override the method implementation as needed, but the call will be made in the way defined in the abstract class

1. Introduction

Intent: define the skeleton of the algorithm in an operation and delay some steps to subclasses. Template method allows subclasses to redefine some specific steps of an algorithm without changing the structure of the algorithm.

Main solution: some methods are common, but this method is rewritten in each subclass.

Key code: it is implemented in the abstract class, and other steps are implemented in the subclass.

2. Application examples:

When cooking, the process is roughly the same, with steps 1, 2, 3 and 4. However, the methods are different for different meals, but the steps of cleaning and adding water are the same
1. Choose different ingredients for different meals
2. Clean food materials
3. Cooking ingredients
4. Differences such as packaging plate.
Advantages: 1. Package the unchanged part and expand the variable part. 2. Extract common codes for easy maintenance. 3. The behavior is controlled by the parent class and implemented by the child class.
Disadvantages: each different implementation requires a subclass to implement, resulting in an increase in the number of classes and a larger system.
Usage scenario: 1. There are methods shared by multiple subclasses with the same logic. 2. Important and complex methods can be considered as template methods.

code

1. The parent class is an abstract template, and the unified method is set as final
package com.jzj.design.demo.templateDesign;

/**
 * Cooking abstract class
 * Cooking process
 * 1.Choose food materials
 * 2.Cleaning ingredients
 * 3.Put it into the pot, cook, high fire, low fire, how long
 * 4.Take it out and pack it
 */
public abstract class AbstractCookTemplate {

    /**
     * Define the main process of cooking template
     */
    public final void cook() {

        /**
         * 1.Select ingredients
         */
        String material = selectMaterial();
        System.out.println("1.The selected ingredients are as follows:" + material);

        /**
         * 2.Abstract method, cleaning ingredients
         */
        System.out.println("2.Start cleaning ingredients");
        washMaterial();



        /**
         * 3.Abstract method, cooking ingredients
         */
        System.out.println("3.Start cooking ingredients");
        cookMaterial();


        /**
         * 4.Abstract methods, packaging
         */
        System.out.println("4.Pack and plate");
        pack();
    }


    /**
     * 1.Abstract method, food selection
     */
    public abstract String selectMaterial();


    /**
     * 2.Abstract method, cleaning ingredients
     */
    void washMaterial() {
        //Add farmer spring water
        System.out.println("Add farmer spring water to the pot");
        System.out.println("Unified cleaning process");
    }

    ;

    /**
     * 3.Abstract method, cooking ingredients
     */
    abstract void cookMaterial();


    /**
     * 4.Abstract methods, packaging
     */
    abstract void pack();

}

2. Subclasses to implement different methods

Rice should be cooked for 30 minutes and put in a thermos cup. The rice subclass should realize steps 1, 3 and 4

package com.jzj.design.demo.templateDesign;

/**
 * Steamed rice
 */
public class RiceCook extends AbstractCookTemplate {
    @Override
    public String selectMaterial() {
        return "Northeast rice";
    }

    @Override
    public void cookMaterial() {
        System.out.println("Northeast rice needs to be steamed over low heat for 30 minutes to make delicious rice");
    }

    @Override
    public void pack() {
        System.out.println("Rice should be packed in a thermos cup to prevent it from getting cold");
    }
}

3. Other subclasses implement different methods

Babao porridge should be made of 8 materials, and then boiled in a pressure cooker. It also needs to be put in a sealed jar to prevent leakage

package com.jzj.design.demo.templateDesign;

/**
 * Make eight treasure porridge
 */
public class CongeeCook extends AbstractCookTemplate {
    @Override
    public String selectMaterial() {
        return "Red bean, mung bean, lentil, peanut, longan, red jujube, rice, red rice";
    }

    @Override
    public void cookMaterial() {
        System.out.println("Babao porridge needs to be steamed in a pressure cooker for 20 minutes, and then simmered over a low heat for 10 minutes to be soft, waxy and delicious");
    }

    @Override
    public void pack() {
        System.out.println("The pipes for packing Babao porridge need to be sealed to prevent leakage of Babao porridge");
    }
}

4. Result display
package com.jzj.design.demo.templateDesign;

public class AbsTplMain {

    public static void main(String[] args) {

        CongeeCook congeeCook = new CongeeCook();
        congeeCook.cook();

        System.out.println("=======================");
        RiceCook riceCook = new RiceCook();
        riceCook.cook();

    }

}

Keywords: Java Design Pattern

Added by jaytux on Wed, 15 Dec 2021 07:04:24 +0200