美文网首页
模版方法模式

模版方法模式

作者: 阳光的技术小栈 | 来源:发表于2018-01-22 15:13 被阅读16次

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

示例—咖啡与茶

咖啡店冲咖啡与泡茶的方法分别是:

冲咖啡:
(1). 把水煮沸
(2). 用沸水冲泡咖啡
(3). 把咖啡倒进杯子
(4). 加糖和牛奶

泡茶
(1). 把水煮沸
(2). 用沸水浸泡茶叶
(3). 把茶倒进杯子
(4). 加柠檬

合并重复代码,并抽象出基类。

UML图表示

模版方法模式-咖啡与茶

代码演示

咖啡因饮料基类

package Template;

public abstract class CaffeineBeverage {

    final void prepareRecipe (){
        boilWater();
        brew();
        pourInCup();
        if (customerWantsCondiments()){
            addCondiments();
        }
    }

    abstract void brew();

    abstract void addCondiments();

    final void boilWater() {
        System.out.println("Boiling water");
    }

    final void pourInCup() {
        System.out.println("Pouring into cup");
    }

    /**
     * 钩子方法,子类覆盖这个方法,但不见得一定要这么做
     * @return
     */
    boolean customerWantsCondiments(){
        return true;
    }

}

咖啡

package Template;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Coffee extends CaffeineBeverage{

    @Override
    void brew() {
        System.out.println("Dripping Coffee through filter");
    }

    @Override
    void addCondiments() {
        System.out.println("Adding Sugar and Milk");
    }

    @Override
    boolean customerWantsCondiments() {
        String answer = getUserInput();

        if (answer.toLowerCase().startsWith("y")) {
            return true;
        }
        else {
            return false;
        }
    }

    private String getUserInput(){
        String answer = null;

        System.out.print("Would you like milk and sugar with your coffee (y/n)? ");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        try {
            answer = in.readLine();
        } catch (IOException e) {
            System.err.println("IO error trying to read your answer");
        }

        if (answer == null) {
            return "no";
        }

        return answer;
    }
}

package Template;

public class Tea extends CaffeineBeverage {
    @Override
    void brew() {
        System.out.println("Steeping the tea");
    }

    @Override
    void addCondiments() {
        System.out.println("Adding Lemon");
    }
}

测试代码

package Template;

public class BeverageTestDrive {
    public static void main(String[] args) {

        Tea tea = new Tea();
        Coffee coffee = new Coffee();

        System.out.println("\nMaking tea...");
        tea.prepareRecipe();

        System.out.println("\nMaking coffee...");
        coffee.prepareRecipe();
    }
}

测试结果

Making tea...
Boiling water
Steeping the tea
Pouring into cup
Adding Lemon

Making coffee...
Boiling water
Dripping Coffee through filter
Pouring into cup
Would you like milk and sugar with your coffee (y/n)? y
Adding Sugar and Milk

相关文章

  • 设计模式-模版方法模式

    设计模式-模版方法模式 定义 模版方法模式(Template Method Pattern)又叫模版模式,是指定义...

  • 设计模式[14]-模版方法模式-Template Method

    1.模版方法模式简介 模版方法模式(Template Method Pattern)是行为型(Behavioral...

  • 设计模式之模版方法模式

    模版方法模式 模版方法是一种只需使用继承就可以实现的非常简单的模式模版方法模式由两部分结构组成,第一部分是抽象父类...

  • 设计模式之模版方法模式

    模版方法模式 模版方法是一种只需使用继承就可以实现的非常简单的模式模版方法模式由两部分结构组成,第一部分是抽象父类...

  • 设计模式之Template模式(模版模式)

    1 模式简介 1.1 模版方法模式的定义:模版方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模...

  • 模版方法模式

    通俗讲,模版模式就是将通用的上升到父类中,个性化的功能由各个子类完成.代码的复用是模版模式主要解决的.

  • 模版方法模式

    模版方法模式 定义:定义一个操作中算法的框架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算...

  • 模版方法模式

  • 模版方法模式

  • 模版方法模式

    模版方法模式(定义):通过把相同的方法移动到父类,去除子类中的重复代码,提供一个很好的代码复用平台

网友评论

      本文标题:模版方法模式

      本文链接:https://www.haomeiwen.com/subject/qwsywxtx.html