模板方法模式(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值