模板方法模式(java语言实现)

什么是模板方法模式

定义:

定义了统一的算法框架,将部分实现延迟到子类中实现。 使得子类在不改变算法结构的同时可以重新定义该算法框架的某些定义步骤。

如何实现模板方法模式

业务和场景分析

这里写图片描述
这里写图片描述
这里写图片描述

uml

这里写图片描述

RefreshBeverage.java

package com.imooc.pattern.template;

/*
 * 抽象基类,为所有子类提供一个算法框架
 *
 * 提神饮料
 */
public abstract class RefreshBeverage {

    /*
     * 制备饮料的模板方法
     * 封装了所有子类共同遵循的算法框架
     */
    public final void prepareBeverageTemplate(){
        //步骤1 将水煮沸
        boilWater();
        //步骤2 泡制饮料
        brew();
        //步骤3 将饮料倒入杯中
        pourInCup();
        if(isCustomerWantsCondiments()){
            //步骤4 加入调味料
            addCondiments();
        }
    }

    /*
     * Hook, 钩子函数,提供一个默认或空的实现
     * 具体的子类可以自行决定是否挂钩以及如何挂钩
     * 询问用户是否加入调料
     */
     protected boolean isCustomerWantsCondiments() {
        return true;
    }

    /*
     * 基本方法,将水煮沸
     */
    private void boilWater() {
        System.out.println("将水煮沸");
    }

    /*
     * 基本方法,将饮料倒入杯中
     */
    private void pourInCup() {
        System.out.println("将饮料倒入杯中");
    }

    /*
     * 抽象的基本方法,泡制饮料
     */
    protected abstract void brew();


    /*
     * 抽象的基本方法, 加入调味料
     */
    protected abstract void addCondiments();


}

Coffee.java

package com.imooc.pattern.template;

/*
 * 具体子类,提供了咖啡制备的具体实现
 */
public class Coffee extends RefreshBeverage {

    @Override
    protected void brew() {
        System.out.println("用沸水冲泡咖啡");

    }

    @Override
    protected void addCondiments() {
        System.out.println("加入糖和牛奶");
    }

}

Tea.java

package com.imooc.pattern.template;

/*
* 具体子类,提供了制备茶的具体实现
*/
public class Tea extends RefreshBeverage {

 @Override
 protected void brew() {
   System.out.println("用80度的热水浸泡茶叶5分钟");
 }

 @Override
 protected void addCondiments() {
   System.out.println("加入柠檬");
 }

 @Override
 /*
  * 子类通过覆盖的形式选择挂载钩子函数
  * @see com.imooc.pattern.template.RefreshBeverage#isCustomerWantsCondiments()
  */
 protected boolean isCustomerWantsCondiments(){
   return false;
 }

}

RefreshBeverageTest.java

package com.imooc.pattern.template;

public class RefreshBeverageTest {

    public static void main(String[] args) {

        System.out.println("制备咖啡...");
        RefreshBeverage b1 = new Coffee();
        b1.prepareBeverageTemplate();
        System.out.println("咖啡好了!");

        System.out.println("\n******************************************");

        System.out.println("制备茶...");
        RefreshBeverage b2 = new Tea();
        b2.prepareBeverageTemplate();
        System.out.println("茶好了!");

    }

}

模板方法模式的特点

(https://en.wikipedia.org/wiki/Template_method_pattern) The template method is used in frameworks, where each implements the invariant parts of a domain’s architecture, leaving “placeholders” for customisation options. This is an example of inversion of control. Reasons to use the template method are to

Let subclasses implement (through method overriding) behavior that can vary.

Avoid duplication in the code: the general workflow structure is implemented once in the abstract class’s algorithm, and necessary variations are implemented in each of the subclasses.

Control at what point(s) subclassing is allowed. As opposed to a simple polymorphic override, where the base method would be entirely rewritten allowing radical change to the workflow, only the specific details of the workflow are allowed to change.

发布了81 篇原创文章 · 获赞 13 · 访问量 14万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章