What is Template Method Design Pattern in Java ?

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.



You May Interest

How Can We Find the Memory Usage of JVM From Java Code ?

How Strategy Design Pattern is Different From State Design Patter ...

How HashMap Works in Java ?

What are the Main Uses of Singleton Design Pattern in Java ?

What are the Main Uses of Spring MVC Module ?