Template Pattern in Java Tutorial with Example


Template mode

In the Template Pattern, an abstract class exposes the way/template that implements its methods. Its subclasses can override method implementations as needed, but the calls will be made in the way defined in the abstract class. This type of design pattern is a behavioral model.

Introduction

Intent: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine certain steps of the algorithm without changing the structure of an algorithm.

Main solution: 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: is implemented in an abstract class, and other steps are implemented in a subclass.

Application example: 1. When building a house, the foundation, the wiring, and the water pipes are the same. Only in the later stage of the building, there are differences such as adding a closet and a fence. 2, the Bodhisattva in the Journey to the West is set to 81 difficult, this is a top-level logical skeleton. 3, Spring support for Hibernate, some of the methods have been packaged, such as open the transaction, get the Session, close the Session, etc., the programmer does not repeat the code that has been standardized, directly lose an entity can be saved.

Advantages: 1. Encapsulate the invariant part and expand the variable part. 2. Extract common code for easy maintenance. 3. The behavior is controlled by the parent class and the child class is implemented.

Disadvantages: Each different implementation requires a subclass to implement, resulting in an increase in the number of classes, making the system even larger.

Usage scenarios: 1. There are multiple methods shared by subclasses, and the logic is the same. 2. Important and complex methods can be considered as template methods.

Note: To prevent malicious operations, the general template method adds a final keyword.

Implementation

We'll create an Game abstract class that defines the action, where the template method is set to final so that it won't be overwritten. Cricket and Football are entity classes that extend Game and override the methods of abstract classes.

TemplatePatternDemo, our demo class uses Game to demonstrate the use of template patterns.

template_pattern_uml_diagram.jpg

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(){ //Initialization tour initialize(); //Start the game startPlay(); //End Game endPlay(); } }

Step 2

Create an entity class that extends the above classes.

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

Executing the program, outputting results:

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!





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition