Template method pattern of java design pattern (12)

Struggle is not necessarily all for money, but also the confidence, calmness and calmness in the bones! I can afford what I like, go where I want to go, and be able to assume the responsibility, spend my money, and live. A quality life! Be an irreplaceable yourself!

definition

The template method pattern defines the steps of an algorithm, and allows subcategories to provide practice for one or more steps. Let the subcategory redefine some steps in the algorithm without changing the algorithm architecture. In software engineering, it is a software design pattern and has no connection with C++ templates.

Design pattern learning, I will blog about 23 design patterns in the near future , so stay tuned~
—1/15/2021

Baidu Encyclopedia

scenes to be used

I want to make soy milk, first of all I need:

  • Prepare soybeans
  • Prepare red beans, soybeans, black beans
  • Add rock sugar
  • Polished into soy milk

It is roughly divided into these steps.

In general, the production process of soy milk is basically the same, but the ingredients are different. I want to drink red bean soy milk, I put red beans, I want to drink soy bean paste, I put soybeans, and finally you can choose to add or not add rock sugar. Here comes the template method mode.

Thinking analysis

The template method pattern is to standardize the code flow by defining a template

UML类图(1.1):
Insert picture description here
By defining the ATemplate abstract method to standardize the soy milk production process, its subclass BlackSoybeanMilkImpl implements what kind of beans to put, and if you put black beans here, you will get black soy milk.

Role analysis

  • ATemplate (abstract class): The template is implemented in the class, the overall code thinking skeleton is defined, and the specific code subclasses are implemented.
  • BlackSoybeanMilkImpl (implementation class): implements the specific code
  • Client: The client directly calls the implementation class, and then uses the polymorphic mechanism to call the template method of the parent class

Code

The ATemplate abstract class defines the code specification:

public abstract class ATemplate {
    
    

    //用来规范豆浆流程
    void aemplate() {
    
    
        soybean();//准备黄豆
        addBean();//添加豆子
        sugar();  //准备冰糖
        polish(); //打豆浆
    }
    //准备黄豆
   private void soybean() {
    
    
        Log.i("模板方法模式 ","准备黄豆");
    }

    //添加豆子
    public abstract void addBean();

    //准备冰糖
    private void sugar() {
    
    
        Log.i("模板方法模式 ","放入冰糖");
    }

    //打豆浆
    private void polish() {
    
    
        Log.i("模板方法模式 ","开始打磨豆浆 预计10分钟完毕");
    }
}

BlackSoybeanMilkImpl black bean implementation:

public class BlackSoybeanMilkImpl extends ATemplate{
    
    

    @Override
    public void addBean() {
    
    
        Log.i("模板方法模式 ","放入 黑豆浆");
    }
}

RedSoybeanMilkImpl Soybean Implementation:

public class RedSoybeanMilkImpl extends ATemplate{
    
    
    @Override
    public void addBean() {
    
    
        Log.i("模板方法模式 ","放入 红豆浆");
    }
}

Client (test code):

//制作黑豆浆
ATemplate blackSoybeanMilk = new BlackSoybeanMilkImpl();
blackSoybeanMilk.aemplate();

Log.i("模板方法模式 "," ===========================");

//制作红豆浆
RedSoybeanMilkImpl redSoybeanMilk = new RedSoybeanMilkImpl();
redSoybeanMilk.aemplate();

Log图(2.1):
Insert picture description here

Hook method understanding

Still the example above, if I want to drink pure soy milk without sugar, I will use the hook here."

Hook method function: identify whether to execute a certain piece of code;

Hook code implementation

ATemplate adds hook method isSugar():

public abstract class ATemplate {
    
    
    //用来规范豆浆流程
    void aemplate() {
    
    
        soybean();//准备黄豆
        addBean();//添加豆子
        if (isSugar()) {
    
    
            sugar();  //准备冰糖
        }
        polish(); //打豆浆
    }
    //准备黄豆
    private void soybean() {
    
    
        Log.i("模板方法模式 ", "准备黄豆");
    }

    //添加豆子
    public abstract void addBean();

    //准备冰糖
    private void sugar() {
    
    
        Log.i("模板方法模式 ", "放入冰糖");
    }

    //打豆浆
    private void polish() {
    
    
        Log.i("模板方法模式 ", "开始打磨豆浆 预计10分钟完毕");
    }

    //默认加糖 钩子方法
    public boolean isSugar() {
    
    
        return true;
    }
}

PureSoybeanMilkImpl pure soy milk production:

Rewrite the hook method () and modify it to no sugar:

public class PureSoybeanMilkImpl extends ATemplate{
    
    
    @Override
    public void addBean() {
    
    
        Log.i("模板方法模式 ","我这里什么都不加,我要喝纯豆浆");
    }
    //不加糖
    @Override
    public boolean isSugar() {
    
    
        return false;
    }
}

Client (test code):

//制作黑豆浆
ATemplate blackSoybeanMilk = new BlackSoybeanMilkImpl();
blackSoybeanMilk.aemplate();

Log.i("模板方法模式 "," ===========================");

//制作红豆浆
RedSoybeanMilkImpl redSoybeanMilk = new RedSoybeanMilkImpl();
redSoybeanMilk.aemplate();


Log.i("模板方法模式 "," ===========================");

//制作纯豆浆  使用钩子 不加糖
PureSoybeanMilkImpl pureSoybeanMilk = new PureSoybeanMilkImpl();
pureSoybeanMilk.aemplate();

Log图(2.2):
Insert picture description here

to sum up:

  • The template method pattern is relatively simple, that is, the code flow is standardized through an abstract class, and the specific places that need to be changed are written as abstract methods and let their subclasses implement them.

  • A hook is a'method variable' in the template method abstract class, which identifies whether to execute a certain piece of code;

Complete code

Go to the Design Patterns/Design Principles homepage

Originality is not easy, your likes are your greatest support for me~

Guess you like

Origin blog.csdn.net/weixin_44819566/article/details/112651624