模版方法模式(行为型)
来源:互联网 发布:高德地图出行数据 编辑:程序博客网 时间:2024/06/08 13:12
模板方法
定义一个操作中的算法的骨架,将一些步骤延迟到子类中。
TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
适用性
1.一次性实现一个算法的不变的部分,并将可变的留给子类来实现。
2.各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。
首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。
最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。
3.控制子类扩展。
结构类图
head_first类图
系统结构图
------------------------------
具体子类
--------------------------------
public class CoffeeWithHook extends CaffeineBeverage {@Overridepublic void addCondiments() {// TODO Auto-generated method stubSystem.out.println("add the Coffee");}@Overridepublic void brew() {// TODO Auto-generated method stubSystem.out.println("add the Mike");}public boolean hook(){ System.out.println("do you want to implement parent method"); Scanner in=new Scanner(System.in); if(in.next().equals("y")){ return true; }else{ return false; } }}public class Tea extends CaffeineBeverage{@Overridepublic void addCondiments() {// TODO Auto-generated method stubSystem.out.println("add the Tea");}@Overridepublic void brew() {// TODO Auto-generated method stubSystem.out.println("adding lemon");}}
---------------------------------
模版
------------------------------
public abstract class CaffeineBeverage {public final void prepareRecipe() {boidWater();brew();pourInCup();if (hook()) {addCondiments();}}public abstract void addCondiments();public abstract void brew();public void pourInCup() {// TODO Auto-generated method stubSystem.out.println("pour into cup");}public void boidWater() {// TODO Auto-generated method stubSystem.out.println("pour the water");}//define a hook methodpublic boolean hook() {return true;}}
---------------------------------
测试类
---------------------------------
public class TestC {public static void main(String[] args) {// TODO Auto-generated method stubCaffeineBeverage coffeeWithHook=new CoffeeWithHook();coffeeWithHook.prepareRecipe();System.out.println("---------------------");CaffeineBeverage tea=new Tea();tea.prepareRecipe();}}
------------------------------------
结果
----------------------------------
pour the wateradd the Mikepour into cupdo you want to implement parent methodyadd the Coffee---------------------pour the wateradding lemonpour into cupadd the Tea
---------------------------------------------------
具体应用场景
0 0
- 模版方法模式(行为型)
- 行为型模式之模版方法模式
- 设计模式-模版方法(行为型)
- 14-行为型-模版方法模式
- 行为型模式-模版方法(template_method)
- 行为模式->模版方法模式
- 设计模式->行为型模式->模版方法模式
- 行为型-模版方法
- 行为型模式9:模版方法模式(Template Method)
- 行为型:设计模式之模版方法模式(十七)
- Java设计模式_(行为型)_模版方法模式
- 模版方法--行为型模式之二:类的关系
- 模版方法(Template Method)——类行为型模式
- (第Ⅳ部分 行为型模式篇) 第15章 模版方法模式(Template Method)
- (第Ⅳ部分 行为型模式篇) 第15章 模版方法模式(Template Method)
- (第Ⅳ部分 行为型模式篇) 第15章 模版方法模式(Template Method)
- 23种设计模式(13)_行为型_模版方法模式(Template Method)
- 行为类模式—模版方法模式(6)
- C# 实现卡布列克数
- 第8周项目1-建立顺序串的算法库
- 8bit全加器
- Spring Bean--自动装配
- 347_ aapt.exe错误
- 模版方法模式(行为型)
- C# NMODBUS
- Java对象之生
- sqlite数据库的基本使用
- 欢迎使用CSDN-markdown编辑器
- Ora_11g Rac R2 修改主机Hostname详解
- 欢迎使用CSDN-markdown编辑器
- 面试技巧性回答
- Hive报错[ERROR] Failed with exception java.io.IOException:java.lang.IllegalArgumentException