Template Method Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

This article deals with yet another behavioural pattern. We are going to see Template Method Implementation.

Template Method Implementation:

Template method design pattern is defined as follows.

Template method design pattern 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 algorithm’s structure.

Template method implementation helps us define a specific structure of a process which must be followed for certain task to be accomplished, however, there could be some behaviour which is specific to subclasses. To implement specific behaviour, subclasses need to have some way to define it. Template method implementation keeps changing behaviour abstract by defining abstract methods which can have specific implementation in subclasses. Generic diagram for template method design pattern as below

Template Method Design Pattern

Template Method Design Pattern

Component involved

  • AbstractClass : contains templateMethod(), which defines skeleton for algorithm. It can call certain concrete methods as there could be common behaviour for any algorithm. It calls abstract methods  operationOne() and operationTwo()which will be implemented in subclasses.
  • ConcreteClass: Extends AbstractClass and implements operationOne() and operationTwo()

Template Method Implementation – Example:

  • We have Vehicle class, a vehicle can be driven by following certain steps.
  • Driving a vehicle contains four steps i.e. start(), engageClutch(), shiftGear() and stop()
  • Bike and Car both extend Vehicle and start() and stop() behaviour is same for both. i.e. It is common behaviour for both. It can be generalised and put in start() and stop() methods of Vehicle class.
  • engageClutch() and shiftGear() behaviour for Bike and Car is different, so it needs to be implemented in their own classes.

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout template-method

If you don’t have git installed, you can either directly download zip or copy paste from below.

Vehicle.java

Bike.java

Car.java

Thats it, we now have template method implementation. Let us test it out.

Output is

 

Class diagram for example as below

Template Method Implementation Example

Template Method Implementation Example

 

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *