1. 策略模式简介

1.1 定义

  策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,手段不同而最终达成的目标一致。客户只关心目标而不在意具体的实现方法,实现方法要根据具体的环境因素而变化。

1.2 使用频率

   中高

2. 策略模式结构图

2.1 结构图

2.2 参与者

  策略模式参与者:

  ◊ Strategy 策略

    ° 定义所支持的算法的公共接口。Context使用这个接口来调用某个ConcreteStrategy定义的算法。

  ◊ ConcreteStrategy 具体策略

    ° 实现Strategy接口中的具体算法。

  ◊ Context 上下文

    ° 通过一个ConcreteStrategy对象来对其进行配置;

    ° 维护一个对Strategy对象的引用;

    ° 可定义一个接口来让Strategy访问它的数据。

3. 策略模式结构实现

  Strategy.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StrategyPattern.Structural
{
public abstract class Strategy
{
public abstract void AlgorithmInterface();
}
}

  ConcreteStrategyA.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StrategyPattern.Structural
{
public class ConcreteStrategyA : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("Called ConcreteStrategyA.AlgorithmInterface()");
}
}
}

  ConcreteStrategyB.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StrategyPattern.Structural
{
public class ConcreteStrategyB : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("Called ConcreteStrategyB.AlgorithmInterface()");
}
}
}

  ConcreteStrategyC.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StrategyPattern.Structural
{
public class ConcreteStrategyC : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("Called ConcreteStrategyC.AlgorithmInterface()");
}
}
}

  Context.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StrategyPattern.Structural
{
public class Context
{
private Strategy _strategy; public Context(Strategy strategy)
{
this._strategy = strategy;
} public void ContextInterface()
{
_strategy.AlgorithmInterface();
}
}
}

  Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace StrategyPattern.Structural
{
class Program
{
static void Main(string[] args)
{
Context context; context = new Context(new ConcreteStrategyA());
context.ContextInterface(); context = new Context(new ConcreteStrategyB());
context.ContextInterface(); context = new Context(new ConcreteStrategyC());
context.ContextInterface();
}
}
}

  运行结果:

4. 策略模式应用分析

4.1 策略模式适用情形

  ◊ 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  ◊ 一个系统需要动态地在几种算法中选择一种。这些具体算法类均有统一的接口,由于多态性原则,客户端可以选择使用任何一个具体算法类,并只持有一个数据类型是抽象算法类的对象。
  ◊ 一个系统的算法使用的数据不可以让客户端知道。策略模式可以避免让客户端涉及到不必要接触到的复杂的和只与算法有关的数据。
  ◊ 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。此时,使用策略模式,把这些行为转移到相应的具体策略类里面,可以避免使用难以维护的多重条件选择语句。

4.2 策略模式优点

(1)支持开闭原则(OCP)。

(2)策略模式使用继承模式抽取公共代码到基类,避免重复代码。

(3)策略模式避免使用多重条件判断语句(if/else、switch等)。

4.3 策略模式缺点

(1)客户端(Client)必须知道所有的策略类,并自行决定使用哪一个策略类。策略模式只适用于客户端知道所有的算法或行为的情况。
(2)策略模式造成很多的策略类。

C#设计模式系列:策略模式(Strategy)的更多相关文章

  1. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  2. 反馈法学习设计模式(一)——策略模式Strategy Pattern

    简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...

  3. JAVA设计模式之策略模式 - Strategy

    在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...

  4. 8.6 GOF设计模式四: 策略模式… Strategy Pattern

    策略模式… Strategy Pattern  在POS系统中,有时需要实行价格优惠, 该如何处理?  对普通客户或新客户报全价  对老客户统一折扣5%  对大客户统一折扣10%  注:课件 ...

  5. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

  6. [设计模式] 21 策略模式 Strategy

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化. 策略模 ...

  7. 设计模式 笔记 策略模式 Strategy

    //---------------------------15/04/28---------------------------- //Strategy 策略模式----对象行为型模式 /* 1:意图 ...

  8. 大熊君说说JS与设计模式之------策略模式Strategy

    一,总体概要 1,笔者浅谈 策略模式,又叫算法簇模式,就是定义了不同的算法,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式和工厂模式有一定的类似,策略模式相对简单容易理解,并 ...

  9. 设计模式之策略模式Strategy

    /** * 策略设计模式 * 策略模式:定义一系列的算法族,使他们之间可以相互转换,动态的改变其行为. * 问题:设计一个鸭子模拟游戏. * 现在有一群鸭子: * ①这些鸭可以有飞的行为(分为快和慢) ...

  10. 大话设计模式之策略模式(strategy)

    策略模式:它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响使用算法的用户. 针对商城收银模式,打折,返现促销等的例子: 打折还是促销其实都是一些算法,可以用工厂模式来 ...

随机推荐

  1. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. 【BZOJ1857】[Scoi2010]传送带 三分法

    三分套三分,挺神奇的...每次找到,每个传送带的上下两个三等分点,下面那个小,则一定有更优的在中间. #include <iostream> #include <cstdio> ...

  3. SQL 查找重复项及批量修改数据成固定格式

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select   peopleId  from ...

  4. 最长下降子序列O(n^2)及O(n*log(n))解法

    求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...

  5. Odoo 二次开发教程(三)-第一个Model及Form、Tree视图

    创建完我们的模块,接下来我们就要为我们的模块添加一些对象.今天我们将要创建一个学生对象(tech.student)和一些基本的属性,并将用form和tree视图将其展示出来: 一. 创建tech.st ...

  6. Python3.5+selenium操作Chrome浏览器

    1.安装selenium 命令提示符下输入: pip install selenium 2.下载chromedriver 点击下载 3.将解压后的chromedriver.exe放到chrome浏览器 ...

  7. Python之路Day17-jQuery

    本节内容: jQuery 参考:http://jquery.cuishifeng.cn/ 模块  <==>类库 Dom/Bom/JavaScript的类库 版本:1.x   1.12 2. ...

  8. StreamingAssets文件夹在不同平台上的引用

    On a desktop computer (Mac OS or Windows) the location of the files can be obtained with the followi ...

  9. Linux上传下载文件快捷命令

    远程链接Linux(如SecrueCRT),要上传文件很下载文件到Linux服务器,只需要使用sz或者rz命令即可快速下载和上传文件了. 使用方法: 1.首先确保Linux服务器系统中安装了lrzsz ...

  10. HTML打折计算价格

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <met ...