1 Preface
The template method mode is a very simple design mode in object-oriented software design. The basic idea is to define a "standard" Algorithm in a method in an abstract class. The basic operations called in this method are implemented by the subclass overload. This method becomes a "template ". Because the algorithm defined by the method lacks some special operations.
2. Details
2.1 Brief Introduction
Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows subclass to redefine certain steps of an algorithm without changing the structure of the algorithm.
2.2 When to use
(1) The unchanged part of the algorithm needs to be implemented at one time, and the variable behavior is left to the subclass for implementation.
(2) The common behavior of subclass should be extracted and put into the public class to avoid code duplication. The differences in existing code should be separated into new operations. Then replace these different codes with a template method that calls these new operations.
(3) The extension of subclass needs to be controlled. You can define a template method to call the hook operation at a specific point. Sub-classes can be expanded on these points through hook operations.
2.3 template method call 5 types of operations
(1) specific operations on a specific class or client class;
(2) specific operations on abstract classes;
(3) Abstract operations;
(4) Factory method;
(5) Hook operation.
3 conclusion
The above is all content and I hope it will help you.