Design Pattern Template Method Pattern Notes

illustrate

Record the writing method of learning design pattern-template method pattern. The JDK version used is version 1.8.

Template Method

Intent : Define the algorithm skeleton in an operation while deferring some steps to subclasses. Template Method allows subclasses to redefine certain specific steps of an algorithm without changing the structure of the algorithm.
Structure :

Insert image description here

in:

  • AbstractClass (abstract class) defines abstract primitive operations, and specific subclasses will redefine them to implement each step of an algorithm: implement a template method and define the skeleton of an algorithm. The template method not only calls primitive operations, but also calls definitions Operations in AbstractClass or other objects.
  • ConcreteClass (concrete class) implements primitive operations to complete the steps in the algorithm related to a specific subclass.

applicability:

  • Implement the immutable parts of an algorithm once and leave the variable behavior to subclasses.
  • Common behaviors in each subclass should be extracted and concentrated into a common parent class to avoid code duplication.
  • Control subclass extensions. Template methods are designed to call "hook" operations at specific points (the default behavior, which subclasses can redefine and extend if necessary), which only allows extension at these points.

scenes to be used:

  • The overall steps of the algorithm are very fixed, but when individual parts are variable, you can use the template method pattern to abstract the easily variable parts for implementation by subclasses.
  • It is necessary to use the subclass to decide whether to execute a certain step in the parent class algorithm, so as to realize the reverse control of the parent class by the subclass.

Table of contents

Insert image description here

Template method pattern example class diagram

Insert image description here

Use this UML class diagram to implement the template method pattern example.

abstract class

package com.example.deesign_patterns.template_method;

//抽象类(定义模板方法和基本方法)
public abstract class AbstractClass {
    
    

    //模板方法定义,烹饪方法
    public final void cookProcess(){
    
    
        pourOil();
        heatOil();
        pourVegetable();
        pourSauce();
        fry();
    }

    //第一步:倒油是一样的,所以直接实现
    public void pourOil(){
    
    
        System.out.println("倒油");
    }

    //第二步:热油是一样的,所以直接实现
    public void heatOil(){
    
    
        System.out.println("热油");
    }

    //第三步:倒蔬菜是不一样的(一个下包菜,一个是下菜心)
    public abstract void pourVegetable();

    //第四步:倒调位料是不一样的
    public abstract void pourSauce();

    //第五步:翻炒是一样的,所以直接实现
    public void fry(){
    
    
        System.out.println("炒啊炒啊炒到熟啊");
    }
}

Cabbage

package com.example.deesign_patterns.template_method;

//包菜类
public class ConcreteClassCabbage extends AbstractClass{
    
    

    @Override
    public void pourVegetable() {
    
    
        System.out.println("下锅的蔬菜是包菜");
    }

    @Override
    public void pourSauce() {
    
    
        System.out.println("下锅的酱料是辣椒");
    }
}

Cabbage

package com.example.deesign_patterns.template_method;

//菜心类
public class ConcreteClassHeart extends AbstractClass{
    
    

    @Override
    public void pourVegetable() {
    
    
        System.out.println("下锅的蔬菜是菜心");
    }

    @Override
    public void pourSauce() {
    
    
        System.out.println("下锅的酱料是蒜蓉");
    }
}

Test class

package com.example.deesign_patterns.template_method;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //炒包菜,创建包菜对象
        ConcreteClassCabbage cabbage=new ConcreteClassCabbage();
        //炒菜心,创建菜心对象
        //ConcreteClassHeart cabbage=new ConcreteClassHeart();
        //调用烹饪方法
        cabbage.cookProcess();
    }
}

Insert image description here

benefit:

  • Improve code reusability. Put the same parts of code in an abstract parent class, and put different code in different subclasses.
  • Reverse control is achieved. Through the operation of a parent class calling its subclass, and by extending different behaviors through the specific implementation of the subclass, reverse control is achieved and complies with the opening and closing principle.

shortcoming:

  • Each different implementation needs to define a subclass, which will lead to an increase in the number of classes, a larger system, and a more abstract design.
  • The abstract methods in the parent class are implemented by the subclass, and the execution result of the subclass will affect the result of the parent class, which leads to a reverse control result, which increases the difficulty of code reading.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131352329