May 27, 2021 Design mode
In Template Pattern, an abstract class exposes the way/template that defines how it is executed. I ts sub-classes can be implemented by rewriting methods as needed, but calls are made in the manner defined in the abstract class. This type of design pattern is behavioral.
Intent: Defines the skeleton of an algorithm in an operation and delays some steps into subsys classes. The template method allows sub-classes to redefine certain steps of an algorithm without changing its structure.
Main solution: Some methods are generic, but they are rewritten in every subse class.
When to use: There are some common methods.
How to solve: Abstract these common algorithms.
Key code: In abstract class implementations, other steps are implemented in sub-classes.
Application example: 1, in the construction of the house, the foundation, walk line, water pipes are the same, only in the later stages of the building with closets and fences and other differences. 2 , the western travelodle inside the 81 difficult, this is a top-level logical skeleton. 3 , Spirng's support for Hibernate encapsulates already set methods, such as opening transactions, getting Session, closing Session, and so on, so that programmers can save it without repeating the code that has already been coded.
Pros: 1, package unchanged part, extended variable part. 2 , extract public code, easy to maintain. 3 , the behavior is controlled by the parent class, the child class is implemented.
Cons: Each different implementation requires a sub-class to implement, resulting in an increase in the number of classes, making the system larger.
Use scenario: 1, there are multiple sub-classes common methods, and the logic is the same. 2, important, complex methods, can be considered as a template method.
Note: To prevent malicious actions, the general template method is added to the final keyword.
We'll create a Game abstraction class that defines the operation, where the template method is set to final so that it is not override. Cricket and Football are entity classes that extend Game, and they rewrite the methods of abstract classes.
TemplatePatternDemo, our demo class uses Game to demonstrate the use of template patterns.
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(); //模板 public final void play(){ //初始化游戏 initialize(); //开始游戏 startPlay(); //结束游戏 endPlay(); } }
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!"); } }
Use Game's template method play() 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(); } }
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!