Using Template Method Design Pattern In Java
Today, I will discuss another very useful design pattern named the Template Method Design Pattern. Read on to find out more!
Join the DZone community and get the full member experience.Join For Free
Today, I will discuss another very useful design pattern named the Template Method Design Pattern.
Template Method Design Pattern
- The Template Method pattern is one of the behavioral design patterns identified by Gamma et al. in the book Design Patterns.
- The Template Method pattern provides a method in a super-class, usually an abstract super-class, and defines the skeleton of an operation in terms of several high-level steps.
- Generally, these steps are implemented by additional helper methods in the same class as the template method.
- The helper methods may be either created as an abstract method, for which sub-classes are required to provide concrete implementations, or hook methods, which have empty bodies in the super-class.
- The Template Method design pattern is used to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes.
- In this way of implementation, the overall structure and sequence of the algorithm are preserved by the parent class.
- The Template Method pattern defines the sequential steps to execute a multi-step algorithm. We can provide a default implementation as well.
- In the Template Method pattern, we define a preset structure method called template method which consists of steps.
- These steps can be created as an abstract method which will be implemented by its sub-classes.
- In the Template Method pattern, an abstract class exposes defined way(s)/template(s) to execute its methods.
- The template method uses and defines the sequence of steps to perform the algorithm.
Let's take an example to understand it better.
Car Manufacturing Example Using Template Method Design Pattern
I am using the same example I used for explaining the Builder Design Pattern to help you understand the difference between both.
First, we define an abstract class to represent a car manufacturing template. Here's the code for CarTemplate class:
Here please notice that manufactureCar() method is a template method here. And methods fixChassis(), fixBody(), paint() and fixInterior() are the steps which will be defined by different sub-classes. The template method uses and defines the sequence of steps to perform the algorithm.
Now we will implement concrete sub-classes to define different flavors of templates.
Here's the code for ClassicCar class:
Here's the code for ModernCar class:
Here's the code for SportsCar class:
Now we will write our Main class to execute and test the output:
Below is the output:
Well, there you have it! I hope this tutorial helped to understand the Template Method pattern.
Source Code can be found here: Template-Method-Design-Pattern-Source-Code
Liked the article? Please don't forget to press that like button. Happy coding!
Opinions expressed by DZone contributors are their own.