Template Pattern
# Template Pattern
In the Template Pattern, an abstract class publicly defines the way/method/template for executing its methods. Its subclasses can override the method implementations as needed, but the call will be made in the way defined in the abstract class. This type of design pattern falls under the behavioral pattern category.
## Introduction
### Intent
Define the skeleton of an algorithm in the parent class, allowing subclasses to redefine certain specific steps of the algorithm without changing the algorithm's structure.
### Main Problem Solved
* Solves the problem of repeatedly implementing the same methods in multiple subclasses by abstracting common methods into the parent class to avoid code duplication.
### Use Cases
* When there are some common methods that can be shared among multiple subclasses.
### Implementation Approach
* **Define an Abstract Parent Class**: Contains the template method and some abstract or concrete methods.
* **Implement Subclasses**: Inherit from the abstract parent class and implement the abstract methods without changing the algorithm structure.
### Key Code
* **Template Method**: Defined in the abstract parent class, calling abstract and concrete methods.
* **Abstract Method**: Implemented by subclasses, representing the variable part of the algorithm.
* **Concrete Method**: Implemented in the abstract parent class, representing the invariant part of the algorithm.
### Application Examples
* **Construction Process**: Steps like foundation, wiring, plumbing are the same, while later steps like adding cabinets or fences differ.
* **The 81 Tribulations in Journey to the West**: The 81 tribulations set by the Bodhisattva represent a top-level logical skeleton.
* **Spring's Support for Hibernate**: Encapsulates common methods such as starting transactions, obtaining Sessions, and closing Sessions.
### Advantages
* **Encapsulates Invariant Parts**: The invariant parts of the algorithm are encapsulated in the parent class.
* **Extends Variable Parts**: Subclasses can extend or modify the variable parts of the algorithm.
* **Extracts Common Code**: Reduces code duplication and facilitates maintenance.
### Disadvantages
* **Increased Number of Classes**: Each different implementation requires a subclass, which may lead to a large system.
### Usage Suggestions
* When there are common methods shared by multiple subclasses with the same logic, consider using the Template Method Pattern.
* For important or complex methods, consider defining them as template methods in the parent class.
### Notes
* To prevent malicious modifications, template methods are usually decorated with the `final` keyword to avoid being overridden by subclasses.
### Main Roles Included
* **Abstract Parent Class (Abstract Class)**:
* Defines the template method and some abstract or concrete methods.
* **Concrete Subclasses (Concrete Classes)**:
* Inherit from the abstract parent class and implement the abstract methods.
* **Hook Method (Optional)**:
* Defined in the abstract parent class, can be overridden by subclasses to affect the behavior of the template method.
* **Client (Optional)**:
* Uses the abstract parent class and concrete subclasses without needing to care about the details of the template method.
## Implementation
We will create an abstract class _Game_ that defines operations, where the template method is set to final so it cannot be overridden. _Cricket_ and _Football_ are concrete classes extending _Game_, overriding the methods of the abstract class.
_TemplatePatternDemo_, our demo class, uses _Game_ to demonstrate the usage of the template pattern.

### Step 1
Create an abstract class whose template method is set to final.
## Game.java
public abstract class Game{abstract void initialize(); abstract void startPlay(); abstract void endPlay(); //Template public final void play(){//Initialize game initialize(); //Start game startPlay(); //End game endPlay(); }}
### Step 2
Create concrete classes extending the above class.
## Cricket.java
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!"); }}
## Football.java
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 3
Use the template method play() of _Game_ to demonstrate how the game is defined.
## TemplatePatternDemo.java
public class TemplatePatternDemo{public static void main(String[]args){Game game = new Cricket(); game.play(); System.out.println(); game = new Football(); game.play(); }}
### Step 4
Execute the program, output the result:
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!
YouTip