This tutorial explains the Gang of Four Design Pattern named Template Method Pattern. It first defines the pattern, then via pattern’s UML Class Diagram shows the various constituent classes in the pattern and explains the role of each of the classes. Next an example use case using Template Method Pattern is presented via its Class Diagram and Java code. Finally, the use case’s class diagram and working of the Java code is explained in detail.
Introduction: Template Method Design Pattern is a behavioral design pattern among the Gang Of Four(GOF)Article on GOF Patterns & their types Design Patterns. I.e. the template method pattern deals with how the classes interact with each other.
What is Template Method Design Pattern
Template Method design pattern is used when the skeleton of an algorithm can be defined in terms of a series of operations, with few of these operations being redefined by sub-classes without changing the algorithm.
Lets say we have few steps to achieve something – say cooking an item. Few of the steps are common such as putting the gas stove on before cooking, putting the stove off after cooking etc. But some steps will be specific to that food item- for example some food will be boiled first then cooked, while some food will be roasted etc.
If we make a template(or, base class in programming terms) of an algorithm for cooking food, then we can implement the common methods defined earlier in the base class itself. At the same time we will leave the steps which are specific to the food item as abstract. These abstract methods will be implemented based on the recipe specific to the food item.
Now lets examine the basic structure of a template method pattern through its class diagram –
Template Method Design Pattern Class Diagram
Explanation of Class Diagram
AbtractBaseClassclass sews the whole algorithm together.
templateMethod()is a concrete method which is responsible for calling all the 4 steps of the algorithm in correct order which is
- Two of the steps in the algorithm are concrete named
concreteStep2(). These concreteMethods have their implementation in the base class which is shared by all the
ConcreteImplementationclasses when they subclass the
- Two steps in the algorithm are abstract named
abstractStep2(). These abstract methods will be implemented by the specific
ConcreteImplementationbased on the needs of the concrete variant.
Note – You can have any number of concrete & abstract methods. I have taken 2 of each just for example.
Example Use Case Implementation of Template Method Pattern in Java
The Java Class Diagram is for a use case of an algorithm of cooking recipes for vegetables. Let us understand the Java code
AbstractRecipeis the abstract base class in above use case.
- 2 concrete methods in the
putStoveOff(). These contain implementation for putting the gas stove on and off. This implementation is independent of which vegetable is being cooked and hence is implemented in the base class and is inherited as-is by the child classes.
cookFood()which calls all the 4 methods
- There are 2 concrete implementation classes called
- The concrete implementation classes implement their own versions of
cookSpecificVegetable()based on the specific recipe needs.
This concludes the article on template method design pattern where we looked at what is the pattern, pattern class diagram and its explanation and finally an example of the pattern in java with class diagram and sample code.