设计模式笔记:策略模式(Strategy)
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)策略模式造成很多的策略类。
设计模式笔记:策略模式(Strategy)的更多相关文章
- 设计模式 笔记 策略模式 Strategy
//---------------------------15/04/28---------------------------- //Strategy 策略模式----对象行为型模式 /* 1:意图 ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
- 反馈法学习设计模式(一)——策略模式Strategy Pattern
简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...
- JAVA设计模式之策略模式 - Strategy
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...
- 8.6 GOF设计模式四: 策略模式… Strategy Pattern
策略模式… Strategy Pattern 在POS系统中,有时需要实行价格优惠, 该如何处理? 对普通客户或新客户报全价 对老客户统一折扣5% 对大客户统一折扣10% 注:课件 ...
- 二十四种设计模式:策略模式(Strategy Pattern)
策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...
- [设计模式] 21 策略模式 Strategy
在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化. 策略模 ...
- 大熊君说说JS与设计模式之------策略模式Strategy
一,总体概要 1,笔者浅谈 策略模式,又叫算法簇模式,就是定义了不同的算法,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式和工厂模式有一定的类似,策略模式相对简单容易理解,并 ...
- 设计模式之策略模式Strategy
/** * 策略设计模式 * 策略模式:定义一系列的算法族,使他们之间可以相互转换,动态的改变其行为. * 问题:设计一个鸭子模拟游戏. * 现在有一群鸭子: * ①这些鸭可以有飞的行为(分为快和慢) ...
- 大话设计模式之策略模式(strategy)
策略模式:它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响使用算法的用户. 针对商城收银模式,打折,返现促销等的例子: 打折还是促销其实都是一些算法,可以用工厂模式来 ...
随机推荐
- oracle 查询 磁盘使用率
SELECT d.tablespace_name "Name", TO_CHAR(NVL(a.bytes / 1024 / 1024 / 1024, 0), '99, ...
- matlab sign函数用法及实例
在MATLAB科学计算过程当中,我们经常需要对我们的计算公式或者计算结果检验其符号,,sign函数就给我们提供了这种方便,下面就通过实例介绍一下matlab sign函数 的用法,希望能够给您带来帮助 ...
- Python2.7-内置类型
1.布尔型:True.False 2.数值型:int.float.long.complex 3.迭代器型:有 __iter__() 方法和 next() 方法,生成器也是其中一种 4.序列型(sequ ...
- flask config
# config配置 { 'DEBUG': False, # 是否开启Debug模式 'TESTING': False, # 是否开启测试模式 'PROPAGATE_EXCEPTIONS': None ...
- Linux加密、安全版块、root密码破解
当一个入侵者进入了你的系统并且种植了木马,通常会想办法来隐蔽这个木马(除了木马自身的一些隐蔽特性外,他会尽量给你检查系统的过程设置障碍),通常入侵者会修改一些文件,比如管理员通常用ps -aux来查看 ...
- linux中断源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 本篇文章主要讲述源码中是如何对中断进行一系列的初始化的. 回顾 在上一篇概述中,介绍了几个对于中断来说非常重要的 ...
- 18-(基础入门篇)GPRS(Air202)拨打电话--(由于板子做修订,所以暂停更新)
https://www.cnblogs.com/yangfengwu/p/9968883.html 这个直接用官方给的demo就可以 先睹为快 现在说个需求哈,是当初一个人给提出的需求 例如存入的号码 ...
- 获取2个集合List<T>的共同元素
获取2个集合List<T>的共同元素,循环2个集合,然后比对. class Bj { public void GetIntersect() { , , , , , , }; , , , , ...
- Java 中单引号和双引号的区别
引自:https://blog.csdn.net/hubianyu/article/details/39700367 单引号引的数据 是char类型的 双引号引的数据 是String类型的char定义 ...
- Luogu P3398 仓鼠找sugar
这还是一道比较好的树剖题(去你的树剖,LCA即可) 这里主要讲两种思路,其实都是很基本也很经典的 1 树链剖分 还是先讲一下这种算法吧,虽然写起来很烦(不过感觉写多了就习惯了,而且还有一种莫名的快感) ...