深入理解设计模式-模板方法模式


一、什么是模板方法模式

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


二、样例分析

在一个店里面,有2种饮料,它们的冲泡步骤是这样的:
1,咖啡:把水煮沸,用沸水冲泡咖啡,倒进杯子,加糖和牛奶
2,茶:把水煮沸,用沸水浸泡茶叶,倒进杯子,加柠檬
采用模板方法模式实现

在这里插入图片描述
在这里插入图片描述

采用模板方法实现:

public abstract class CaffeineBeverage {
    
    

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

    abstract void brew();

    abstract void addCondiments();

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

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


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 class Tea extends CaffeineBeverage {
    
    
    @Override
    public void brew() {
    
    
        System.out.println("Steeping the tea");
    }

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

采用模板方法加钩子实现:

钩子是一种被声明在抽象类中的方法,但只有空的或者默认的实现。钩子的存在,可以让子类有能力对算法的不同点进行挂钩。要不要挂钩,由子类决定。

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

    abstract void brew();

    abstract void addCondiments();

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

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

    boolean customerWantsCondiments() {
    
    
        return true;
    }
}


public class CoffeeWithHook extends CaffeineBeverageWithHook {
    
    

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

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

    @Override
    public 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 ioe) {
    
    
            System.err.println("IO error trying to read your answer");
        }
        if (answer == null) {
    
    
            return "no";
        }
        return answer;
    }
}


public class TeaWithHook extends CaffeineBeverageWithHook {
    
    

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

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

    @Override
    public boolean customerWantsCondiments() {
    
    

        String answer = getUserInput();

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

    private String getUserInput() {
    
    
        // get the user's response
        String answer = null;

        System.out.print("Would you like lemon with your tea (y/n)? ");

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
    
    
            answer = in.readLine();
        } catch (IOException ioe) {
    
    
            System.err.println("IO error trying to read your answer");
        }
        if (answer == null) {
    
    
            return "no";
        }
        return answer;
    }
}


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();


        TeaWithHook teaHook = new TeaWithHook();
        CoffeeWithHook coffeeHook = new CoffeeWithHook();

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

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

三、优缺点

优点:

  • 封装不变部分,扩展可变部分。
  • 提取公共代码,便于维护。
  • 行为由父类控制,子类实现

缺点:

  • 每一个不同的实现都需要一个子类来实现,导致类的个数增加,使得系统更加庞大

四、使用场景

  • 算法的整体步骤很固定,但其中个别部分易变时,这时候可以使用模板方法模式,将容易变的部分抽象出来,供子类实现。

结尾

  • 感谢大家的耐心阅读,如有建议请私信或评论留言。
  • 如有收获,劳烦支持,关注、点赞、评论、收藏均可,博主会经常更新,与大家共同进步

猜你喜欢

转载自blog.csdn.net/qq359605040/article/details/130400050