23 Design Patterns - Template Mode

Template mode

First, the definition

Framework defines one operation algorithm, delay some steps to subclasses, subclasses such an interface without changing the algorithm to redefine certain steps of the algorithm changes.

Two, UML diagram

 

 

Structure Third, the template pattern

A template pattern from the abstract class and a (set of) specific implementation classes.

Abstract class three methods:

1, abstract methods : the corresponding specifications defined in an abstract class, accordingly implemented by a specific category. (Typically performed by the main logic function abstract methods)

2, Template Method : declared in the abstract class and are achieved. Generally the template is defined as a method of final approach, particularly to prevent overridden in class.

3, hook method: in the Prime Minister declared in the class and to achieve, but subclasses can be rewritten, extended hook method to influence the logic of an abstract class.

The main role of the abstract class is a logical framework to build a business, usually done by experienced people.

Fourth, examples

A programmer to get a job, a set of data array is sorted and printed.

Analysis: The task is divided into two steps, step is to sort the data, another step is to sort the data after printing. Template mode is used for this, the architect responsible for the architecture building, then assigned to the complexity of the logic of the early, middle and high engineer.

Abstract class: AbstractSort

com.golf.module Package; 
// array of a sort and print 
public abstract class AbstractSort { 
    // abstract method of the abstract class 
    public abstract int [] sortArray (int Array [], int Low, High int); 
    / / template abstract class method 
    public void printArray Final (int Array []) { 
        int [] = ARR sortArray (Array, 0,-be array.length. 1); 
        for (int I = 0; I <arr.length; I ++) { 
            System.out.println (ARR [I]); 
        } 
    } 
}
    

 

 

 

Specific implementation class:

com.golf.module Package; 
// implementation class 
public class the extends the Sort AbstractSort { 
    // use quick sort 
    @Override 
    public int [] sortArray (int [] Array, Low int, int High) { 
        IF (Low> High ) 
            return Array; 
        // the value stored in the leftmost intermediate variable 
        int TEMP = Array [Low]; 
        int I = Low; 
        int J = High; 
        the while (Array [J]> = TEMP && I <J) {// from the start of the array rightmost view is smaller than the value of temp 
            J,; 
        } 
        array [I] = array [J]; // find the value is smaller than the right to the left temp 
        while (array [i] <= temp && i < j) {// begin looking from the left of the array is greater than the value of temp 
            I ++; 
        }
        array [j] = array [i ]; // to the left than to the right into the larger the value of temp
        array [i] = temp; // reference value 
        sortArray (array, low, j- 1); // call to the left half of the array 
        sortArray (array, j + 1, high); // call to the right half of the array 
        return array ; 
    } 
}

 

 

Client:

package com.golf.module;
​
public class Client {
    public static void main(String[] args) {
        int arr[] = {2,1,4,5,23,13,41,56};
        AbstractSort abstractSort = new Sort();
        abstractSort.printArray(arr);
    }
}

 


Fifth, the advantages and disadvantages

advantage:

1, easy to extend, it can thus extend the functionality implemented by the abstract methods. In line with the open - closed principle.

2, easy to maintain

Guess you like

Origin www.cnblogs.com/bm654/p/11981409.html