隨筆-348  評論-598  文章-0  trackbacks-0
          在 Strategy Pattern中,當一個任務控制器需要處理某項事物的時候,它可以命令他所控制的事務讀取一個configuration file,然后根據這個file將一些配置信息傳遞給一個接口從而調用相應的信息,而這個接口可能是一個Aggregation的接口,這個Aggregation可能是一組規則或者內容不一樣,但具有相同的類型或者特點的事務,可以用一個Abstract class讓他們繼承,
          這樣統一將對象交給固定接口,而外部只要調用這個接口即可。
          以下是“四人幫”的說法。
          Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.[6]





          public class TaskController {
            
          public void process () {
               
          // this code is an emulation of a
               
          // processing task controller
               
          // . . .
               
          // figure out which country you are in
               CalcTax myTax;
               myTax
          = getTaxRulesForUS();
               SalesOrder mySO
          = new SalesOrder();
               mySO.process( myTax);// 當然你還可以讓myTax=getTaxRulesForCan();這樣mySo.process(myTax)就會按照加拿大的稅率處理
            }

            
          private CalcTax getTaxRulesForUS() {
               
          // In real life, get the tax rules based on
               
          // country you are in.  You may have the
               
          // logic here or you may have it in a
               
          // configuration file
               
          // Here, just return a USTax so this
               
          // will compile.
               return new USTax();
            }

          }


          public class SalesOrder {
             
          public void process (CalcTax taxToUse) {
                
          long itemNumber= 0;
                
          double price= 0;

                
          // given the tax object to use

                
          // . . .

                
          // calculate tax
                double tax=
                   taxToUse.taxAmount( itemNumber, price);
             }

          }


          public abstract class CalcTax {
             
          abstract public double taxAmount(
                
          long itemSold, double price);
          }


          public class CanTax extends CalcTax {
             
          public double taxAmount(
                
          long itemSold, double price) {
                
          // in real life, figure out tax according to
                
          // the rules in Canada and return it
                
          // here, return 0 so this will compile
                return 0.0;
             }

          }

          public class USTax extends CalcTax {
             
          public double taxAmount(
                
          long itemSold, double price) {
                
          // in real life, figure out tax according to
                
          // the rules in the US and return it
                
          // here, return 0 so this will compile
                return 0.0;
             }

          }


          實際整個Strategy的核心部分就是抽象類的使用,使用Strategy模式可以在用戶需要變化時,修改量很少,而且快速.

          Strategy和Factory有一定的類似,Strategy相對簡單容易理解,并且可以在運行時刻自由切換。Factory重點是用來創建對象。

          Strategy適合下列場合:

          1.以不同的格式保存文件;

          2.以不同的算法壓縮文件;

          3.以不同的算法截獲圖象;

          4.以不同的格式輸出同樣數據的圖形,比如曲線 或框圖bar等



          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2007-04-07 17:23 TiGERTiAN 閱讀(285) 評論(0)  編輯  收藏 所屬分類: Design Patterns

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 肃宁县| 卢龙县| 延长县| 伊金霍洛旗| 沧源| 舒城县| 正宁县| 富阳市| 金阳县| 嘉义县| 锡林郭勒盟| 新乡市| 阳江市| 内黄县| 河北省| 嘉兴市| 隆化县| 通州区| 榆林市| 双牌县| 台前县| 句容市| 沁水县| 久治县| 望奎县| 马关县| 阿拉善右旗| 即墨市| 八宿县| 辉县市| 凤山市| 芜湖市| 绵竹市| 白河县| 延吉市| 陇川县| 同仁县| 庄浪县| 芷江| 江城| 称多县|