Java single application - Architecture Mode - 03. Design mode - 24. Template mode

Original address:
More tutorials: Beam cloud - free course

Template mode

Serial number Chapter in text video
1 Summary -
2 Realization -

Please refer to the navigation above for reading

1. overview

In Template Pattern, an abstract class exposes a method / template that defines how its methods are executed. Its subclass can override the method implementation as needed, but the call will take place in the way defined in the abstract class. This type of design pattern belongs to behavioral pattern.


Define the skeleton of the algorithm in an operation, and delay some steps to the subclass. The template method allows subclasses to redefine some specific steps of an algorithm without changing the structure of the algorithm.

Main solutions:

Some methods are generic, but they are rewritten in every subclass.

When to use:

There are some general methods.

How to solve:

Abstract these general algorithms.

Key code:

In the abstract class implementation, other steps are implemented in the subclass.

Application example:

  • When building a house, the foundation, wiring and water pipes are the same. Only in the later stage of the building can there be differences such as adding closets and fences.
  • The 81 difficulties set by Bodhisattva in journey to the west is a top-level logical framework.
  • The support for Hibernate in Spring encapsulates some established methods, such as opening transactions, obtaining sessions, closing sessions, etc. programmers do not write the standardized code repeatedly, and can save it by throwing an entity directly.


  • Encapsulate the invariant part and extend the mutable part.
  • Extract common code for easy maintenance.
  • The behavior is controlled by the parent class and implemented by the child class.


Each different implementation needs a subclass, which leads to the increase of the number of classes and makes the system larger.

Usage scenario:

  • There are methods shared by multiple subclasses, and the logic is the same.
  • Important and complex methods can be considered as template methods.

matters needing attention:

In order to prevent malicious operations, the general template methods are added with the final keyword.

2. implementation

We will create a Game abstract class that defines the operation, where the template method is set to final so that it is not overridden.

Cricket and Football are entity classes that extend Game, and they override methods of abstract classes.

TemplatePatternDemo, our demo class uses Game to demonstrate the usage of template pattern.

Step 1

Create an abstract class whose template method is set to final., the code is as follows:

public abstract class Game {
   abstract void initialize();
   abstract void startPlay();
   abstract void endPlay();

   public final void play(){

      //Initialize game

      //Start the game

      //End the game

Step 2

Create entity classes that extend the above classes.

The code of is as follows:

public class Cricket extends Game {

   void endPlay() {
      System.out.println("Cricket Game Finished!");

   void initialize() {
      System.out.println("Cricket Game Initialized! Start playing.");

   void startPlay() {
      System.out.println("Cricket Game Started. Enjoy the game!");
}, the code is as follows:

public class Football extends Game {

   void endPlay() {
      System.out.println("Football Game Finished!");

   void initialize() {
      System.out.println("Football Game Initialized! Start playing.");

   void startPlay() {
      System.out.println("Football Game Started. Enjoy the game!");

Step 3

Use Game's template method play() to demonstrate how the Game is defined., the code is as follows:

public class TemplatePatternDemo {
   public static void main(String[] args) {

      Game game = new Cricket();;
      game = new Football();;      

Step 4

Execute the program and 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!

Last article: Strategy mode
Next article: Visitor mode

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.

Keywords: Java Hibernate Spring

Added by Rollo Tamasi on Fri, 13 Mar 2020 15:53:20 +0200