Defined:
defines the framework for an algorithm in an operation, and delays some steps into subclasses. So that subclasses can redefine some specific steps of the algorithm without altering the structure of an algorithm.
Sounds like a good high-end look, I understand:
1. The parent class declares several abstract methods (basic methods) and several specific methods (template methods)
2. Abstract methods are steps of an algorithm (process) that are implemented in subclasses
3. Template method is an algorithm (process) framework, in the parent class has been agreed to implement the basic method call, complete the fixed logic
4. The structure of an algorithm (process) is defined in the parent class, and the specific implementation details are implemented in subclasses
Note: In order to prevent malicious action, the generic template method is combined with final, which prohibits overriding
General class Diagram:
In fact, the template approach is a pattern that is often used in programming. For example, one day, programmer a gets a task: Given an array of integers, sort the numbers in the array from small to large, and then print out the results after the order. After analysis, this task can be divided into two parts, sorting and printing, printing function is good to achieve, sort of a bit troublesome. But a has the method, first completes the printing function, the sorting function other person does.
Abstract class Abstractsort {
/**
* Sorts array arrays 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 newly graduated colleague B said: "There is a task, the main logic I have written, you put the rest of the logic to achieve it." So the Abstractsort class to B, let B write implementation. b Take a look, too easy, 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 index variable
int Temp; Staging variable for
(int i = index; i < Array.Length i++) {
if (Array[i] < MinValue) {//find minimum
minvalue = AR Ray[i]; Store minimum value
indexmin = i
}
}
Temp = Array[index]; Exchange of two numerical
array[index] = array[indexmin];
Array[indexmin] = Temp;
}
}
Write it and give it to A,a for a run:
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);
}
Run Result:
Sort results: 0 1 3 4 5 7 9 10 12 32
the structure of the template method pattern
The template method pattern consists of an abstract class and a (or a group of) implementation classes that consist of an inheritance structure, and the methods in the abstract class are divided into three types:
Abstract method: The parent class declares but does not implement, but defines the specification and then implements it by its subclasses.
Template method: Declared and implemented by an abstract class. In general, the template method invokes an abstract method to accomplish the primary logical function, and most of the template methods are defined as final types, indicating that the primary logical functionality cannot be overridden in subclasses.
Hook method: Declared and implemented by an abstract class. But subclasses can be extended, and subclasses can influence the logic of the template method by extending the hook method.
The task of an abstract class is to build a logical framework, usually written by an experienced person, because the quality of an abstract class directly determines the stability of the program.
Implementation classes are used to implement the details. The template method in an abstract class is precisely the way to complete the business logic by implementing class extensions. As long as the extension method in the implementation class has passed the unit test, the overall function will not appear big error under the premise of correct template method.
advantages of Template method patterns:
1. Package invariant parts, extending variable parts
2. Extract common part code for easy maintenance
3. Behavior is controlled by the parent class, and the subclass implements
Scenario for template Method patterns:
1. Multiple subclasses have public methods and the logic is essentially the same
2. For complex algorithms, the core algorithm is designed as a template method, and the detail function is realized by each subclass
3. Refactoring Code
Extension of template Method pattern
- The basic method can be designed as a protected type because it does not need to provide access externally
- Design Hook method: Externally provided interface that can affect the specific execution order within the template method
Summarize:
The parent class establishes a framework that, when overridden by a parent-class partial method, invokes a method that inherits from the parent class, producing different results.