Related links:
[design mode] column: [design mode] column
Relevant examples can be downloaded: Examples of common Java design patterns
Template method pattern
Template Method Pattern defines the algorithm skeleton in an operation, and delays some steps of the algorithm to subclasses, so that subclasses can redefine some specific steps of the algorithm without changing the structure of the algorithm. It is a behavioral pattern.
An abstract class exposes methods / templates that define how its methods are executed. Its subclasses can override the method implementation as needed, but the call will be made in the way defined in the abstract class.
advantage
-
It encapsulates the invariant part and extends the variable part. It encapsulates the algorithm that is considered to be the invariant part into the parent class, and inherits the variable part algorithm from the subclass, which is convenient for the subclass to continue to expand.
-
It extracts part of the common code from the parent class to facilitate code reuse.
-
Some methods are implemented by subclasses, so subclasses can add corresponding functions through extension, which conforms to the "open close principle".
shortcoming
-
It is necessary to define a subclass for each different implementation, which will increase the number of classes, make the system larger and the design more abstract, and indirectly increase the complexity of the system implementation.
-
Abstract methods in the parent class are implemented by subclasses, and the execution results of subclasses will affect the results of the parent class, which leads to a reverse control structure, which improves the difficulty of code reading.
-
Due to the shortcomings of the inheritance relationship itself, if a new abstract method is added to the parent class, all subclasses must be changed again.
Role of template method pattern
The template method pattern needs to pay attention to the cooperation between abstract classes and concrete classes. It uses the polymorphism technology of virtual functions and the reverse control technology of "don't call me, let me call you".
The template method mode mainly includes the following roles:
1) Abstract Class / Abstract Class
Abstract template class, which is responsible for giving the outline and skeleton of an algorithm. It consists of a template method and several basic methods. These methods are defined as follows:
① Template method
The skeleton of the algorithm is defined, and the basic methods contained in it are called in a certain order.
② Basic method
It is a step in the whole algorithm, including the following types
-
Abstract method: declared in an abstract class and implemented by a concrete subclass
-
Concrete method: it has been implemented in an abstract class and can be inherited or overridden in a concrete subclass.
-
Hook method: it has been implemented in abstract classes, including logical methods for judgment and empty methods that need to be overridden by subclasses.
2) Concrete Class
The concrete implementation class implements the abstract methods and hook methods defined in the abstract class, which are a constituent step of a top-level logic.
Sample code
abstract class
/** * Template method pattern - abstract class / abstract template * * (Milk tea) * */ public abstract class MilkTea { /** * Template method */ public void makingMilkTea() { // Add tea tea(); // Add milk milk(); // Add ingredients if (needToAddCondiments()) { addCondiments(); } } /** * Abstract method, adding ingredients */ public abstract void addCondiments(); /** * Specific method */ public void tea() { System.out.println("The first step is to add black tea"); } /** * Specific method */ public void milk() { System.out.println("Step two, add milk"); } /** * Hook method is used to judge whether ingredients need to be added * @return */ public boolean needToAddCondiments() { return true; } }
Specific subclass
/** * Template method pattern - concrete subclass / concrete implementation * * (Pearl milk tea) * */ public class PearlMilkTea extends MilkTea{ @Override public void addCondiments() { System.out.println("The third step is to add pearls"); } }
/** * Template method pattern - concrete subclass / concrete implementation * * (Burning fairy grass) * */ public class GrassJellyMilkTea extends MilkTea{ @Override public void addCondiments() { System.out.println("The third step is to add burning fairy fodder"); } }
/** * Template method pattern - concrete subclass / concrete implementation * * (Pure milk tea) * */ public class PureMilkTea extends MilkTea{ @Override public void addCondiments() { } @Override public boolean needToAddCondiments() { return false; } }
Test class
/** * Template method pattern * */ public class TemplatePatternDemo { public static void main(String[] args) { System.out.println("===Making pearl milk tea==="); MilkTea milkTea = new PearlMilkTea(); milkTea.makingMilkTea(); System.out.println("===Making burning fairy grass==="); milkTea = new GrassJellyMilkTea(); milkTea.makingMilkTea(); System.out.println("===Making pure milk tea==="); milkTea = new PureMilkTea(); milkTea.makingMilkTea(); } }
Conclusion
1. See more about design patterns [design mode] column
2. Relevant example codes can be downloaded: Examples of common Java design patterns
PS: [ Examples of common Java design patterns Included in] [design mode] column The code involved in the code can be downloaded by students who need the code. If you have downloaded it before, you don't need to download it again~
If the above contents are incorrect or need to be supplemented, please consult more and update and correct them in time~
Welcome to comment ~ thank you for your praise~