设计模式——模板方法

   想象一下这样场景,一条工作流水线上,要对产品进行多道加工,有些部分有机器来加工,而有些是用人来加工,但是不同人根据自己的特点来做。这就是模板方法的模型。父类定义了模板方法,但是某些步骤由子类来完成。
   在来看下定义:模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
   "挂钩",某些步骤可以做也可以不做,因此可以加入if判断来决定。

(https://img-blog.csdn.net/20150309095553316)

   这里有一个原则:别调用(打电话给)我们,我们会调用(打电话给)你。
   意思是允许底层组件将自己挂钩到系统上,但是高层组件会决定什么时候和怎样使用这些底层组件。
   但是现实中模板方法会发生遍历,比如对象排序Array.sort(Comparable),被排序对象需要实现Comparable,并实现compareTo()方法。

代码:

父类
public abstract class CaffeineBeverage {

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

    public abstract void brew();

    public abstract void addCondiments();

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

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

    public boolean customerWantsCondiments(){
        return true;
    }
}

具体子类子类
public class Coffee extends CaffeineBeverage{

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

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

    public boolean customerWantsCondiments(){
        String answer = getUserInput();

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

    private String getUserInput(){
        String answer = null;
        System.out.println("Would you like milk and sugar with you coffee(y/n)");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            answer = in.readLine();
        } catch (Exception e) {
            // TODO: handle exception
        }
        if(answer == null){
            return "no";
        }
        return answer;
    }

}
public class Tea extends CaffeineBeverage{

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

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

客户端

public class BeverageTestDrive {

    public static void main(String[] args) {
        Coffee coffeeWithHook = new Coffee();
        coffeeWithHook.prepareRecipe();
    }
}

排序

public class Duck implements Comparable{

    String name;
    int weight;

    public Duck(String name,int weight){
        this.name = name;
        this.weight = weight;
    }

    public String toString(){
        return name + " weights" + weight;
    }

    @Override
    public int compareTo(Object object) {
        Duck otherDuck = (Duck)object;
        if(this.weight < otherDuck.weight){
            return -1;
        }else if(this.weight == otherDuck.weight){
            return 0;
        }else{
            return 1;
        }
    }


}


客户端
public class DuckSortTestDrive {

    public static void main(String[] args) {
        Duck[] ducks = {
                new Duck("Daffy",8),
                new Duck("Dewey",2),
                new Duck("Howard",7),
                new Duck("Louie",2)
        };
        System.out.println("Before sorting:");
        display(ducks);

        Arrays.sort(ducks);

        System.out.println("After sorting:");
        display(ducks);
    }

    public static void display(Duck[] ducks){
        for(int i = 0; i < ducks.length;i++){
            System.out.print(ducks[i] + " ");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值