TEMPLATE METHOD
DESIGN PATTERN
-SWAPNIL SHAH
WHAT IS A DESIGN PATTERN…
A design pattern is a general reusable solution to
a commonly occurring problem within a given
context in software design.
A design pattern is not a finished design that can
be transformed directly into source or machine
code but a description or template for how to
solve a problem that can be used in many
different situations
DIFFERENT PATTERNS BY TYPE
CREATIONAL
STRUCTURAL
BEHAVIORAL
ABSTRACT
FACTORY
ADAPTER
CHAIN OF
RESPONSIBILITY
BUILDER
BRIDGE
COMMAND
FACTORY METHOD
COMPOSITE
INTERPRETER
PROTOTYPE
DECORATOR
ITERATOR
SINGELTON
FAÇADE
MEDIATOR
FLYWEIGHT
MEMENTO
PROXY
OBSERVER
STATE
STRATEGY
TEMPLATE
METHOD
VISITOR
DIFFERENT PATTERNS BY TYPE
CREATIONAL
STRUCTURAL
BEHAVIORAL
ABSTRACT
FACTORY
ADAPTER
CHAIN OF
RESPONSIBILITY
BUILDER
BRIDGE
COMMAND
FACTORY
COMPOSITE
INTERPRETER
PROTOTYPE
DECORATOR
ITERATOR
SINGELTON
FAÇADE
MEDIATOR
FLYWEIGHT
MEMENTO
PROXY
OBSERVER
STATE
STRATEGY
TEMPLATE
METHOD
VISITOR
OF THE 23 PATTERNS WE HAVE COVERED 8 IN CLASS
WHAT IS TEMPLATE METHOD PATTERN??
The template method pattern is a behavioral
design pattern that defines the program skeleton of
an algorithm in a method, called template method,
which defers some steps to subclasses. It lets one
redefine certain steps of an algorithm without
changing the algorithm's structure
Behavioral design pattern : Design patterns that
identify common communication patterns between
objects and realize these communication patterns
Defines the program skeleton of an algorithm in a
method, called template method
Template method defers some steps in subclasses
It lets one redefine certain steps of an algorithm
without changing the algorithm's structure.
LETS SEE WHAT IT ACTUALLY MEANS
We’re going to create a Game abstract class defining
operations with a template method set to be final so
that it cannot be overridden. Cricket and Football are
concrete classes extend Game and override its methods.
STEP 1
CREATE AN ABSTRACT CLASS WITH A TEMPLATE
METHOD BEING FINAL.
public abstract class Game
{
abstract void initialize();
abstract void startPlay();
abstract void endPlay();
//template method
public final void play()
{
//initialize the game
initialize();
//start game
startPlay();
//end game
endPlay();
}
}
STEP 2
CREATE CONCRETE CLASSES EXTENDING THE ABOVE
CLASS.
public class Cricket extends Game
{
@Override
void endPlay()
{
System.out.println("Cricket Game Finished!");
}
@Override
void initialize()
{
System.out.println("Cricket Game Initialized! Start playing.");
}
@Override
void startPlay()
{
System.out.println("Cricket Game Started. Enjoy the game!");
}
}
STEP 3
SIMILAR STEP FOR FOOTBALL CLASS
public class Football extends Game {
@Override
void endPlay() {
System.out.println("Football Game Finished!");
}
@Override
void initialize() {
System.out.println("Football Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Football Game Started. Enjoy the game!");
}
}
STEP 4
USE THE GAME’S TEMPLATE METHOD PLAY()
TO DEMONSTRATE A DEFINED WAY OF
PLAYING GAME.
public class TemplatePatternDemo
{
public static void main(String[] args)
{
Game game = new Cricket();
game.play();
System.out.println();
game = new Football();
game.play();
}
}
STEP 5
OUTPUT
Cricket Game Initialized! Start playing.
Cricket Game Started. Enjoy the game!
Cricket Game Finished!
Football Game Initialized! Start playing.
Football Game Started. Enjoy the game!
Football Game Finished!
The Template Method pattern should be used:
– To implement the invariant parts of an
algorithm once and leave it up to subclasses to
implement the behavior that can vary.
– When refactoring is performed and common
behavior is identified among classes. A abstract
base class containing all the common code (in the
template method) should be created to avoid code
duplication.
THANK YOU…
© Copyright 2026 Paperzz