The template method mode is also called the template method mode. It is one of the 23 design modes proposed by gof. The template method mode is one of the action modes.AlgorithmSome of the necessary processing methods in are granted to the abstract method, and the behavior of the entire algorithm is changed through the different implementations of the abstract method inherited by the subclass.
This article describes the concept and usage of the template method in the design mode, and how to use the template method mode for development in actual applications.
Concept of template method mode
The template method pattern, just like its name, defines a template method (public) with fixed algorithms and can be subdivided into multiple steps in the parent class of the abstract class ), the template method mode abstracts the variable steps that can be subdivided into abstract methods that can be overloaded by the quilt class (protected abstract), and reloads (redefinition) in the subclass ), you do not need to change the algorithm steps of the template method, but can redefine some specific steps in the algorithm.
[This figure is from Wikipedia wikipedia.org]:
The figure is explained based on the above definition.
-Abstractclass is equivalent to the parent class of the abstract class. concreteclass is the concrete implementation subclass. In a specific application, one or more implementation subclasses may exist.
-Abstractclass defines a public templatemethod () template method and the Method1 () and method2 () Methods used as steps.
-Abstractclass # templatemethod () method body call the Method1 () and method2 () Methods
Public void templatemethod (){
...
This. Method () 1;
...
This. Method () 2;
...
}
-The Method1 () and method2 () methods are protected Abstract METHODS (protected abstract ). This method must be reloaded to implement the concreteclass subclass.
Application scenarios of the template method mode
The template method mode is generally used in applications with the following conditions:
-Unified operation steps or procedures
-Different Operation Details
-There are multiple application scenarios with the same operation steps, but some specific operation details are different