Template Method is a behavioral design pattern. Template Method design pattern is used to create a method stub and deferring some of the steps of implementation to the subclasses.
Template method defines the steps to execute an algorithm and it can provide default implementation that might be common for all or some of the subclasses. Let’s understand this pattern with an example, suppose we want to provide an algorithm to build a house. The steps need to be performed to build a house are - building foundation, building pillars, building walls and windows. The important point is that the we can’t change the order of execution because we can’t build windows before building the foundation. So in this case we can create a template method that will use different methods to build the house. Now building the foundation for a house is same for all type of houses, whether its a wooden house or a glass house. So we can provide base implementation for this, if subclasses want to override this method, they can but mostly it’s common for all the types of houses. To make sure that subclasses don’t override the template method, we should make it final.
Since we want some of the methods to be implemented by subclasses, we have to make our base class as abstract class. HouseTemplate.java
package com.journaldev.design.template;
public abstract class HouseTemplate {
//template method, final so subclasses can't override
public final void buildHouse(){
buildFoundation();
buildPillars();
buildWalls();
buildWindows();
System.out.println("House is built.");
}
//default implementation
private void buildWindows() {
System.out.println("Building Glass Windows");
}
//methods to be implemented by subclasses
public abstract void buildWalls();
public abstract void buildPillars();
private void buildFoundation() {
System.out.println("Building foundation with cement,iron rods and sand");
}
}
buildHouse()
is the template method and defines the order of execution for performing several steps.
We can have different type of houses, such as Wooden House and Glass House. WoodenHouse.java
package com.journaldev.design.template;
public class WoodenHouse extends HouseTemplate {
@Override
public void buildWalls() {
System.out.println("Building Wooden Walls");
}
@Override
public void buildPillars() {
System.out.println("Building Pillars with Wood coating");
}
}
We could have overridden other methods also, but for simplicity I am not doing that. GlassHouse.java
package com.journaldev.design.template;
public class GlassHouse extends HouseTemplate {
@Override
public void buildWalls() {
System.out.println("Building Glass Walls");
}
@Override
public void buildPillars() {
System.out.println("Building Pillars with glass coating");
}
}
Let’s test our template method pattern example with a test program. HousingClient.java
package com.journaldev.design.template;
public class HousingClient {
public static void main(String[] args) {
HouseTemplate houseType = new WoodenHouse();
//using template method
houseType.buildHouse();
System.out.println("************");
houseType = new GlassHouse();
houseType.buildHouse();
}
}
Notice that client is invoking the template method of base class and depending of implementation of different steps, it’s using some of the methods from base class and some of them from subclass. Output of the above program is:
Building foundation with cement,iron rods and sand
Building Pillars with Wood coating
Building Wooden Walls
Building Glass Windows
House is built.
************
Building foundation with cement,iron rods and sand
Building Pillars with glass coating
Building Glass Walls
Building Glass Windows
House is built.
Thats all for template method design pattern in java, I hope you liked it.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Facade is similar to Factory pattern. Template pattern is similar to Strategy/Composite pattern.
- Nakul
Using above example we can call houseType.buildwalls and houseType.buildPillar
- Mohsin
What if I want to use other class instance(let say DAO call or Service layer call) in default method buildWindows() as we can’t use @Autowire for that instance ?
- Sharath
Thank you bro
- Raheem
How should i know whether the first step is completed or not?
- Mahadev
Excellent thank you
- Mustafa
That’s a fantastic explanation.
- Larsen
explanation is superb.
- shobhit khandelwal
really helpful. Thanks a lot. :)
- Prosenjit
What if I do it like below…the purpose of template method is lost. HouseTemplate houseType = new WoodenHouse(); houseType .buildWindows(); houseType .buildWalls(); houseType .buildPillars(); How to restrict this.
- test