深刻探索Java設計模式(二)之策略模式

抽絲剝繭 細說架構那些事——【優銳課】html

策略設計模式是Java API庫中常見的模式之一。這與另外一個設計模式(稱爲狀態設計模式)很是類似。本文是在學習完優銳課JAVA架構VIP課程—【框架源碼專題】中《學習源碼中的優秀設計模式》後寫下的學習感悟。簡要介紹了該思想,並提供了有關如何在Java中實現該思想的示例。java

深刻探索Java設計模式(一)之單例模式算法

 

總覽

策略模式也稱爲策略模式。它被歸類爲行爲軟件設計模式,其中重點是在對象之間找到靈活的通訊模式。它有助於在運行時對象之間創建靈活的通訊。編程

 

策略模式

策略模式的基本思想是在類的較小層次擴展中組合一組操做。與該策略相關的對象肯定在給定狀況下將使用哪一種算法。例如,它使咱們可以在運行時交換算法的實現細節,而無需咱們重寫它。這個想法與依賴注入中的實現模式產生了共鳴,由於它還容許在測試過程當中將實現換出,例如在測試代碼中執行模擬實現。設計模式

從狀態設計模式的角度來看,它相似於狀態設計模式,它是封裝上下文對象狀態的對象。策略設計模式中的對象相似地封裝了算法的實現,而且能夠根據須要在運行時交換事件。架構

在Java API庫中,java.awt.Container組件是使用此模式的示例。此處,LayoutManager充當策略對象。這些類(例如BorderLayout,FlowLayout和GridLayout)實現接口LayoutManager。實現的類定義一個名爲addLayoutComponent()的方法。此方法內的定義肯定邏輯,或如何將組件佈置在Container對象中。例如,FlowLayout從左到右放置它們,而BorderLayout將它們放置在名爲CENTER,NORTH,EAST,SOUTH,WEST的區域中。 Container類包含稱爲LayoutManager對象的策略對象。由於該接口包含對實現該接口的類的對象的引用,因此策略對象能夠隨時引用已實現的類。app

 

策略模式的使用

它基本上是從通用基類繼承的類的集合。它具備相同的基本結構和通用功能,但區別在於使用方式。能夠說,它按照所應用的策略起做用。策略模式經過提供運行時靈活性來加強你的能力。與其餘相似的模式(例如狀態和命令)不一樣,能夠經過建立代理類來利用這種靈活性,該代理類在運行時控制特定策略的選擇。框架

 

實現策略模式

這是此模式的快速實現。這個想法很簡單,可是能夠有效地選擇所需的實現並根據須要進行交換。沒什麼好想的,類的接口和層次結構建立了演繹,這是在代碼中使用的一種策略。dom

在這裏,它使咱們可以使用Sort接口編寫代碼,而咱們想要排序的代碼沒必要關心用於排序的算法。經過對接口和適合特定實現的類結構進行編程,咱們能夠根據須要使用接口,並在須要其餘接口時當即更改策略。從某種意義上說,這個想法是可擴展的,在之後,咱們能夠添加更多的實現。針對該接口編寫的任何代碼都不會更改,但能夠使用新的實現。ide

 1 package org.mano.example;  2 public interface Sort {  3    int [] sort(int[] nos);  4 }  5 package org.mano.example;  6 public class BubbleSort implements Sort{  7  @Override  8    public int [] sort(int[] nos) {  9       System.out.println("\n--- BUBBLE SORT strategy
10          in action---\n");
11       int n = nos.length; 12       for (int i = 0; i < n-1; i++) 13       for (int j = 0; j < n-i-1; j++) 14       if (nos[j] > nos[j+1]) 15  { 16          int tmp = nos[j]; 17          nos[j] = nos[j+1]; 18          nos[j+1] = tmp; 19  } 20       return nos; 21  } 22 } 23 package org.mano.example; 24 public class SelectionSort implements Sort{ 25  @Override 26    public int [] sort(int [] nos) { 27       System.out.println("\n--- SELECTION SORT strategy
28          in action ---\n");
29       int n = nos.length; 30       for (int i = 0; i < n-1; i++) 31  { 32          int mindex = i; 33          for (int j = i+1; j < n; j++) 34             if (nos[j] < nos[mindex]) 35                mindex = j; 36          int temp = nos[mindex]; 37          nos[mindex] = nos[i]; 38          nos[i] = temp; 39  } 40       return nos; 41  } 42 } 43 package org.mano.example; 44 public class ShellSort implements Sort { 45  @Override 46    public int [] sort(int[] nos) { 47       System.out.println("\n--- SHELL SORT strategy
48          in action ---\n");
49       int n = nos.length; 50       for (int part = n/2; part > 0; part /= 2) 51  { 52          for (int i = part; i < n; i += 1) 53  { 54             int temp = nos[i]; 55             int j; 56             for (j = i; j >= part && nos[j - part] >
57                temp; j -= part) nos[j] = nos[j - part]; 58             nos[j] = temp; 59  } 60  } 61       return nos; 62  } 63 } 64 package org.mano.example; 65 import java.util.Random; 66 public class SortingApp { 67    private Sort sortStrategy; 68    public SortingApp(Sort sort){ 69       this.sortStrategy = sort; 70  } 71    public int [] sort(int[] data){ 72       return sortStrategy.sort(data); 73  } 74    public void changeStrategy(Sort anotherStrategy){ 75       sortStrategy = anotherStrategy; 76  } 77    public void printArray(int arr[]) 78  { 79       int n = arr.length; 80       for (int i=0; i<n; ++i) 81          System.out.print(arr[i]+" "); 82  System.out.println(); 83  } 84    public static int [] getDummyData(){ 85       Random r = new Random(); 86       int [] data = new int [10]; 87       for (int i=0;i<10;i++) 88          data[i] = r.nextInt(100); 89       return data; 90  } 91    public static void main(String[] args){ 92       SortingApp app = new SortingApp(new BubbleSort()); 93  app.printArray(app.sort(getDummyData())); 94       app.changeStrategy(new SelectionSort()); 95  app.printArray(app.sort(getDummyData())); 96       app.changeStrategy(new ShellSort()); 97  app.printArray(app.sort(getDummyData())); 98  } 99 }

 

輸出

 1 --- BUBBLE SORT strategy in action---
 2  
 3 5 15 22 38 41 45 56 72 72 97
 4  
 5 --- SELECTION SORT strategy in action ---
 6  
 7 42 47 52 55 60 76 79 82 86 96
 8  
 9 --- SHELL SORT strategy in action ---
10  
11 11 13 19 24 27 33 47 72 72 88

 

結論

策略模式使咱們可以將有關要使用的實施策略的決策推遲到運行時爲止。當咱們使用Spring Framework將XML用做配置文件來構造對象及其依賴項時,將在運行時讀取它。這種模式的主要優勢是它利用了實現之間的動態變化,而無需從新編譯。

感謝閱讀!歡迎留言。想更深刻探討學習也歡迎私信我。下篇繼續~

深刻探索Java設計模式(三)之裝飾器模式

深刻探索Java設計模式(四)之享元模式

深刻探索Java設計模式(五)之構建器模式

原文出處:https://www.cnblogs.com/youruike-/p/12060947.html

相關文章
相關標籤/搜索