今天天氣非常好,下着淅淅瀝瀝的小雨,刮着風,感覺甚好;我在北京向各位問好。這幾天公司基本沒什么事兒,從昨天開始就復習了一下多線程編程,今天給各位分享一種Java23種設計模式中最常見的設計模式--策略模式。為什么將策略模式和多線程綁在一起呢,不知道各位有沒有注意過我們在進行多線程編程的時候,創建線程的方式有2種,一種是繼承Thread類,另外一種就是實現Runnable接口;當然,我們會毫不保留的選擇第二種,因為擴展性強,習慣接口開發等等原因,但是第二種方式還潛藏了23種Java設計模式中的其中一個模式,那就是低調而又強勢的"策略模式"。
說說策略模式吧,這種模式的設計思想就是:為了考慮業務邏輯的千變萬化和復雜程度是不一定的,因此我們需要對這套程序進行高度的抽象,這樣才能將業務邏輯和抽象進行相分離,才能夠讓這套程序的可擴展性變的極強,這也是在開發中會使用策略模式的強大之處。好了讀到這兒,我相信從來沒有接觸過這種設計模式的讀者來說,還是一頭霧水,接下來我就開始通過多線程來請出我們的"策略模式"。
先看代碼再解釋:
1 public static void main(String[] args) { 2 //創建線程一: 3 new Thread(new Runnable() { 4 @Override 5 public void run() { 6 int i=100; 7 while(i>0){ 8 System.out.println("創建的線程一:"+i--); 9 } 10 } 11 }).start(); 12 //創建線程二: 13 new Thread(new Runnable() { 14 @Override 15 public void run() { 16 int i=100; 17 while(i>50){ 18 System.out.println("創建的線程二:"+i--); 19 } 20 } 21 }).start(); 22 //主線程: 23 int i=0; 24 while(i<100){ 25 System.out.println("主線程:"+i++); 26 } 27 28 }
上面這段代碼是一個最簡單的多線程案例,這段程序總共由3個線程來同時執行(當然除此之外還有Java虛擬機的垃圾回收等等其他一些線程),並交錯打印變量的值。在這兒我們先暫時停一下,我想反問各位一個問題,為什么我們重寫的是Runnable接口的run()方法,但是開啟線程的卻是start()方法?沒錯,正是"策略模式"在操控,什么?還有這種操作。哈哈哈,我當時學習這種設計模式的時候也是各種奇怪,各種撞牆,不過還好我通過了一個實例至少簡單明白了它是如何操控的。
下面我就通過一個實例讓各位讀者切身體會一下策略模式的好處和強大。需求:設計一個隨心所欲的算法器?
第一步:只需設計一個算法接口即可,主要是因為規范或者讓程序知道如何進行調用。
1 /** 2 * 算法接口 3 * @author zxz 4 * 5 */ 6 interface CalculatorInterface{ 7 int calc(int x,int y); 8 }
第二步:就是整個程序的結構,也就是算法器的高度抽象部分,里面約束了整個程序的框架和大概流程,但是最重要的是並未涉及到業務層面的東西,因為我這個需求是設計一個算法器,肯定是有數據流入流出的,那么它就是將數據的流入流出做了規范,只是提供了一個默認的邏輯實現。
1 /** 2 * 算法器 3 */ 4 class Calculator{ 5 private int x=0; 6 private int y=0; 7 private CalculatorInterface calculatorInterface = null; 8 9 public Calculator(int x, int y) { 10 this.x = x; 11 this.y = y; 12 } 13 14 public Calculator(int x, int y,CalculatorInterface calculatorInterface) { 15 this(x,y); 16 this.calculatorInterface = calculatorInterface; 17 } 18 19 public int calc(int x,int y){ 20 return x+y; 21 } 22 //只需關注接口,並且將接口用到的參數傳遞進去即可,不必擔心具體的業務是如何封裝的【這段代碼請各位好好斟酌】 23 public int result(){ 24 if(null!=calculatorInterface){ 25 return calculatorInterface.calc(x, y); 26 } 27 return calc(x, y); 28 } 29 }
第三步:額外定義加法和減法算法類,為了下面的測試使用。
1 /** 2 * 加法算法 3 */ 4 class AddStrategy implements CalculatorInterface{ 5 @Override 6 public int calc(int x, int y) { 7 return x+y; 8 } 9 } 10 /** 11 * 減法算法 12 */ 13 class SubStrategy implements CalculatorInterface{ 14 @Override 15 public int calc(int x, int y) { 16 return x-y; 17 } 18 }
第四步:我們開始測試,看看現象應該就能明白策略模式是如何低調的。
1 /** 2 * 主程序 3 */ 4 public class Strategy { 5 6 public static void main(String[] args) { 7 //沒有任何策略時的結果 8 Calculator c = new Calculator(33, 3); 9 System.out.println(c.result()); 10 11 //傳入減法策略的結果 12 Calculator c1 = new Calculator(33, 3,new SubStrategy()); 13 System.out.println(c1.result()); 14 15 //【是時候展示真正的技術了】策略模式的強大,在這里算法可以隨意設置,只要你使用接口方式創建對象 16 int result = new Calculator(33, 3,new CalculatorInterface() { 17 @Override 18 public int calc(int x, int y) { 19 return (x+y)/2; 20 } 21 }).result(); 22 System.out.println(result); 23 } 24 25 }
最后,策略設計模式的實現過程算是完成了,主要就是這套代碼,需要各位自己斟酌,如有不對的地方留言指正,共勉。