In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要介绍"什么是模板模式",在日常操作中,相信很多人在什么是模板模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"什么是模板模式"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1、概述
模板模式:封装了一个算法步骤(prepareRecipe),并允许子类为一个或多个步骤方法提供实现模板模式可以使子类在不改变算法结构的情况下,重新定义算法中的某些步骤。
模板是一个抽象类,模板中所包含的内容:
1、算法步骤,使用final修饰,子类不可更改
2、已经实现的方法,使用final修饰,子类不可更改
3、抽象的方法
4、hook钩子,子类可自定义实现
如泡茶和泡咖啡,总共有以下四个步骤:
1、烧水
2、冲泡
3、倒入杯子中
4、添加调味品
所以我们将这四个步骤定义成一个模板,具体代码如下
public abstract class HotDrink { // 封装了算法步骤,使用final修饰,表示子类不能更改算法步骤 public final void prepareRecipe() { boilWater(); brew(); pourInCup(); addCondiments(); } public abstract void boilWater(); public abstract void brew(); public abstract void pourInCup(); public abstract void addCondiments();}
由于泡茶和泡咖啡的第一步和第三步操作是相同的,所以我们可以对父类模板作进一步的优化
public abstract class HotDrink { public final void prepareRecipe() { boilWater(); brew(); pourInCup(); addCondiments(); } // 已经实现的方法 public final void boilWater() { System.out.println("Boiling water"); } // 抽象方法 public abstract void brew(); public final void pourInCup() { System.out.println("Pouring into cup"); } public abstract void addCondiments();}
泡茶类实现如下
public class Tea extends HotDrink{ @Override public void brew() { System.out.println("Brewing tea"); } @Override public void addCondiments() { System.out.println("Adding lemon"); }}
泡咖啡类实现如下
public class Coffee extends HotDrink{ @Override public void brew() { System.out.println("Brewing Coffee "); } @Override public void addCondiments() { System.out.println("Adding sugar and milk"); }}
运行类实现如下
public class MainTest { public static void main(String[] args) { HotDrink mCoffee = new Coffee() ; HotDrink tea = new Tea(); mCoffee.prepareRecipe(); tea.prepareRecipe(); }}
2、模板模式中的hook
虽然超类的算法步骤是final的,但是子类可以通过覆盖hook函数的方法来影响超类的算法步骤
hook为子类覆盖方法的可选项,父类默认实现,子类可进行覆盖后更改
如泡茶和泡咖啡中的第四步,假如我们将模板定义为第四步是可以选项,则我们可以将模板代码定义成如
public abstract class HotDrinkTemplate { public final void prepareRecipe() { boilWater(); brew(); pourInCup(); // 默认为加佐料 if(wantCondimentsHook()){ addCondiments(); }else{ System.out.println("No Condiments"); } } // hook方法默认实现,子类可覆盖 public boolean wantCondimentsHook() { return true; } public final void boilWater() { System.out.println("Boiling water"); } public abstract void brew(); public final void pourInCup() { System.out.println("Pouring into cup"); } public abstract void addCondiments();}
子类可实现如下
public class TeaWithHook extends HotDrinkTemplate{ @Override public void brew() { System.out.println("Brewing tea"); } @Override public void addCondiments() { System.out.println("Adding lemon"); } @Override public boolean wantCondimentsHook() { return false; }}到此,关于"什么是模板模式"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.