The blog for Design Patterns, Linux, HA and Myself!
In this tutorial we’ll be looking into the Template Design Pattern. We’ll try to create a Report Generator that will generate Tax Report and Expense Report. While doing so, we’ll be encountering some design issues that we’ll solve using the Template Method Design Pattern.
It has been divided into two stages:
In this section we’ll be creating two classes. One for Tax Report Generation and another for Expense Report Generation.
The TaxReport class. It has a method, generate
, that’ll do the following tasks:
Note: Make sure to go through the code comments as well. It’ll help you understand the concept better.
class TaxReport {
public void generate() {
createDatabaseConnection();
executeMySQLQuery();
convertToPDF();
}
public void createDatabaseConnection() {
System.out.println("Creating Database Connection...");
}
public void executeMySQLQuery() {
System.out.println("Executing MySQL Query...");
}
public void convertToPDF() {
System.out.println("Converting To PDF...");
}
}
The ExpenseReport class. It has a generate method that’ll do the following tasks:
class ExpenseReport {
public void generate() {
createDatabaseConnection();
executePostgresQuery();
convertToXLS();
}
public void createDatabaseConnection() {
System.out.println("Creating Database Connection...");
}
public void executePostgresQuery() {
System.out.println("Executing Postgres Query...");
}
public void convertToXLS() {
System.out.println("Converting To XLS...");
}
}
After performing a quick code review we’ve got following points to ponder:
createDatabaseConnection
methods in both the classes share the same responsibility, i.e., to create a database
connection.convertToPDF
and convertToXLS
. However, their implementations are different but their
intention is same, to convert to a format.It seems like there is code duplication(or behaviour duplication?). The first thing that comes into the mind is to remove it by creating an abstract class that will hold the common attributes.
So, this is what we’ve created after the review, a new ReportGenerator
class
abstract class ReportGenerator {
final public void generate() {
createDatabaseConnection();
executeQuery();
convert();
}
public void createDatabaseConnection() {
System.out.println("Creating Database Connection...");
}
abstract public void executeQuery();
abstract public void convert();
}
Two methods, executeQuery
and convert
, are abstract and the concrete classes must provide the implementation.
But there’s an implementation for createDatabaseConnection
because this property will remain same for all the concrete
implementations, although, it can be overridden by them.
And following are the implementations for:
class TaxReport extends ReportGenerator {
public void executeQuery() {
System.out.println("Executing MySQL Query...");
}
public void convert() {
System.out.println("Converting To PDF...");
}
}
class ExpenseReport extends ReportGenerator {
public void createDatabaseConnection() {
System.out.println("Creating Database Connection...");
}
public void executeQuery() {
System.out.println("Executing Postgres Query...");
}
public void convert() {
System.out.println("Converting To XLS...");
}
}
Here, we’ve generalized and abstracted the createConnection
method as it remains same for both the classes.
Also, we’ve templatized the way a report is generated, i.e., combining the task:
into the method generate()
.
We’ve delegated the task of executing the Query and converting the response to desired format.
This way the algorithm of creating a report remains same, the generate
method, but the subclasses can redefine the
steps but it cannot updated the structure of the algorithm.
Finally the definition from the Wikipedia
In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book Design Patterns. The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps.
I’ve created these tutorials after learning Design Patterns from this book Head First Design Patterns (A Brain Friendly Guide).