类图:
实现:
AbstractTemplate.java
package com.template;
public abstract class AbstractTemplate {
public abstract void setup();
public abstract void execute();
public abstract void shutdown();
public void run() {
setup();
try{
execute();
} finally {
shutdown();
}
}
}
TemplateA.java
package com.template;
public class TemplateA extends AbstractTemplate{
@Override
public void setup() {
System.out.println("setup A");
}
@Override
public void execute() {
System.out.println("execute A");
}
@Override
public void shutdown() {
System.out.println("shutdown A");
}
}
Main.java
package com.template;
public class Main {
public static void main(String args[]) {
AbstractTemplate templateA = new TemplateA();
templateA.run();
}
}
输出:
setup A
execute A
shutdown A