隨筆-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 閱讀(289) 評論(0)  編輯  收藏 所屬分類: Design Patterns

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


          網站導航:
           
          主站蜘蛛池模板: 灵宝市| 麻江县| 萨嘎县| 博湖县| 鞍山市| 若尔盖县| 宁波市| 赣州市| 通州市| 贞丰县| 沽源县| 枣阳市| 耒阳市| 中西区| 慈利县| 庆城县| 定襄县| 昌江| 灯塔市| 本溪市| 彭水| 铜川市| 南部县| 张家川| 邯郸县| 墨竹工卡县| 虹口区| 大石桥市| 大洼县| 乌鲁木齐市| 噶尔县| 调兵山市| 关岭| 大邑县| 阿拉善左旗| 通榆县| 津市市| 微博| 余江县| 治县。| 罗甸县|