java
package com.jmj.pattern.template;
public abstract class AbstractClass {
//模板方法定义
public final void cookProcess(){
pourOil();
heatoil();
pourVegetable();
pourSauce();
fry();
}
public void pourOil(){
System.out.println("倒油");
}
public void heatoil(){
System.out.println("热油");
}
public void fry(){
System.out.println("炒炒到熟啊");
}
public abstract void pourVegetable();
public abstract void pourSauce();
}
java
package com.jmj.pattern.template;
public class ConcreteClass_BaoCai extends AbstractClass {
@Override
public void pourVegetable() {
System.out.println("下锅的蔬菜是包菜");
}
@Override
public void pourSauce() {
System.out.println("下锅的酱料是辣椒");
}
}
java
package com.jmj.pattern.template;
public class ConcreteClass_Caixin extends AbstractClass {
@Override
public void pourVegetable() {
System.out.println("下锅的蔬菜是菜心");
}
@Override
public void pourSauce() {
System.out.println("下锅的酱料是蒜蓉");
}
}
java
package com.jmj.pattern.template;
public class Client {
public static void main(String[] args) {
ConcreteClass_BaoCai concreteClass_baoCai = new ConcreteClass_BaoCai();
concreteClass_baoCai.cookProcess();
ConcreteClass_Caixin concreteClass_caixin = new ConcreteClass_Caixin();
concreteClass_caixin.cookProcess();
}
}