It is a behavioral design pattern. We can use it to create an outline
for an algorithm or a complex operation. We first create the skeleton
of a program. Then we delegate the steps of the operation to
subclasses. The subclasses can redefine the inner implementation of
each step.
E.g. While designing a Game in Java, we can implement it as an
algorithm with Template Method pattern. Each step in the game can
be deferred to subclasses responsible for handling that step.
Let say we implement Monopoly game in Java. We can create
methods like initializeGame(), makeMove(), endGame() etc. Each
of these methods can be handled in subclasses in an independent
manner.
We can use same algorithm for Chess game with same set of
abstract methods. The subclass for Chess game can provide the
concrete implementation of methods like initializeGame(),
makeMove(), endGame() etc.
Template Method pattern is very useful in providing customizable
class to users. We can create the core class with a high level
implementation. And our users can customize our core class in their
custom subclasses.
xxxxxxxxxx
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");
}
}
https://www.digitalocean.com/community/tutorials/template-method-design-pattern-in-java
xxxxxxxxxx
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* An example of Template method design pattern in Java.
*
* @author
*/
public class TemplatePatternDemo{
public static void main(String args[]) {
System.out.println("Making tea using template method pattern");
CaffeineBeverageTemplate template = new Tea();
template.prepareRecipe();
System.out.println("Making coffie using template method");
template = new Coffie();
template.prepareRecipe();
}
}
abstract class CaffeineBeverageTemplate {
public final void prepareRecipe() {
boilWater();
brew();
pourInCup();
if (isCondimentRequested()) {
addCondiments();
}
}
public void boilWater() {
System.out.println("Boiling water");
}
public abstract void brew();
public void pourInCup() {
System.out.println("Pouring in Cup");
}
public abstract boolean isCondimentRequested();
public abstract void addCondiments();
}
class Tea extends CaffeineBeverageTemplate {
@Override
public void brew() {
System.out.println("Brewing tea leaves");
}
@Override
public boolean isCondimentRequested() {
return false;
}
@Override
public void addCondiments() {
}
}
class Coffie extends CaffeineBeverageTemplate {
@Override
public void brew() {
System.out.println("Brewing coffie beans");
}
@Override
public boolean isCondimentRequested() {
return true;
}
@Override
public void addCondiments() {
System.out.println("Adding some cream");
}
}
Output
Making tea using template method pattern
Boiling water
Brewing tea leaves
Pouring in Cup
Making coffee using the template method
Boiling water
Brewing coffee beans
Pouring in Cup
Adding some cream
xxxxxxxxxx
// Java Template Method pattern
abstract class AbstractClass {
final void templateMethod() {
primitiveOperation1();
primitiveOperation2();
concreteOperation();
}
abstract void primitiveOperation1();
abstract void primitiveOperation2();
void concreteOperation() {
//implementation here
}
}