Template Method Pattern
Template Method Pattern
It defines a workflow to finish specific operations/steps which have order.
It allows subclasses to change step(s) detail without modifying the workflow’s structure.
Optionally it can provide default implementation base on your requirements.
For above diagram,
XXTemplate
is an abstract classtemplateMethod
is a final method which consist of operation steps,final
makes it cannot be overrid by subclassesstep1()
andstep2()
are abstract methodsstep3()
is default implementations(optionally)- template method of superclass call method of subclasses, this is known as Hollywood Principle – “don’t call us, we’ll call you.”.
Example
public abstract class OrderProcessTemplate {
public boolean isGift;
public abstract void doSelect();
public abstract void doPayment();
public abstract void doDelivery();
public final void wrapGift() {
System.out.println("Gift wrap successfull");
}
public final void processOrder(boolean isGift) {
doSelect();
doPayment();
if( isGift ) {
wrapGift();
}
doDelivery();
}
}
public class OnlineOrder extends OrderProcessTemplate {
@Override
public void doSelect() {
System.out.println("OnlineOrder.doSelect");
}
@Override
public void doPayment() {
System.out.println("OnlineOrder.doPayment");
}
@Override
public void doDelivery() {
System.out.println("OnlineOrder.doDelivery");
}
}
public class OnSiteOrder extends OrderProcessTemplate {
@Override
public void doSelect() {
System.out.println("OnSiteOrder.doSelect");
}
@Override
public void doPayment() {
System.out.println("OnSiteOrder.doPayment");
}
@Override
public void doDelivery() {
System.out.println("OnSiteOrder.doDelivery");
}
}
public class Test {
public static void main(String[] args) {
OrderProcessTemplate order = new OnlineOrder();
order.processOrder(false);
order = new OnSiteOrder();
order.processOrder(true);
}
}
reference template-method-design-pattern
Written on July 30, 2018