Template mode
In the Template Pattern (Template Pattern), an abstract class publicly defines the way/template to execute its methods. Its subclasses can override the method implementation as needed, but the calls will be made in the manner defined in the abstract class. This type of design pattern is a behavioral pattern.
Introduction
Intent: Define the skeleton of an algorithm in an operation, while deferring some steps to subclasses. Template methods allow subclasses to redefine specific steps of an algorithm without changing the structure of the algorithm.
Main solution: Some methods are common, but this method is rewritten in every subclass.
When to use: There are some general methods.
How to solve: Abstract these general algorithms.
Key code: is implemented in the abstract class, and other steps are implemented in the subclass.
Application examples: 1. When building a house, the foundation, wiring, and water pipes are all the same. Only in the later stages of construction are there differences such as adding closets and fences. 2. The 81 difficulties set by Bodhisattva in Journey to the West are a top-level logical framework. 3. The support for Hibernate in Spirng encapsulates some predetermined methods, such as opening transactions, obtaining Sessions, closing Sessions, etc. Programmers do not need to repeatedly write the standardized code, and can save it by simply throwing an entity.
Advantages: 1. Encapsulate the constant parts and expand the variable parts. 2. Extract public code to facilitate maintenance. 3. Behavior is controlled by the parent class and implemented by the subclass.
Disadvantages: Each different implementation requires a subclass to implement, resulting in an increase in the number of classes and making the system larger.
Usage scenarios: 1. There are methods common to multiple subclasses with the same logic. 2. Important and complex methods can be considered as template methods.
Note: To prevent malicious operations, the final keyword is generally added to template methods.
Implementation
We will create a Game abstract class that defines the operation, where the template method is set to final so that it will not be overridden. Cricket and Football are entity classes that extend Game, and they override the methods of the abstract class.
TemplatePatternDemo, our demo class uses Game to demonstrate the usage of template pattern.
Step 1
Create an abstract class with its template method set to final.
Game.java
public abstract class Game { abstract void initialize(); abstract void startPlay(); abstract void endPlay(); //模板 public final void play(){ //初始化游戏 initialize(); //开始游戏 startPlay(); //结束游戏 endPlay(); } }
Step 2
Create an entity class that extends 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 demonstrates 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
Verify the 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!