設計模式、用Delphi實現---->Strategy模式

Strategy模式

 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

起源

DelphiSTRATEGY模式是在STRATEGY的基礎上進行了擴展。更多STRATEGY模式的資料請參閱 《設計模式208頁》

目的

定義一系列的算法,把它們一個個封裝起來,並且使它們可相互替換。本模式使得算法可獨立於使用它的客戶面變化。

動機

·         由於需要可以動態的變換不同的策略。

·         更好的封裝多個行爲與算法結構,而不讓客戶程序直接干擾

·         通過相關的條件語句選擇正確的行爲方法。

Template方法使用繼承來變換部分算法。Strategies則使用代理和組合來變換所有的算法並支持動態變換。以後,將使用context對象在運行期動態的分配具體的算法。同樣,通過使用context對象,客戶類將從依賴的算法接口中分離出來,通過context對象可以提供更多的泛化接口。同樣意義的是,通過contextstrategy接口可以在將來設計並提供全系統的相關算法來實現具體的應用而無需改變接口。

Strategies同樣讓你您創建單一的、間單的、易維護的類框架。類框架依賴於繼承。

 

 

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

應用

Implementation

下例中的對信用卡進行月利率進行計算。Strategy模式通過充一的抽象類TfinanzeCharge封裝、訪問接口並擔供不同的算法來進行月利率計算。TregularChargeTpreferredCharge信用卡的月利率計算封裝了不同的具體算法

TmonthlyCharge實現了TcontextCharge接口並配置了不同的策略對象。TconntextCharge成爲客戶對象與策略對象的中場發動機,這樣有助於感輕客戶對象與策略/對象的依賴性。

 

 

    // 策略接口 (抽象類)

  TFinanzeCharge = class

  public

      // 返回計算的結果

    function getCharge(const Balance: double): double; virtual; abstract;

  end;

 

    // 具體策略

  TRegularCharge = class(TFinanzeCharge)

  public

    function getCharge(const Balance: double): double; override;

  end;

 

    //具體策略

  TPreferredCharge = class(TFinanzeCharge)

  public

     function getCharge(const Balance: double): double; override;

  end;

 

客戶程序依賴上下文接口來調度指定的策略。無論如何,因爲上下文接口是爲客戶程序而產生的,客戶程序必需知道可用的策略/對象。如果上下文無法返回一個有效的實例,可選擇選擇默認策略的方法實現。

 

 

     // 上下文接口

  TChargeContext = class

  public

    function ComputeCharges(const Balance: double): double; virtual; abstract;

  end;

 

//具體上下文

TmonthlyCharges作爲客戶對象與策略對象的中場發動機,並通過在它的構造器傳遞一個具體的利率計算實例進行設置。

This class acts as a mediator between the client and the strategy classes, and is configured by using composition and passing an instance of a concrete finance charge in its constructor.

 

  TMonthlyCharges = class(TChargeContext)

  private

    FFinanzeCharge: TFinanzeCharge;

  public

// 客戶程序訪問的接口

    function ComputeCharges(const Balance: double): double; override;

    // constructor configures the context object

    constructor Create(aFinanzeCharge: TFinanzeCharge); virtual;

    destructor Destroy; override;

  end;

 

---

implementation

 

// TRegularCharge

function TRegularCharge.getCharge(const Balance: double): double;

begin

  result := Balance * (REG_RATE / 12);

end;

 

// TPreferredCharge

function TPreferredCharge.getCharge(const Balance: double): double;

begin

   // this could be a complex algorithm that takes into account the

   // credit card holder’s buying patterns and reward points accumulated.

  result := Balance * (PREFERRED_RATE / 12);

end;

 

 

// Concrete Context

// TMonthlyCharges

constructor TMonthlyCharges.Create(aFinanzeCharge: TFinanzeCharge);

begin

  inherited Create;

    // aFinanzeCharge configures the context object

    // this class takes ownership of aFinanzeCharge (will destroy it)

  FFinanzeCharge := aFinanzeCharge;

end;

 

destructor TMonthlyCharges.Destroy;

begin

  FFinanzeCharge.Free;

  inherited Destroy;

end;

 

function TMonthlyCharges.ComputeCharges(const Balance: double): double;

begin

  result := FFinanzeCharge.getCharge(Balance);

end;

 

Delphi實例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章