设计模式、用Delphi实现---->Strategy模式

Strategy模式

 

 

起源

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

目的

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

动机

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

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

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

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

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

 

 

应用

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实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值