Delphi的STRATEGY模式是在STRATEGY的基础上进行了扩展。更多STRATEGY模式的资料请参阅 《设计模式208页》<o:p></o:p>
目的<o:p></o:p>
定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户面变化。<o:p></o:p>
动机<o:p></o:p>
· 由于需要可以动态的变换不同的策略。
· 更好的封装多个行为与算法结构,而不让客户程序直接干扰
· 通过相关的条件语句选择正确的行为方法。
Template方法使用继承来变换部分算法。Strategies则使用代理和组合来变换所有的算法并支持动态变换。以后,将使用context对象在运行期动态的分配具体的算法。同样,通过使用context对象,客户类将从依赖的算法接口中分离出来,通过context对象可以提供更多的泛化接口。同样意义的是,通过context与strategy接口可以在将来设计并提供全系统的相关算法来实现具体的应用而无需改变接口。
Strategies同样让你您创建单一的、间单的、易维护的类框架。类框架依赖于继承。
应用<o:p></o:p>
下例中的对信用卡进行月利率进行计算。Strategy模式通过充一的抽象类TfinanzeCharge封装、访问接口并担供不同的算法来进行月利率计算。TregularCharge、TpreferredCharge为信用卡的月利率计算封装了不同的具体算法<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;
分享到:
相关推荐
Building a Successful Board-Test Strategy A successful test strategy is a byproduct of overall product life-cycle management. Anyone building such a strategy must consider the product's development, ...
All Weather Strategy
Creating your strategy: Define the components of your technology strategy using proven patterns Communicating the strategy: Convey your technology strategy in a compelling way to a variety of ...
"Informatica Update Strategy Transformation 组件详解" Informatica 的 Update Strategy Transformation 组件是一个强大的工具,用于控制数据的更新策略。在数据集成和数据迁移过程中,Update Strategy ...
World Map Strategy Kit 2是功能强大的游戏工具包,可用于基于真实或虚构的世界地图创建策略游戏。 包括出色的视觉吸引力功能,这些功能实施起来很复杂,可以为您开发游戏提供AAA级启动。 ***视觉功能*** - 标准/...
Strategy An Introduction to Game Theory(3rd Edition).
Title: The Art of Application Performance Testing: From Strategy to Tools 2nd Edition Author: Ian Molyneaux Length: 278 pages Edition: 2 Language: English Publisher: O"Reilly Media Publication ...
dynamic cooperative coevolution strategy was proposed. The strategy was added to the dynamic multi-swarm particle swarm optimization algorithm. And the dual grouping of population and decision ...
In this study, we propose a hybrid variable selection strategy based on the continuous shrinkage of variable space which is the core idea of variable combination population analysis (VCPA). The VCPA-...
This paper presents a pseudospectral strategy for lane change maneuvers of automated vehicles on the highway. This strategy can optimize the longitudinal and lateral control simultaneously to account ...
实验一 Strategy模式的应用 某公司专门销售各种打印机,销售打印机时都有一定的折扣让利给顾客,但折扣计算的方法有很多种,如:不打折;每台减扣固定的金额;按售价的5%打折等等,且折扣计算方法可能发生变化。...
(原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.)
User experience (UX) strategy requires a careful blend of business strategy and UX design, but until now, there hasn’t been an easy-to-apply framework for executing it. This hands-on guide introduces...
Supply Chain Management- Strategy, Planning and Operations (5th Edition)
Writing_Scientific_Research_Articles-Strategy_and_Steps.pdf
STRATEGY-品牌私域流量搭建-2020.12-21页精品报告2020.pdf
《Strategy.Game.Programming.With.Directx.9.0》一书的源代码
SAP_Release_Strategy_2013.pdf sap 产品发布策略
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。...
设计模式-策略模式(Strategy)结合Android小知识讲解,简单明了