Delphi的STRATEGY模式是在STRATEGY的基礎上進行了擴展。更多STRATEGY模式的資料請參閱 《設計模式208頁》
目的
定義一系列的算法,把它們一個個封裝起來,并且使它們可相互替換。本模式使得算法可獨立于使用它的客戶面變化。
動機
? 由于需要可以動態的變換不同的策略。
? 更好的封裝多個行為與算法結構,而不讓客戶程序直接干擾
? 通過相關的條件語句選擇正確的行為方法。
Template方法使用繼承來變換部分算法。Strategies則使用代理和組合來變換所有的算法并支持動態變換。以后,將使用context對象在運行期動態的分配具體的算法。同樣,通過使用context對象,客戶類將從依賴的算法接口中分離出來,通過context對象可以提供更多的泛化接口。同樣意義的是,通過context與strategy接口可以在將來設計并提供全系統的相關算法來實現具體的應用而無需改變接口。
Strategies同樣讓你您創建單一的、間單的、易維護的類框架。類框架依賴于繼承。
應用
下例中的對信用卡進行月利率進行計算。Strategy模式通過充一的抽象類TfinanzeCharge封裝、訪問接口并擔供不同的算法來進行月利率計算。TregularCharge、TPReferredCharge為信用卡的月利率計算封裝了不同的具體算法 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實例