模板方法模式

一. 定义

         模板方法模式在一个方法中定义了一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。

二. 模式起源

三. 原则

3.1 好莱坞原则

        别调用我们,我们会调用你。

四. UML图

         

五. 代码

5.1 饮料类(模板方法类)

package model.method;

/**
 * 饮料类
 */
public abstract class Drinking {

    /**
     * 步骤1:烧水
     */
    public void boilWater() {
        System.out.println("the water has been boiled...");
    }

    /**
     * 步骤2:浸泡 (茶叶或咖啡豆) 具体由子类实现
     */
    public abstract void brew();

    /**
     * 步骤3:将饮料倒入杯子
     */
    public void pourInCup() {
        System.out.println("the drink has been into the cup...");
    }

    /**
     * 步骤4:添加佐料  具体由子类实现
     */
    public abstract void addCondiments();

    /**
     * 是否需要佐料
     * 模板方法模式的钩子, 可以用来判断是否需要执行某个步骤
     *
     * @return
     */
    public boolean needCondiments() {
        return true;
    }

    /**
     * 模板方法  定义了方法的执行步骤
     * 为了防止子类进行修改,设置为final
     */
    public final void prepare() {

        boilWater();
        brew();
        pourInCup();

        // 钩子方法needCondiments()的使用 子类可以通过覆盖此方法来判断是否要执行addCondiments()方法
        if (needCondiments()) {
            addCondiments();
        }
    }

}

5.2 茶类

package model.method;

/**
 * 茶
 */
public class Tea extends Drinking {

    @Override
    public void brew() {
        System.out.println("brewing the tea...");
    }

    @Override
    public void addCondiments() {
        System.out.println("adding lemon...");
    }
}

5.3 咖啡类

package model.method;

/**
 * 咖啡类
 */
public class Coffee extends Drinking {

    @Override
    public void brew() {
        System.out.println("brewing the coffee...");
    }

    @Override
    public void addCondiments() {
        System.out.println("adding milk...");
    }

    @Override
    public boolean needCondiments() {
        // 钩子方法: 内部可以做处理,来判断是否需要执行步骤
        return false;
    }
}

5.4 测试

package model.method;

/**
 * 测试类
 */
public class Main {

    public static void main(String[] args) {

        System.out.println("使用旧方法泡茶...");
        Tea oldTea = new Tea();
        oldTea.boilWater();
        oldTea.brew();
        oldTea.pourInCup();
        oldTea.addCondiments();

        System.out.println("使用旧方法泡咖啡...");
        Coffee oldCoffee = new Coffee();
        oldCoffee.boilWater();
        oldCoffee.brew();
        oldCoffee.pourInCup();
        oldCoffee.addCondiments();

        System.out.println("使用模板方法:泡茶...");
        Tea tea = new Tea();
        tea.prepare();

        System.out.println("使用模板方法:泡咖啡...");
        Coffee coffee = new Coffee();
        coffee.prepare();
    }

}

5.5 结果

使用旧方法泡茶...
the water has been boiled...
brewing the tea...
the drink has been into the cup...
adding lemon...
使用旧方法泡咖啡...
the water has been boiled...
brewing the coffee...
the drink has been into the cup...
adding milk...
使用模板方法:泡茶...
the water has been boiled...
brewing the tea...
the drink has been into the cup...
adding lemon...
使用模板方法:泡咖啡...
the water has been boiled...
brewing the coffee...
the drink has been into the cup...

六. 感悟

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值