Delphi的STRATEGY模式是在STRATEGY的基礎(chǔ)上進(jìn)行了擴(kuò)展。更多STRATEGY模式的資料請(qǐng)參閱 《設(shè)計(jì)模式208頁》
目的
定義一系列的算法,把它們一個(gè)個(gè)封裝起來,并且使它們可相互替換。本模式使得算法可獨(dú)立于使用它的客戶面變化。
動(dòng)機(jī)
? 由于需要可以動(dòng)態(tài)的變換不同的策略。
? 更好的封裝多個(gè)行為與算法結(jié)構(gòu),而不讓客戶程序直接干擾
? 通過相關(guān)的條件語句選擇正確的行為方法。
Template方法使用繼承來變換部分算法。Strategies則使用代理和組合來變換所有的算法并支持動(dòng)態(tài)變換。以后,將使用context對(duì)象在運(yùn)行期動(dòng)態(tài)的分配具體的算法。同樣,通過使用context對(duì)象,客戶類將從依賴的算法接口中分離出來,通過context對(duì)象可以提供更多的泛化接口。同樣意義的是,通過context與strategy接口可以在將來設(shè)計(jì)并提供全系統(tǒng)的相關(guān)算法來實(shí)現(xiàn)具體的應(yīng)用而無需改變接口。
Strategies同樣讓你您創(chuàng)建單一的、間單的、易維護(hù)的類框架。類框架依賴于繼承。
應(yīng)用
下例中的對(duì)信用卡進(jìn)行月利率進(jìn)行計(jì)算。Strategy模式通過充一的抽象類TfinanzeCharge封裝、訪問接口并擔(dān)供不同的算法來進(jìn)行月利率計(jì)算。TregularCharge、TPReferredCharge為信用卡的月利率計(jì)算封裝了不同的具體算法 TmonthlyCharge實(shí)現(xiàn)了TcontextCharge接口并配置了不同的策略對(duì)象。TconntextCharge成為客戶對(duì)象與策略對(duì)象的中場(chǎng)發(fā)動(dòng)機(jī),這樣有助于感輕客戶對(duì)象與策略/對(duì)象的依賴性。
// 策略接口 (抽象類)
TFinanzeCharge = class
public
// 返回計(jì)算的結(jié)果
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;
客戶程序依賴上下文接口來調(diào)度指定的策略。無論如何,因?yàn)樯舷挛慕涌谑菫榭蛻舫绦蚨a(chǎn)生的,客戶程序必需知道可用的策略/對(duì)象。如果上下文無法返回一個(gè)有效的實(shí)例,可選擇選擇默認(rèn)策略的方法實(shí)現(xiàn)。
// 上下文接口
TChargeContext = class
public
function ComputeCharges(const Balance: double): double; virtual; abstract;
end;
//具體上下文
類TmonthlyCharges作為客戶對(duì)象與策略對(duì)象的中場(chǎng)發(fā)動(dòng)機(jī),并通過在它的構(gòu)造器傳遞一個(gè)具體的利率計(jì)算實(shí)例進(jìn)行設(shè)置。
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實(shí)例