Introduction of template method mode
Template method pattern: defines the skeleton of an algorithm and allows subclasses to provide implementation for one or more steps;
The template method pattern enables subclasses to redefine some steps of the algorithm without changing the algorithm structure;
Template method mode type: behavioral;
Two, template method mode applicable scenario
Applicable scenarios of template method mode:
- Parent class perspective: implement a constant part of the algorithm at one time, and leave the variable part to the child class implementation;
- Subclass perspective: in each subclass, the common part is extracted and concentrated in a common parent class to avoid code duplication;
The purpose of template method pattern is to allow subclasses to extend or implement a specific step of fixed methods; For the template, it is a set of fixed algorithms. Some algorithm steps in the fixed algorithm can be extended through subclasses;
Three, template method mode advantages and disadvantages
Advantages of template method mode:
-
Improve code reusability: put the same part of the code in the abstract parent class;
-
Improve Extensibility: put different codes in different subclasses, and add new behaviors through subclass expansion;
-
Comply with the opening and closing principle: call the operation of the child class through the parent class, and add new behavior through the extension of the child class;
The template method pattern defines the invariable behavior in the parent class, removes the repeated code of the child class, reflects its advantages, and provides a good code reuse platform;
Disadvantages of template method mode:
-
Increasing complexity: the number of classes increases, which increases the complexity of the system; Abstract classes are introduced. For each implementation, a subclass needs to be defined;
-
Inheritance disadvantages: template methods are mainly implemented through inheritance, and the inheritance relationship itself has disadvantages. If a new abstract method is added to the parent class, all subclasses must be modified;
4, Template method extension
Template method extension: extension by hook method;
- Hook method: provides default behavior, and subclasses can be extended when necessary;
Hook method is the template to further develop and expand subclasses;
5, Template method pattern and related design pattern
Template method mode and factory method mode: factory method is a special implementation of template method;
Template method pattern and policy pattern: both encapsulate the algorithm logic;
-
The purpose of policy mode is to make various algorithms replace each other without affecting the use of end users and transparent to end users;
-
Template method mode refers to an algorithm flow, in which some different algorithm steps are handed over to subclasses for implementation,
The template method mode will not change the algorithm flow, the policy mode can change the algorithm flow, and the policies of the policy mode can be replaced with each other;
6, Template method pattern code example
Business scenario: put the elephant in the refrigerator;
1. Template method abstract class
package templatemethod; public abstract class Fridge { /** * The template method cannot be modified. It is modified with final. Subclasses are not allowed to override this method * Process to prevent subclasses from modifying template methods */ protected final void store() { openDoor(); closeDoor(); // This hook method allows subclasses to control the execution flow of template methods if (needColdStorage()) { codeStorage(); } put(); } /** * The method is invariant and subclass modification is not allowed */ final void openDoor() { System.out.println("Open the refrigerator door"); } final void codeStorage() { System.out.println("Turn on the refrigeration function"); } /** * Hook method, subclass can be overridden * Open appropriate permissions to the application layer to control the template method process * @return */ protected boolean needColdStorage() { return false; } /** * Abstract methods that require subclasses to implement */ abstract void put(); final void closeDoor() { System.out.println("Close the refrigerator door"); } }
2. Template method implementation class 1
Note that this class implements the hook method and changes the execution flow of the template method;
package templatemethod; public class FishFridge extends Fridge { @Override void put() { System.out.println("Put the fish in the fridge"); } @Override protected boolean needColdStorage() { // Refrigerated storage is required return true; } }
3. Template method implementation class 2
package templatemethod; public class ElephantFridge extends Fridge { @Override void put() { System.out.println("Put the elephant in the fridge"); } }
4. Test class
package templatemethod; public class Main { public static void main(String[] args) { // Put the elephant in the fridge Fridge elephantFridge = new ElephantFridge(); elephantFridge.store(); System.out.println(); // Put the fish in the fridge Fridge fishFridge = new FishFridge(); fishFridge.store(); } }
Execution results:
Open the refrigerator door Close the refrigerator door Put the elephant in the fridge Open the refrigerator door Close the refrigerator door Refrigerated storage Put the fish in the fridge