詳解Java編程中的戰略形式。本站提示廣大學習愛好者:(詳解Java編程中的戰略形式)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解Java編程中的戰略形式正文
戰略形式屬於對象的行動形式。其意圖是針對一組算法,將每個算法封裝到具有配合接口的自力的類中,從而使得它們可以互相調換。戰略形式使得算法可以在不影響到客戶真個情形下產生變更。
戰略形式的構造
戰略形式是對算法的包裝,是把應用算法的義務和算法自己朋分開來,委派給分歧的對象治理。戰略形式平日把一個系列的算法包裝到一系列的戰略類外面,作為一個籠統戰略類的子類。用一句話來講,就是:“預備一組算法,並將每個算法封裝起來,使得它們可以交換”。上面就以一個表示性的完成講授戰略形式實例的構造。
這個形式觸及到三個腳色:
源代碼
情況腳色類
public class Context { //持有一個詳細戰略的對象 private Strategy strategy; /** * 結構函數,傳入一個詳細戰略對象 * @param strategy 詳細戰略對象 */ public Context(Strategy strategy){ this.strategy = strategy; } /** * 戰略辦法 */ public void contextInterface(){ strategy.strategyInterface(); } }
籠統戰略類
public interface Strategy { /** * 戰略辦法 */ public void strategyInterface(); }
詳細戰略類
public class ConcreteStrategyA implements Strategy { @Override public void strategyInterface() { //相干的營業 } } public class ConcreteStrategyB implements Strategy { @Override public void strategyInterface() { //相干的營業 } } public class ConcreteStrategyC implements Strategy { @Override public void strategyInterface() { //相干的營業 } }
以戰略形式剖析Java源碼
聲明:這裡參考了Java源碼剖析-戰略形式在Java聚集框架完成代碼中的表現
在java的聚集框架中,結構Map或許Set時傳入Comparator比擬器,或許創立比擬器傳入Collections類的靜態辦法中作為辦法的參數為Collection排序時,都應用了戰略形式
簡略的挪用代碼:
import java.util.*; public class TestComparator { public static void main(String args[]) { LinkedList<String> list = new LinkedList<String>(); list.add("wangzhengyi"); list.add("bululu"); // 創立一個逆序比擬器 Comparator<String> r = Collections.reverseOrder(); // 經由過程逆序比擬器停止排序 Collections.sort(list, r); System.out.println(list); } }
應用Collections.reverseOrder()辦法完成一個比擬器後,再挪用Collections.sort(list, r)把比擬器傳入該辦法中停止排序,上面看一下sort(list, r)中的代碼:
public static <T> void sort(List<T> list, Comparator<? super T> c) { Object[] a = list.toArray(); Arrays.sort(a, (Comparator)c); ListIterator i = list.listIterator(); for (int j=0; j<a.length; j++) { i.next(); i.set(a[j]); } }
Array.sort(a, (Comparator)c);這句持續把比擬器傳入處置,上面是Array.sort(a, (Comparator)c)的詳細操作:
public static <T> void sort(T[] a, Comparator<? super T> c) { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, c); } static <T> void sort(T[] a, Comparator<? super T> c) { sort(a, 0, a.length, c); } /** To be removed in a future release. */ private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) { T[] aux = a.clone(); if (c==null) mergeSort(aux, a, 0, a.length, 0); else mergeSort(aux, a, 0, a.length, 0, c); }
持續跟下去好了:
private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off, Comparator c) { int length = high - low; // Insertion sort on smallest arrays if (length < INSERTIONSORT_THRESHOLD) { for (int i=low; i<high; i++) for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) swap(dest, j, j-1); return; } // Recursively sort halves of dest into src int destLow = low; int destHigh = high; low += off; high += off; int mid = (low + high) >>> 1; mergeSort(dest, src, low, mid, -off, c); mergeSort(dest, src, mid, high, -off, c); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (c.compare(src[mid-1], src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); return; } // Merge sorted halves (now in src) into dest for(int i = destLow, p = low, q = mid; i < destHigh; i++) { if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) dest[i] = src[p++]; else dest[i] = src[q++]; } }
把應用到比擬器的代碼遴選出來:
// If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (c.compare(src[mid-1], src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); return; }
這裡的compare辦法在Comparator接口中也有界說:
public interface Comparator<T> { int compare(T o1, T o2); }
因為這裡是泛型完成了Comparator,所以現實履行時,會依據比擬器的詳細完成類挪用到完成代碼,也就是下面創立的逆序比擬器的compare辦法,其完成辦法以下:
public int compare(Comparable<Object> c1, Comparable<Object> c2) { return c2.compareTo(c1); }