1、提高代码的重用性
package TemplateTest;
public abstract class TemplateSource {
public abstract void compataOperation1();
public abstract void compataOperation2();
public void template() {
System.out.println("start");
compataOperation1();
compataOperation2();
System.out.println("end");
}
}
package TemplateTest;
public class TemplateA extends TemplateSource {
@Override
public void compataOperation1() {
System.out.println("operation A mothod");
}
@Override
public void compataOperation2() {
System.out.println("operation A 2 mothod");
}
}
package TemplateTest;
public class TemplateB extends TemplateSource {
@Override
public void compataOperation1() {
System.out.println("B1");
}
@Override
public void compataOperation2() {
System.out.println("B2");
}
}
package TemplateTest;
public class Test {
public static void main(String[] args) {
TemplateSource a = new TemplateA();
TemplateSource b = new TemplateB();
a.template();
b.template();
}
}