`
cooldesigner
  • 浏览: 66718 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

Strategy

阅读更多

起源<o:p></o:p>

DelphiSTRATEGY模式是在STRATEGY的基础上进行了扩展。更多STRATEGY模式的资料请参阅 《设计模式208页》<o:p></o:p>

目的<o:p></o:p>

定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户面变化。<o:p></o:p>

动机<o:p></o:p>

·         由于需要可以动态的变换不同的策略。

·         更好的封装多个行为与算法结构,而不让客户程序直接干扰

·         通过相关的条件语句选择正确的行为方法。

Template方法使用继承来变换部分算法。Strategies则使用代理和组合来变换所有的算法并支持动态变换。以后,将使用context对象在运行期动态的分配具体的算法。同样,通过使用context对象,客户类将从依赖的算法接口中分离出来,通过context对象可以提供更多的泛化接口。同样意义的是,通过contextstrategy接口可以在将来设计并提供全系统的相关算法来实现具体的应用而无需改变接口。

Strategies同样让你您创建单一的、间单的、易维护的类框架。类框架依赖于继承。

应用<o:p></o:p>

Implementation

下例中的对信用卡进行月利率进行计算。Strategy模式通过充一的抽象类TfinanzeCharge封装、访问接口并担供不同的算法来进行月利率计算。TregularChargeTpreferredCharge信用卡的月利率计算封装了不同的具体算法<o:p></o:p>

TmonthlyCharge实现了TcontextCharge接口并配置了不同的策略对象。TconntextCharge成为客户对象与策略对象的中场发动机,这样有助于感轻客户对象与策略/对象的依赖性。

 <o:p></o:p>

 <o:p></o:p>

    // 策略接口 (抽象类)

  TFinanzeCharge = class

  public

      // 返回计算的结果

    function getCharge(const Balance: double): double; virtual; abstract;<o:p></o:p>

  end;

 <o:p></o:p>

    // 具体策略<o:p></o:p>

  TRegularCharge = class(TFinanzeCharge)

  public

    function getCharge(const Balance: double): double; override;<o:p></o:p>

  end;

 <o:p></o:p>

    //具体策略<o:p></o:p>

  TPreferredCharge = class(TFinanzeCharge)

  public

     function getCharge(const Balance: double): double; override;<o:p></o:p>

  end;

 <o:p></o:p>

客户程序依赖上下文接口来调度指定的策略。无论如何,因为上下文接口是为客户程序而产生的,客户程序必需知道可用的策略/对象。如果上下文无法返回一个有效的实例,可选择选择默认策略的方法实现。<o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>

     // 上下文接口<o:p></o:p>

  TChargeContext = class

  public

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

  end;

 <o:p></o:p>

//具体上下文<o:p></o:p>

TmonthlyCharges作为客户对象与策略对象的中场发动机,并通过在它的构造器传递一个具体的利率计算实例进行设置。<o:p></o:p>

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.

 <o:p></o:p>

  TMonthlyCharges = class(TChargeContext)

  private

    FFinanzeCharge: TFinanzeCharge;

  public

// 客户程序访问的接口

    function ComputeCharges(const Balance: double): double; override;<o:p></o:p>

    // constructor configures the context object<o:p></o:p>

    constructor Create(aFinanzeCharge: TFinanzeCharge); virtual;<o:p></o:p>

    destructor Destroy; override;

  end;

 <o:p></o:p>

---

implementation

 <o:p></o:p>

// TRegularCharge<o:p></o:p>

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

begin

  result := Balance * (REG_RATE / 12);

end;

 <o:p></o:p>

// TPreferredCharge<o:p></o:p>

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;

 <o:p></o:p>

 <o:p></o:p>

// Concrete Context<o:p></o:p>

// TMonthlyCharges<o:p></o:p>

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;

 <o:p></o:p>

destructor TMonthlyCharges.Destroy;

begin

  FFinanzeCharge.Free;

  inherited Destroy;

end;

 <o:p></o:p>

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

begin

  result := FFinanzeCharge.getCharge(Balance);

end;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics