Interpretation of template method design pattern

Table of contents

Soy milk making problem

Basic introduction to template method pattern

basic introduction

Principle class diagram of template method pattern

The template method pattern solves the problem of making soybean milk 

Application example requirements

Idea analysis and diagram (class diagram)

 Hook method of template method pattern

 Notes and Details of the Template Method Pattern


Soy milk making problem

Write the program for making soymilk, and the description is as follows:
1) The process of making soymilk is to select materials ---> add ingredients ---> soak ---> put it into a soymilk machine and break it
2) By adding different ingredients, you can make different flavors 3) The steps of material selection, soaking, and crushing in a soymilk machine
are the same for every flavor of soymilk
4) Please use the template method mode to complete (Note: because the template method mode is relatively simple and easy I just thought of this solution, so I used it directly, no longer using the traditional solution to lead to the template method mode)

Basic introduction to template method pattern

basic introduction

1) Template Method Pattern (Template Method Pattern), also known as Template Pattern (Template Pattern), z exposes a template that defines the method that executes it in an abstract class. Its subclasses can override the method implementation as needed, but calls will be made in the manner defined in the abstract class.
2) Simply put, the template method pattern defines the skeleton of an algorithm in an operation, and delays some steps to the subclass, so that the subclass can redefine some specific steps of the algorithm without changing the structure of the
algorithm3 ) This type of design pattern is a behavioral pattern.

Principle class diagram of template method pattern


Explanation of the principle class diagram - that is (the role and responsibility of the template method pattern)
1) AbstractClass abstract class, the template method (template) is implemented in the class, and the skeleton of the algorithm is defined. The specific subclass needs to implement other abstract methods operationr2 ,3,4
2) ConcreteClass implements the abstract method operationr2,3,4 to complete the steps of characteristic subclasses in the algorithm 

The template method pattern solves the problem of making soybean milk 

Application example requirements

Write the program for making soymilk, and the instructions are as follows:
Process selection of soymilk ---> Adding ingredients ---> Soaking ---> Put it into a soymilk machine and crush it.
By adding different ingredients, you can make soymilk with different tastes
. The steps of soaking and crushing in a soymilk machine are the same for making soymilk of every flavor (red bean, peanut soymilk...)

Idea analysis and diagram (class diagram)

 SoyaMilk 

public abstract class SoyaMilk {

    final  void make(){
        select();
        addCondiments();
        soak();
        beat();
    }

    void  select(){
        System.out.println("第一步选好材料");
    }
    abstract  void  addCondiments();

    void  soak(){
        System.out.println("第三步黄豆和配料开始浸泡,需要 3 小时");
    }

    void  beat(){
        System.out.println("第四步黄豆和配料放到豆浆机去打碎");
    }

}

 RedBeanSoyaMilk 

public class RedBeanSoyaMilk extends  SoyaMilk{
    @Override
    void addCondiments() {
        System.out.println("加入好的红豆");
    }
}

 PeanutSoyaMilk  

public class PeanutSoyaMilk  extends  SoyaMilk{

    @Override
    void addCondiments() {
        System.out.println(" 加入上好的花生 ");
    }
}

 Client 

public class Client {
    public static void main(String[] args) {
        System.out.println("----制作红豆豆浆----");
        SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk();
        redBeanSoyaMilk.make();
        System.out.println("----制作花生豆浆----");
        SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk();
        peanutSoyaMilk.make();
    }
}

 

 Hook method of template method pattern

1) In the parent class of the template method pattern, we can define a method, which does nothing by default, and subclasses can override it depending on the situation. This method is called a "hook".
2) Still use the above example of making soy milk to explain. For example, we also want to make pure soy milk without adding any ingredients. Please use the hook method to modify the previous template method

public abstract class SoyaMilk {

    final  void make(){
        select();
        if (customerWantCondiments()){
            addCondiments();
        }
        soak();
        beat();
    }

    boolean customerWantCondiments() {
        return true;
    }

    void  select(){
        System.out.println("第一步选好材料");
    }
    abstract  void  addCondiments();

    void  soak(){
        System.out.println("第三步黄豆和配料开始浸泡,需要 3 小时");
    }

    void  beat(){
        System.out.println("第四步黄豆和配料放到豆浆机去打碎");
    }

}

PureSoyaMilk  

public class PureSoyaMilk extends SoyaMilk{

    @Override
    void addCondiments() {

    }

    @Override
    boolean customerWantCondiments() {
        return false;
    }
}

 Notes and Details of the Template Method Pattern

1) The basic idea is: the algorithm only exists in one place, that is, in the parent class, and it is easy to modify. When you need to modify the algorithm, just modify the template method of the parent class or some steps that have been implemented, and the subclass will inherit these modifications
2) Maximize code reuse. The template method of the parent class and some steps that have been implemented will be inherited by the subclass and used directly.
3) It not only unifies the algorithm, but also provides great flexibility. The template method of the parent class ensures that the structure of the algorithm remains unchanged, while the implementation of some steps is provided by the subclass.
4) The disadvantage of this mode: each different implementation requires a subclass implementation, resulting in an increase in the number of classes, making the system even larger 5) General template
methods are added with the final keyword to prevent subclasses from rewriting the template Method.
6) Usage scenario of template method pattern: When a certain process is to be completed, the process needs to perform a series of steps. This series of steps is basically the same, but the individual steps may be different in implementation. Usually, the template method pattern is considered to deal with

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/130249576