In the template pattern (Template Pattern), an abstract class exposes a way / template that defines the method in which it is executed. Its subclasses can override the method implementation as needed, but the invocation will be made in the manner defined in the abstract class. This type of design pattern belongs to behavioral pattern. 意图: Define the skeleton of an algorithm in an operation and defer some steps to a subclass. The template method allows subclasses to redefine certain steps of an algorithm without changing the structure of the algorithm. 主要解决: Some methods are common, but this method is rewritten in each subclass. 何时使用: There are some general methods. 如何解决: Abstract these general algorithms. 关键代码: In the abstract class implementation, the other steps are implemented in the subclass. 应用实例: 1. When building a house, the foundation, wiring and water pipes are all the same, and only in the later stage of the building can there be differences such as adding closets and fences. 2. The 81 difficulties set by the Bodhisattva in Journey to the West, which is a logical skeleton at the top. 3. The support for Hibernate in spring encapsulates some established methods, such as opening a transaction, obtaining Session, closing Session, etc. Programmers do not repeat the code that has been standardized, just throw an entity and save it. 优点: 1. Encapsulate the invariant part and extend the variable part. 2. Extract the common code for easy maintenance. 3. The behavior is controlled by the parent class and implemented by the subclass. 缺点: Each different implementation needs a subclass to implement, which leads to an increase in the number of classes and makes the system larger. 使用场景: 1. There are methods common to multiple subclasses and the logic is the same. 2. Important and complex methods can be considered as template methods. 注意事项: In order to prevent malicious operation, the general template method adds the final keyword. We will create a Game Abstract class, where the template method is set to final so that it is not overridden. Cricket And Football It’s expanded. Game Entity classes that override the methods of the abstract class TemplatePatternDemo Our demo class uses the Game To demonstrate the use of template patterns. Create an abstract class whose template method is set to final. Create an entity class that extends the above class. Use Game Template method play () to demonstrate how the game is defined. Execute the program and output the result: 6.26.1. Introduction ¶
6.26.2. Realize ¶

6.26.3. Step 1 ¶
Game.java ¶
publicabstractclassGame{abstractvoidinitialize();abstractvoidstartPlay();abstractvoidendPlay();//模板publicfinalvoidplay(){//初始化游戏initialize();//开始游戏startPlay();//结束游戏endPlay();}}
6.26.4. Step 2 ¶
Cricket.java ¶
publicclassCricketextendsGame{@OverridevoidendPlay(){System.out.println("Cricket
Game Finished!");}@Overridevoidinitialize(){System.out.println("Cricket
Game Initialized! Start
playing.");}@OverridevoidstartPlay(){System.out.println("Cricket Game
Started. Enjoy the game!");}}
Football.java ¶
publicclassFootballextendsGame{@OverridevoidendPlay(){System.out.println("Football
Game Finished!");}@Overridevoidinitialize(){System.out.println("Football
Game Initialized! Start
playing.");}@OverridevoidstartPlay(){System.out.println("Football Game
Started. Enjoy the game!");}}
6.26.5. Step 3 ¶
TemplatePatternDemo.java ¶
publicclassTemplatePatternDemo{publicstaticvoidmain(String[]args){Gamegame=newCricket();game.play();System.out.println();game=newFootball();game.play();}}
6.26.6. Step 4 ¶
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!