Design Mode - Template Pattern

When reading the Spring source code, you find that there are a number of template patterns used in the Spring code, such as loading configuration files from the file system directory (FileSystemXmlApplicationContext), loading configuration files from the class path (ClassPathXmlApplicationContext), and loading configuration files from the project context directory (XmlWebApplicationContext).This uses the template design pattern during loading, so I intentionally learned the template design pattern to better understand the source code.

1. Definition of template design patterns in books

Defines the skeleton of the algorithm in an operation, delaying some steps to subclasses.Template methods allow subclasses to redefine certain steps of an algorithm without changing its structure.

My translation is that there are a fixed number of steps to complete a thing, but each step has different implementation details depending on the object; you can define a total method to complete the thing in the parent class and call the implementation method of each step according to the steps required to complete the event.The implementation of each step is done by subclasses.

2. For example

Zhang's team recently accepted a need to automate the brewing of coffee and tea in a coffee shop.Previously, this coffee shop was manually prepared by baristas for coffee and tea.Now coffee shops need to introduce automated ordering and beverage preparation systems, and Zhang is responsible for the functions of beverage preparation.

The process of making coffee and tea by hand by a barista:

Making coffee:

Boil water

Make coffee with boiling water

Put the coffee in the cup

Sugar and milk

Make tea:

Boil water

Brewing tea with boiling water

Pour the tea into the cup

* Add lemon

Implement using template mode

Let's first look at a class diagram

  

Code implementation

package xuelongjiang.designpartten.templatemethod;

/**
 *
 * Template Method
 *
 *
 */
abstract   public class CaffeineBeverage {

    //Algorithm.The algorithm for abstract classes is final Not allowed to be modified by subclasses
     public final  void   prepareRecipe(){

         //Specific steps of the algorithm
         boilWater(); //Boiling water
         brew();//Brewing
         pourInCup();// Pour the drink into a cup
         if(hook()){
             addCondiments();// Seasoning
         }

    }


   public  abstract  void brew();

   public   abstract  void addCondiments();


    public void boilWater(){

        System.out.println("Boiling water");
    }


    public  void pourInCup(){
        System.out.println("Pour the drink into a cup");
    }

    /**
     *  Hook, implementation can do some control on algorithm steps
     *
     * @return
     */
    public boolean hook(){
        return true;
    }

}

You can see that we have a hook method in the abstract class of brewing coffee/tea, which is the hook method.Returns true by default, and if brewing coffee is seasoned by default, the subclass does not have to override the hook method.

* Coffee

package xuelongjiang.designpartten.templatemethod;

/**
 *Coffee
 */
public class Coffee extends  CaffeineBeverage {


    @Override
    public void brew() {
        System.out.println("Brewing ground coffee with boiling water");
    }

    @Override
    public void addCondiments() {
        System.out.println("Sugar and milk");
    }

    @Override
    public boolean hook() {
        return super.hook();
    }
}

Teas

package xuelongjiang.designpartten.templatemethod;

/**
 * Teas
 */
public class Tea extends  CaffeineBeverage {


    @Override
    public void brew() {
        System.out.println("Soak tea with boiling water");
    }

    @Override
    public void addCondiments() {
        System.out.println("Add lemon");
    }


    @Override
    public boolean hook() {
        return super.hook();
    }
}

- Test Class

package xuelongjiang.designpartten.templatemethod;
/**
 * Test Class
 */
public class TemplateMethodTest {


    public static void main(String[] args) {
        CaffeineBeverage caffeineBeverage = new Tea();
        caffeineBeverage.prepareRecipe();

        System.out.println("-------------------");

        caffeineBeverage = new Coffee();
        caffeineBeverage.prepareRecipe();


    }
}

  

Key points)

Hollywood Principle: Don't call us, we'll call you.(That is, the way high-level components do to low-level components is: don't call us, we'll call you).

The template method defines the steps of the algorithm, delaying their implementation to subclasses.

Template method patterns provide an important technique for code reuse.

Abstract classes of template methods can define specific methods, Abstract methods, and hooks.

Abstract methods are implemented by subclasses.

To prevent subclasses from altering the algorithm in a template method, you can declare the template method final


---------------------
Code section reference: https://blog.csdn.net/u013565163/article/details/79285617

Tags: brew Spring

Posted on Tue, 21 Apr 2020 11:02:08 -0700 by USMarineNCO