definition:
A framework for defining an algorithm in an operation, delaying some steps to a subclass. This allows a subclass to redefine certain specific steps of an algorithm without changing the structure of an algorithm.
It sounds very high-end, my understanding:
1. The parent class declares several abstract methods (basic methods) and several specific methods (template methods)
2. The abstract method is a step of an algorithm (process) that is implemented in a subclass
3. The template method is an algorithm (process) framework. It has been agreed in the parent class to implement calls to basic methods and complete fixed logic.
4. The structure of an algorithm (process) is defined in the parent class, and the specific implementation details are implemented in the subclass.
Note: In order to prevent malicious operations, the general template method is added with final, and rewriting is prohibited.
General Class Diagram:
In fact, the template method is a frequently used pattern in programming. Let’s first look at an example. One day, programmer A got a task: give an array of integers, sort the numbers in the array from small to large, and then print out the sorted results. After analysis, this task can be roughly divided into two parts: sorting and printing. The printing function is easy to implement, so sorting is a bit troublesome. But A has a way, first complete the printing function and find someone else to do the sort function.
abstract class AbstractSort { /** * Sort array from small to large* @param array */ protected abstract void sort(int[] array); public void showSortResult(int[] array){ this.sort(array); System.out.print("Sort result:"); for (int i = 0; i < array.length; i++){ System.out.printf("%3s", array[i]); } } }After writing, A found a colleague who had just graduated and started working and said: There is a task, I have written the main logic, so you can implement the remaining logic. So I gave the AbstractSort class to B and let B write to implement it. B took it over and saw it. It was too simple. It was done in 10 minutes. The code is as follows:
class ConcreteSort extends AbstractSort { @Override protected void sort(int[] array){ for(int i=0; i<array.length-1; i++){ selectSort(array, i); } } private void selectSort(int[] array, int index) { int MinValue = 32767; // Min value variable int indexMin = 0; // Min value index variable int Temp; // Staging variable for (int i = index; i < array.length; i++) { if (array[i] < MinValue){ // Find the minimum value MinValue = array[i]; //Storage the minimum value indexMin = i; } } Temp = array[index]; //Swap two numeric values array[index] = array[indexMin]; array[indexMin] = Temp; } }After writing it, give it to A, and A will take it to run it:
public class Client { public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // Preset data array public static void main(String[] args){ AbstractSort s = new ConcreteSort(); s.showSortResult(a); } } Running results:
Sort results: 0 1 3 4 5 7 9 10 12 32
Structure of template method pattern
The template method pattern consists of an abstract class and a (or a group of) implementation classes through inheritance structures. The methods in abstract classes are divided into three types:
Abstract method: The parent class only declares but does not implement it, but defines the specifications, and then implements them by its subclass.
Template method: declared and implemented by abstract classes. Generally speaking, template methods call abstract methods to complete the main logical functions, and template methods are mostly defined as final types, indicating that the main logical functions cannot be rewritten in subclasses.
Hook method: declared and implemented by abstract class. However, subclasses can be extended, and subclasses can affect the logic of template methods by extending hook methods.
The task of abstract classes is to build a logical framework, which is usually written by experienced personnel, because the quality of abstract classes directly determines whether the program is stable.
Implementation classes are used to implement details. The template method in abstract classes completes business logic by implementing class extension methods. As long as the extension method in the implementation class passes unit test, the overall function will generally not have major errors under the premise that the template method is correct.
Advantages of template method mode:
1. Encapsulate the unchanged part and expand the variable part
2. Extract public part of the code for easy maintenance
3. The behavior is controlled by the parent class and implemented by the child class
Applicable scenarios for template method mode:
1. Multiple subclasses have public methods and the logic is basically the same
2. For complex algorithms, the core algorithm is designed as a template method, and the detailed functions are implemented by each subclass.
3. Refactoring the code
Extension of template method pattern
Summarize:
The parent class establishes a framework. After the child class rewrites some methods of the parent class, it calls the method inherited from the parent class to produce different results.