Design patterns learning summary (13) - Template Method Pattern

definition

Template Method is to define the so-called skeleton algorithms in a one method, some steps to subclasses delay. Template methods such subclasses without changing the structure of the algorithm, some of the steps of the algorithm are redefined.

Template Method pattern is based on inherited code reuse technology. In the method of the template pattern, we can use the same code in the portion of the parent class, and the different code into different subclasses. That we need to declare an abstract parent class, will be part of the logic implemented in the form of specific methods and specific constructor, then declare some abstract way for subclasses to implement the remaining logic, different subclasses can be in different ways enable this logic.

In fact, the so-called template is a method that has become the definition of the algorithm is a set of steps, any steps which can all be abstract, referred to subclasses to be responsible for implementation. This ensures that the structure of the algorithm remains unchanged, while providing section implemented by subclasses.

A template is a method, so different from his ordinary existence and the way to do that? Template method is defined in an abstract class, the basic operations of a method for forming the total combined algorithm or a set of steps. The common approach is to implement the various steps of the method, we can consider the general approach is an integral part of the template approach.

Character

  • AbstractClass: abstract class
  • ConcreteClass: Specific subclasses

 Advantages and disadvantages

advantage

  • After the template method pattern defines a set of algorithms to achieve specific subclass referred to responsible.

  • Template Method mode is a code reuse techniques.

  • Template Method pattern results in a reverse control structure, called through a parent operating its subclasses, adding new behavior by extending the sub-class, in line with "the principle of opening and closing."

Shortcoming

  • Each of the different implementations require a subclass to achieve, resulting in an increase in the number of class makes the system more bulky.

Examples

With coffee and tea, for example, they require the following steps:

1) 烧开水
2) 将咖啡/茶叶放入杯中
3)将开水倒入杯中

Template abstract class:

public abstract class BrewingBeverage {


    /**
     * 添加咖啡/茶叶,具体逻辑由子类实现
     */
    abstract void addCondiments();

    /**
     *
     * @desc
     *   模板方法,用来控制泡茶与冲咖啡的流程
     *   申明为 final,不希望子类覆盖这个方法,防止更改流程的执行顺序
     * @return void
     */
    final void prepareRecipe(){
        boilWater();
        addCondiments();
        pourInCup();
    }

    /**
     * 烧水
     */
    void boilWater(){
        System.out.println("烧水");
    }

    /**
     * 将水倒入杯中
     */
    void pourInCup(){
        System.out.println("将水倒入杯中");
    }
}

Specific implementation class:

public class Tea extends BrewingBeverage {
    @Override
    void addCondiments() {
        System.out.println("添加茶叶");
    }
}

public class Coffee extends BrewingBeverage {
    @Override
    void addCondiments() {
        System.out.println("添加咖啡");
    }
}

transfer:

public static void main(String[] args) {
    Tea tea = new Tea();
    tea.prepareRecipe();

    Coffee coffee = new Coffee();
    coffee.prepareRecipe();;
}

Console output:

烧水
添加茶叶
将水倒入杯中
烧水
添加咖啡
将水倒入杯中

Guess you like

Origin www.cnblogs.com/markLogZhu/p/11582617.html