1.定义
2.代码
package com.chengjie; abstract class CookVegetable { final void cook() { this.pourOil(); this.pourVegetable(); this.pourSauce(); this.fry(); } void pourOil() { System.out.println("Step1:倒油!"); } abstract void pourVegetable(); abstract void pourSauce(); void fry() { System.out.println("Step4:翻炒!"); } } class LajiaoBaocai extends CookVegetable { @Override void pourVegetable() { System.out.println("Step2:倒包菜!"); } @Override void pourSauce() { System.out.println("Step3:倒辣椒!"); } } class SuanniCaixin extends CookVegetable { @Override void pourVegetable() { System.out.println("Step2:倒菜心!"); } @Override void pourSauce() { System.out.println("Step3:倒蒜泥!"); } } public class TestTemplateMethod { public static void main(String[] args) { new LajiaoBaocai().cook(); new SuanniCaixin().cook(); } }
3.优点
4.缺点
5.应用场景
6.参考
https://www.jianshu.com/p/a3474f4fee57
原文:https://www.cnblogs.com/forTheDream1991/p/10450768.html