Template Design Pattern
Question : - What
is Template Design Pattern?
Answer : - This
design pattern also falls under Behavioral design pattern. The
Template design pattern defines the common steps of an algorithm and
leave the details to be implemented by the inherited classes. This
design pattern is very popular in framework development. The Template
Design Pattern helps in maximizing the code reusability.
As
per GOF:-
“Defines the skeleton of an algorithm in a method, deferring some
steps to subclasses. Template Method lets subclasses redefine certain
steps of an algorithm without changing the algorithms structure.”
Question :- How
Template pattern works?
Answer
: -
- Define an abstract base class with a qualified method which contains common implementation logic,
- Declare the abstract methods for subclasses to override their particular behaviours
- Declare a Template method in superclass that holds the common algorithm implementation steps.
- Derived classes can override implemented methods according to their requirements.
The
Template pattern is achieved through abstract classes. The Template
method design pattern works on a certain principle which tells Don't
call us, we'll call you.
The template method in the parent class controls the overall process.
Question
: - When to use Template design pattern?
Answer:-
- When behaviour of an algorithm can vary in parent and child classes, we should leave subclasses to implement the behaviour through overriding.
- If we want to avoid code duplication.
Example
of Template design pattern in JDK :-
a)
Template design pattern in Java :-
- All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
- All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
- javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.
- org.springframework.jdbc.core.JdbcTemplate class has all the common repeated code blocks related with JDBC workflow, such as update, query, execute,
- Similarly, we have HibernateTemplate and others
b)
Real time example of Template design pattern:-
- We are creating web project using Eclipse, then it is providing a sample structure for a web project and few required files like web.xml.
- Code generators
- XML parsers.
- In MS-Office, when we will try to create a spreedsheet using MS-excel or any presentation using MS-Powerpoint then we can see the template are available for specific workflow.
- Validation in business components
We will see the example where we will use an order template for normal and bulk order.
OrderTemplate.java
package com.gaurav.designpattern.template;
/**
* @author Gaurav
*
*/
public abstract class OrderTemplate {
public boolean isGift;
public abstract boolean getSelectedItem();
public abstract boolean getPayment(double itemCost, double grossTotal);
public final void getGiftPack() {
System.out.println("Selected item is packed as Gift.");
}
public abstract boolean delivery();
public final boolean orderProcess(double itemCost, double grossTotal) {
boolean orderProcessFlag = false;
orderProcessFlag = getSelectedItem();
if (orderProcessFlag) {
getPayment(itemCost, grossTotal);
}
double giftAmount = grossTotal - itemCost;
if (giftAmount >= 30.00)
isGift = true;
else
isGift = false;
boolean isDelivered = false;
if (orderProcessFlag && isGift) {
getGiftPack();
isDelivered = delivery();
} else {
isDelivered = delivery();
}
return isDelivered;
}
}
Order.java
package com.gaurav.designpattern.template;
/**
* @author Gaurav
*
*/
public class Order extends OrderTemplate{
@Override
public boolean getSelectedItem() {
boolean indicator = false;
System.out.println("product are added to shopping cart");
System.out.println("Delivery address is provided by customer.");
indicator = true;
return indicator;
}
@Override
public boolean getPayment(double itemCost, double grossTotal) {
boolean iPaymentSuccessful = false;
System.out.println("Payment is provided by the customer");
iPaymentSuccessful = true;
return iPaymentSuccessful;
}
@Override
public boolean delivery() {
boolean isDelivered = false;
System.out.println("Item got delivered to the customer.");
isDelivered = true;
return isDelivered;
}
}
BulkOrder.java
package com.gaurav.designpattern.template;
/**
* @author Gaurav
*
*/
public class BulkOrder extends OrderTemplate{
@Override
public boolean getSelectedItem() {
boolean indicator = false;
System.out.println("Multiple products are added to shopping cart");
System.out.println("Delivery address which is warehouse is provided by the retailer.");
indicator = true;
return indicator;
}
@Override
public boolean getPayment(double itemCost, double grossTotal) {
boolean iPaymentSuccessful = false;
System.out.println("This order is for cash on delivery, payment is not given by the retailer.");
iPaymentSuccessful = true;
return iPaymentSuccessful;
}
@Override
public boolean delivery() {
boolean isDelivered = false;
System.out.println("Item got delivered to the retailer.Payment is received.");
isDelivered = true;
return isDelivered;
}
}
TemplateDesignPatternDemo.java
package com.gaurav.designpattern.template;
/**
* @author Gaurav
*
*/
public class TemplateDesignPatternDemo {
public static void main(String... args) {
double itemCost = 5000;
double grossTotal = 5030;
System.out.println("##### This is the example for normal Order using generic OrderTemplate. #####");
OrderTemplate order = new Order();
order.orderProcess(itemCost, grossTotal);
System.out.println("\n##### This is the example for bulk Order using generic OrderTemplate. #####");
itemCost = 200000.00;
grossTotal = 200000.00;
OrderTemplate bulkOrder = new BulkOrder();
bulkOrder.orderProcess(itemCost, grossTotal);
}
}
Result:-
##### This is the example for normal Order using generic OrderTemplate. #####
product are added to shopping cart
Delivery address is provided by customer.
Payment is provided by the customer
Selected item is packed as Gift.
Item got delivered to the customer.
##### This is the example for bulk Order using generic OrderTemplate. #####
Multiple products are added to shopping cart
Delivery address which is warehouse is provided by the retailer.
This order is for cash on delivery, payment is not given by the retailer.
Item got delivered to the retailer.Payment is received.