Template Method defines a framework for operation of the algorithm, some steps to subclasses delay. Subclasses may not change such a configuration algorithm to certain steps of the algorithm to redefine
The main role of a template method
Template Method pattern using Java inheritance mechanism, which, AbstractClass called the abstract template , its methods are divided into two categories:
- The basic methods: by subclass to achieve, and is called template method
- Template Methods: The basic scheduling method implemented, in fixed logic
ConcreteClass1 and belong ConcreteClass2 specific template , the basic method as defined in the parent class implementation
2, to achieve a template method
//抽象模板类
public abstract class AbstractClass {
//基本方法
protected abstract void doSomething();
//基本方法
protected abstract void doAnything();
public void templateMethod() {
//调用基本方法,完成相关逻辑
this.doAnything();
this.doSomething();
}
}
//具体模板类
public class ConcreteClass1 extends AbstractClass {
@Override
protected void doSomething() {
//业务逻辑
}
@Override
protected void doAnything() {
//业务逻辑
}
}
//具体模板类
public class ConcreteClass2 extends AbstractClass {
@Override
protected void doSomething() {
//业务逻辑
}
@Override
protected void doAnything() {
//业务逻辑
}
}
public class Client {
public static void main(String[] args) {
AbstractClass class1 = new ConcreteClass1();
AbstractClass class2 = new ConcreteClass2();
//调用模板方法
class1.templateMethod();
class2.templateMethod();
}
}
3, extended template method
Business logic is as follows: H1 Hummer models can control whether it loud horn
//抽象模板类
public abstract class HummerMode {
protected abstract void start();
protected abstract void stop();
protected abstract void alarm();
protected abstract void engineBoom();
final public void run() {
//发动汽车
this.start();
//引擎开始轰鸣
this.engineBoom();
//汽车喇叭响
if (isAlarm()) {
this.alarm();
}
//到达目的地停车
this.stop();
}
//钩子方法,默认喇叭是会响的
protected boolean isAlarm() {
return true;
}
}
//具体模板类
public class HummerH1 extends HummerMode {
private boolean alarmFlag = true;//要响喇叭
public void setAlarm(boolean isAlarm) {
this.alarmFlag = isAlarm;
}
//重写钩子方法,通过子类方法返回值决定公共部分的执行结果
@Override
protected boolean isAlarm() {
return this.alarmFlag;
}
@Override
protected void start() {
System.out.println("悍马H1发送...");
}
@Override
protected void stop() {
System.out.println("悍马H1停车...");
}
@Override
protected void alarm() {
System.out.println("悍马H1鸣笛...");
}
@Override
protected void engineBoom() {
System.out.println("悍马H1引擎发动...");
}
}
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("H1型号悍马");
System.out.println("H1型号悍马是否需要喇叭声响? 0-不需要 1-需要");
String type = new BufferedReader(new InputStreamReader(System.in)).readLine();
HummerH1 h1 = new HummerH1();
if (type.equals("0")) h1.setAlarm(false);
h1.run();
}
}
0 input operation result as follows:
H1型号悍马
H1型号悍马是否需要喇叭声响? 0-不需要 1-需要
0
悍马H1发送...
悍马H1引擎发动...
悍马H1停车...
The result of running the input 1 as follows:
H1型号悍马
H1型号悍马是否需要喇叭声响? 0-不需要 1-需要
1
悍马H1发送...
悍马H1引擎发动...
悍马H1鸣笛...
悍马H1停车...