感想
模板模式:封装了一个算法步骤,并允许子类为一个或多个步骤方法提供实现。模板模式可以使子类在不改变算法结构的情况下,重新定义算法中的某些步骤。
以下例子引用了韧雪飞舞的例子:设计模式之 - 模板模式(Template Pattern)
a. 先来写一个抽象的做菜父类:
1 | public abstract class DodishTemplate { |
b. 下来做两个番茄炒蛋(EggsWithTomato)和红烧肉(Bouilli)实现父类中的抽象方法:
1 | /** |
1 | /** |
c. 在测试类中我们来做菜:1
2
3
4
5
6
7
8
9
10
11public class App {
public static void main(String[] args) {
DodishTemplate eggsWithTomato = new EggsWithTomato();
eggsWithTomato.dodish();
System.out.println("-----------------------------");
DodishTemplate bouilli = new Bouilli();
bouilli.dodish();
}
}