Java — Template Method Design Pattern Example using Java

Prabhu Kumar
3 min readNov 30, 2020

--

Template Method Design Pattern defines skeleton of algorithms in a method deferring some steps to subclasses. And allows subclasses to redefine some steps without changing the algorithm.

Structure Summary

  • Analyze the target algorithm to see if you can be break it into multiple steps. Consider which steps are common to all subclasses and which ones are unique
  • Define the steps common to all subclasses in the base class.
  • For the steps that are unique across the subclasses, define a placeholder/hook method signatures in the base class and allocate each possible implementation to its own derived class.

The participant classes in the template method pattern are

AbstractClass

The AbstractClass contains the templateMethod that defines the skeleton of an algorithm, which should be made final so that it cannot be overridden

ConcreteClass

The ConcreteClass implements all the operations required by the templateMethod that were defined as abstract in the parent class

UML Diagram

Implementation steps

Step 1 — Create the abstract class that defines the template method

Step 2 — Create the concrete subclasses and implement the placeholder/hook methods defined in the abstract class

Step 3 — Create the client (not shown in the above diagram)

Code Example

Step 1:

public abstract class LemonJuice {public final void prepare(){extract();addSugar();addComponent();garnish();}public void extract(){System.out.println(“Extract juice from lemon”);}public void addSugar(){System.out.println(“Add sugar to juice”);}public abstract void addComponent();public abstract void garnish();}

Here the abstract methods addComponent and garnish have been provided in the template method prepare as hooks for which implementation has been provided in the subclasses of the LemonJuice class

Step 2:

public class SodaLemonJuice extends LemonJuice {@Overridepublic void addComponent() {System.out.println(“add soda to lemon juice”);}@Overridepublic void garnish() {System.out.println(“garnish with mint”);}}public class WaterLemonJuice extends LemonJuice {@Overridepublic void addComponent() {System.out.println(“ add water to lemon juice”);}@Overridepublic void garnish() {System.out.println(“ garnish with ginger”);}}

Here we have created subclasses namely “SodaLemonJuice” and “WaterLemonJuice” which implements “LemonJuice” abstract class and provided body to “addComponent” and “garnish” methods.

Step 3:

public class Test {public static void main(String[] args) {LemonJuice juice = new SodaLemonJuice();juice.prepare();}}

Here we have instantiated an object of “SodaLemonJuice” subclass and invoked the template method “prepare”. The “addComponent” and “garnish” methods are called on the object of class “SodaLemonJuice” where as the “extract” and “addSugar” methods are called from the abstract superclass “LemonJuice” in the inheritance hierarchy as these methods are not defined in the subclasses.

Verify the output

Extract juice from lemonAdd sugar to juiceadd soda to lemon juicegarnish with mint

Summary

The Template Method pattern is a behavioral design pattern.

Use the Template Method pattern when you want to let clients extend only particular steps (addComponent and garnish) of an algorithm, but not the whole algorithm.

A hook is a method that is declared in the abstract class, but doesn’t provide any implementation like addComponent and garnish in our example.

The prepare template method determines when to call these hooks, in otherwords the high-level components call the low-level components and this is based on the Hollywood Principle “Don’t Call Us, We’ll Call You!”

I hope this explanation and Java Template Method Pattern code example have been helpful. If you like this tutorial please give a clap.

--

--

Prabhu Kumar
Prabhu Kumar

No responses yet