Strategy模式
起源
Delphi的STRATEGY模式是在STRATEGY的基础上进行了扩展。更多STRATEGY模式的资料请参阅 《设计模式208页》
目的
定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户面变化。
动机
? 由于需要可以动态的变换不同的策略。? 更好的封装多个行为与算法结构,而不让客户程序直接干扰? 通过相关的条件语句选择正确的行为方法。
Template方法使用继承来变换部分算法。Strategies则使用代理和组合来变换所有的算法并支持动态变换。以后,将使用context对象在运行期动态的分配具体的算法。同样,通过使用context对象,客户类将从依赖的算法接口中分离出来,通过context对象可以提供更多的泛化接口。同样意义的是,通过
context与
strategy接口可以在将来设计并提供全系统的相关算法来实现具体的应用而无需改变接口。
Strategies同样让你您创建单一的、间单的、易维护的类框架。类框架依赖于继承。
应用
Implementation
下例中的对信用卡进行月利率进行计算。
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 objectconstructor Create(aFinanzeCharge: TFinanzeCharge); virtual;destructor Destroy; override; end;---implementation
// TRegularChargefunction TRegularCharge.getCharge(const Balance: double): double;begin result := Balance * (REG_RATE / 12);end;
// TPreferredChargefunction 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// TMonthlyChargesconstructor 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实例