`
cooldesigner
  • 浏览: 67443 次
  • 性别: 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;

分享到:
评论

相关推荐

    The All Weather Strategy

    全天候策略(All Weather Strategy)是一种投资组合管理方法,旨在通过跨资产类别的风险平衡分配来实现长期稳定的回报。该策略由桥水基金(Bridgewater Associates)创始人雷·达里奥(Ray Dalio)提出,并自1996年...

    Strategy An Introduction to Game Theory(3rd Edition).

    ### 游戏理论入门——《Strategy: An Introduction to Game Theory》第三版 #### 书籍概述 《Strategy: An Introduction to Game Theory》是一本由Joel Watson编写的关于游戏理论的经典教材,现已是第三版。该书由W...

    Informatica Update Strategy Transformation组件 详解

    "Informatica Update Strategy Transformation 组件详解" Informatica 的 Update Strategy Transformation 组件是一个强大的工具,用于控制数据的更新策略。在数据集成和数据迁移过程中,Update Strategy ...

    策略模式 Strategy Pattern

    ### 策略模式 Strategy Pattern #### 概述 策略模式是一种行为设计模式,它使得算法可以在运行时被更改。这种模式允许一个类的行为或其算法在运行时根据需要进行改变,通过这种方式,我们可以轻松地扩展不同的算法...

    World Map Strategy Kit 2 V9.7.1

    World Map Strategy Kit 2是功能强大的游戏工具包,可用于基于真实或虚构的世界地图创建策略游戏。 包括出色的视觉吸引力功能,这些功能实施起来很复杂,可以为您开发游戏提供AAA级启动。 ***视觉功能*** - 标准/...

    策略(strategy)模式

    2. **具体策略类(Concrete Strategy)**:实现了策略接口,提供了具体的算法实现。可以有多个具体策略类,每个类代表一种算法。 3. **上下文(Context)**:上下文使用策略接口来调用策略对象的算法。它通常包含了...

    Competitive Strategy (Michael E. Porter)

    Competitive Strategy Techniques for Anal yzing Industries and Competitors (Michae l E. Porter) [size=13.3333px]Now nearing its sixtieth printing in English and tr anslated into nineteen languages, ...

    Strategy.Game.Programming.With.Directx.9.0.Source.Code

    《Strategy.Game.Programming.With.Directx.9.0》是一本专为游戏开发者设计的书籍,主要探讨了如何利用DirectX 9.0进行即时战略(RTS)游戏的编程。DirectX是微软推出的一系列应用程序接口(API),主要用于多媒体,...

    PSO.zip_On Strategy_algorithm_article_cec2013_pso dynamic

    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 ...

    VCPA-based hybrid strategy.zip

    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-...

    STRATEGY-品牌私域流量搭建-2020.12-21页精品报告2020.pdf

    尽管提供的【部分内容】中包含大量OCR扫描错误和乱码,但依然可以推测出这份文件标题为《STRATEGY-品牌私域流量搭建-2020.12-21页精品报告2020.pdf》是一份专注于品牌私域流量搭建策略的行业报告。报告的标签为...

    走进设计模式之 策略模式(Strategy)

    在`Strategy.cpp`、`main.cpp`、`Context.cpp`、`Strategy.h`、`Context.h`等文件中,我们可以看到这种模式的具体实现,其中`a.out`是编译后的可执行文件。通过理解和应用策略模式,开发者可以更灵活地管理代码中的...

    Real-time Strategy FX v1.0

    《实时策略外汇交易系统在Unity中的应用:Real-time Strategy FX v1.0》 Unity引擎作为全球领先的跨平台游戏开发工具,已经广泛应用于各种类型的游戏制作,包括实时策略类游戏。"Real-time Strategy FX v1.0"是针对...

    Java 设计模式-策略模式(Strategy)Android讲解

    首先,策略模式由三个主要组成部分构成:上下文(Context)、策略(Strategy)接口和具体策略(Concrete Strategy)。上下文是使用策略的对象,它维护一个对策略的引用,并调用策略的接口来执行算法。策略接口定义了一组...

    行为型——Strategy(策略模式)

    策略模式的核心组成部分包括环境(Context)、策略(Strategy)和具体策略(Concrete Strategy)。 1. **环境(Context)**:这是使用策略的对象,它维持一个对策略对象的引用,可以在运行时根据需要选择合适的策略。环境...

    Real-time Strategy FX.rar

    "Real-time Strategy FX"是一个利用Unity引擎开发的项目,它将游戏开发技术与金融交易策略相结合,旨在提供一个直观、高效的外汇交易模拟环境。Unity因其强大的3D图形渲染能力、跨平台支持和丰富的社区资源,已经...

    sap release strategy 2013

    从提供的部分内容来看,2013年的SAP Release Strategy文档是为了解释SAP在软件产品发布方面的一般原则、策略和计划,这些计划虽然旨在指导未来的业务方向,但是由于变化的市场和技术环境,它们并不是最终的承诺。...

    Strategy Companion Analyzer 安裝手冊下載

    ### Strategy Companion Analyzer 安装手册知识点详解 #### 标题:Strategy Companion Analyzer 安裝手冊下載 **知识点概述:** 本手册详细介绍了如何安装和配置 Strategy Companion Analyzer 的过程,包括环境...

Global site tag (gtag.js) - Google Analytics