Template method pattern

1. Definitions

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

2. The structure of the template method pattern

write picture description here
- AbstractClass : An abstract class used to define algorithm skeletons and primitive operations, and concrete subclasses implement various steps of an algorithm by redefining these primitive operations. In this class, you can also provide a common implementation of the algorithm.
- ConcreteClass: Concrete implementation class. Used to implement certain steps in the algorithm skeleton to complete functions related to specific subclasses!

3. Essence

Fixed algorithm skeleton

4. Examples

Going to a bank to do business, such as withdrawing money, depositing money or applying for a card, basically requires three major steps (skeleton). Each step can be extracted into the parent class for definition, (1) number selection (3) service scoring is the same operation, which can be directly implemented in the parent class, and then (2) the specific business needs to be implemented in the subclass. accomplish.

BankBusinessTemplate

/**
 *银行办业务模板
 * @author lijun
 * @since 2018-03-19 15:17
 */
public abstract class BankBusinessTemplate {

    /**
     * 取号
     */
    private  void  takeNumber(){
        System.out.println("取到60号,等待办理业务");
    }

    /**
     * 办业务
     */
    public abstract void transactBusiness();

    /**
     * 服务评价打分
     */
    private void evaluate(){
        System.out.println("非常满意!");
    }

    /**
     * 业务处理
     */
    public void  progress(){
        takeNumber();
        transactBusiness();
        evaluate();
    }

}

WithdrawalBusiness

/**
 * 取款业务
 * @author lijun
 * @since 2018-03-19 15:25
 */
public class WithdrawalBusiness extends BankBusinessTemplate {
    /**
     * 办业务
     */
    @Override
    public void transactBusiness() {
        System.out.println("正在办理取款业务取出300元.");
    }
}

DepositBusiness

/**
 * 存款业务
 * @author lijun
 * @since 2018-03-19 15:28
 */
public class DepositBusiness extends BankBusinessTemplate {
    /**
     * 办业务
     */
    @Override
    public void transactBusiness() {
        System.out.println("正在办理存款业务存入300元.");
    }
}

ApplyCardBusiness

/**
 * 办卡业务
 * @author lijun
 * @since 2018-03-19 15:35
 */
public class ApplyCardBusiness  extends BankBusinessTemplate {
    /**
     * 办业务
     */
    @Override
    public void transactBusiness() {
        System.out.println("正在办理办卡业务 你成功申请了一张卡卡号 8888888888888888");
    }
}

transfer

 @Test
    public  void testWithDrawal(){
        BankBusinessTemplate businessTemplate = new WithdrawalBusiness();
        businessTemplate.progress();
    }

output

取到60号,等待办理业务
正在办理取款业务取出300元.
非常满意!

The template design pattern is often used in database operations. JDBC connection to the database usually has the following steps:
1. Obtain the connection
2. Create the statement set
3. Execute the statement set and obtain the result set
4. Parse the statement set
5. Close the result set
6. Close the statement set
7. Close the connection

Among them, 1,3,5,6,7 are basically fixed and 2,4 are not fixed. It also satisfies the structure of the template method. We can also use the template method to implement JDBC connection to the database for addition, deletion, modification and query! For specific examples, please download the source code. The source code only implements the query function!

Source address

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723686&siteId=291194637