Template method design pattern is a behavioral design pattern. This pattern defines a template or skeleton of an operation, which is used by it’s sub classes with different implementation. Initially a minimum required operational structure/template of an algorithm is defined in main class. Sub classes can add or differ additional operations to the existing structure according to the requirement.

GoF Definition : Define the skeleton of an algorithm in an operation, deferring some steps to sub classes. The template method lets sub classes redefine certain steps of an algorithm without changing the algorithm’s structure.

Example Template Method Design Pattern in Java

Let’s consider a real world example of making dosa. The basic process of making different types of dosa will remain same, but there can be little addition of materials added to dosa according to the type of dosa we are going to prepare. For example : Butter dosa, Onion dosa, Masala dosa, etc.

To implement our dosa example with template method design pattern, we have to create a basic dosa template by creating an abstract Dosa class as shown below. This class contains basic operations required for preparing a plain normal dosa and an abstract addDosaIngradients() method to add additional ingredients according to the requirement.

Here, prepareDosa() is our template method, which contains the core operation to prepare dosa. Note that to prevent the template method overridden from subclasses, we have made it final.

Dosa.java

package com.asb.example;

public abstract class Dosa {

	public void prepareBatter() {
		
		System.out.println("Preparing Dosa Batter..");
	}
	
	public void spreadBatter() {
	
		System.out.println("Spread the Batter on Pan..");
	}
	
	public void heating() {
		
		System.out.println("Heating Dosa on Pan..");
		System.out.println("Dosa is Ready..!!!");
	}
	
	public abstract void addDosaIngradients();
	
	//Template Method
	public final void prepareDosa() {
		prepareBatter();
		spreadBatter();
		addDosaIngradients();
		heating();
	}
}

Our basic Dosa abstract class is ready. Now we need to create subclasses, which extends the basic Dosa abstract class and provides additional operation to prepare butter dosa and onion dosa. For this create two subclasses of Dosa class as shown below.

ButterDosa.java

package com.asb.example;

public class ButterDosa extends Dosa {

	@Override
	public void addDosaIngradients() {

		System.out.println("Adding Butter...");
	}
}

OnionDosa.java

package com.asb.example;

public class OnionDosa extends Dosa {

	@Override
	public void addDosaIngradients() {
		
		System.out.println("Adding Onion slices..");
	}
}

Finally, test the design pattern by creating a class TemplateMethodDesignPatternExample with main method as shown below. Here, we are Creating an onion dosa instance first, and prepare dosa by calling prepareDosa() template method. Next, we are creating a butter dosa object and preparing the butter dosa. Each time, corresponding addDosaIngradients() method will be invoked internally from prepareDosa() method.

package com.asb.example;

public class TemplateMethodDesignPatternExample {

	public static void main(String[] args) {
		
		System.out.println("**Preparing Dosa Example**\n");
		Dosa dosa = new OnionDosa();
		dosa.prepareDosa();
		
		System.out.println();
		dosa = new ButterDosa();
		dosa.prepareDosa();
		System.out.println("\n**Serve Prepared Dosa**");
	}
}

By running the above class, we get the following output. The main advantage of template method design pattern is re-usability of the code.

Output

Template Method Design Pattern in Java

You may also interested in