<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-348  評論-598  文章-0  trackbacks-0
    在 Strategy Pattern中,當一個任務控制器需要處理某項事物的時候,它可以命令他所控制的事務讀取一個configuration file,然后根據(jù)這個file將一些配置信息傳遞給一個接口從而調(diào)用相應的信息,而這個接口可能是一個Aggregation的接口,這個Aggregation可能是一組規(guī)則或者內(nèi)容不一樣,但具有相同的類型或者特點的事務,可以用一個Abstract class讓他們繼承,
    這樣統(tǒng)一將對象交給固定接口,而外部只要調(diào)用這個接口即可。
    以下是“四人幫”的說法。
    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重點是用來創(chuàng)建對象。

    Strategy適合下列場合:

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

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

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

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



    ---------------------------------------------------------
    專注移動開發(fā)

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

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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 中文字幕人成无码免费视频| a高清免费毛片久久| 精品无码人妻一区二区免费蜜桃| 久久精品国产亚洲精品| 牛牛在线精品观看免费正| 免费人妻无码不卡中文字幕18禁| 亚洲av成人无码网站…| 亚洲精品97久久中文字幕无码| 国产精品自拍亚洲| yy6080亚洲一级理论| 国产黄在线播放免费观看| 亚洲自偷自偷图片| 久久国产精品免费专区| 亚洲第一香蕉视频| 久久久www成人免费毛片| 亚洲AV日韩AV无码污污网站| 亚洲av无码乱码在线观看野外| 深夜福利在线视频免费| 亚洲精品成人无码中文毛片不卡| 性无码免费一区二区三区在线| 亚洲色欲www综合网| 免费毛片在线播放| 免费一级全黄少妇性色生活片| 国产亚洲精品资源在线26u| 亚洲人成免费网站| 久久综合亚洲色hezyo| 亚洲片一区二区三区| 5555在线播放免费播放| 亚洲色偷偷色噜噜狠狠99| 亚洲第一福利网站在线观看| 久别的草原电视剧免费观看| 亚洲综合色7777情网站777| 亚洲av无码成人精品区| 免费无遮挡无码永久视频 | 免费国内精品久久久久影院| a级毛片免费观看在线| 亚洲日韩在线视频| 亚洲国产一区视频| 美女视频黄是免费的网址| 特级做a爰片毛片免费看| 亚洲精品在线电影|