Java design pattern template method pattern

What is a template method

The template method pattern is a behavioral pattern of a class. Define a template method of the operation process, and then declare some abstract methods, forcing the subclass to implement and improve its business logic. Different subclasses have different implementations, so the subclasses have different business logic.

Structure of the template method pattern

AbstractClass: The role of Abstract Template has the following responsibilities

  • One or more abstract operations are defined so that subclasses can implement them. These abstract operations are called basic operations, and they are the constituent steps of a top-level logic
  • Defined and implemented a template method. This template method is generally a concrete method, which gives a skeleton of the top-level logic, and the logical composition steps are deferred to the subclass implementation in the corresponding abstract operations. The top-level logic may also call some specific methods.

ConcreteClass: Concrete Template roles have the following responsibilities:

  • To implement the abstract methods defined by the parent class, they are the constituent steps of a top-level logic.
  • Each abstract template role can have any number of specific template roles corresponding to it, and each specific template role can give different implementations of these abstract methods (that is, the composition steps of the top-level logic), thereby enabling the realization of the top-level logic Each is different.

Insert picture description here

For example: such as payment function, there are Alipay payment, WeChat payment. You can define a payment template abstract class, define a payment implementation method, and then declare the payment method abstract class.

Template abstract class, define the specific implementation method of pay(monry), declare the abstract method callPay(money), implemented by subclasses

package com.template;

public abstract class AbstractPay {
    
    
    /**
     * 定义支付的实现方法
     * @param money 金额
     * @return
     */
    protected boolean pay(double money) {
    
    
        return callPay(money); // 调用字类的实现方法
    }
    /**
     * 声明抽象的支付方式
     * @param money
     * @return
     */
    protected abstract boolean callPay(double money);
}

The Alipay payment inheritance class implements the callPay(money) abstract method, indicating that the payment cannot be less than 20 yuan

package com.template.concrete;

import com.template.AbstractPay;

public class AliPay extends AbstractPay {
    
    
    protected boolean callPay(double money) {
    
    
        System.out.println("支付宝支付");
        if (money < 20.0D) {
    
    
            System.out.println("小于最小支付");
        }
        return true;
    }
}

WeChat payment inherits the class, implements the callPay(money) abstract method, indicating that the payment cannot be greater than 20 yuan

package com.template.concrete;

import com.template.AbstractPay;

public class WeChatPay extends AbstractPay {
    
    
    protected boolean callPay(double money) {
    
    
        System.out.println("微信支付");
        if (money > 20.0D) {
    
    
            System.out.println("超出最大支付了");
        }
        return true;
    }
}

Write another static factory pattern to create objects

package com.template.factory;

import com.template.AbstractPay;
import com.template.concrete.AliPay;
import com.template.concrete.WeChatPay;

public class PayFactory {
    
    
    public static AbstractPay getAbstractPay(String payCode) {
    
    
        switch (payCode) {
    
    
            case "ali_pay":
                return new AliPay();
            case "weChat_pay":
                return new WeChatPay();
            default:
                return null;
        }
    }
}

Insert picture description here
advantage:

The template method pattern is to remove the repetitive code in the subclass by moving the unchanged behavior to a unified parent class, and the subclass realizes some details of the template parent class, which is helpful for the expansion of the template parent class. The operation implemented by a parent class calling a subclass, and adding new behaviors through subclass extension, conforms to the "opening and closing principle".

Disadvantage

The result of subclass execution affects the result of the parent class, which will increase the difficulty of code reading.

scenes to be used

Multiple functions have common behavioral operations.

Guess you like

Origin blog.csdn.net/liuqi199707/article/details/109031303