设计模式笔记:策略模式(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)
策略模式:它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响使用算法的用户. 针对商城收银模式,打折,返现促销等的例子: 打折还是促销其实都是一些算法,可以用工厂模式来 ...
随机推荐
- 深入浅出的webpack构建工具---AutoDllPlugin插件(八)
深入浅出的webpack构建工具---AutoDllPlugin插件(八) DllPlugin插件能够快速打包,能把第三方依赖的文件能提前进行预编译打包到一个文件里面去.提高了构建速度.因为很多第三方 ...
- 2018AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions
论文标题:AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions 来源/作者机构情况: 谷歌,http:// ...
- QT写TXT文件
#include <QDir> //头文件 QDir *TEST = new QDir; bool exist = TEST->exists("TEST") ...
- 错误:java.io.FileNotFoundException: /storage/emulated/0/Documents/eclipse-inst-win64.exe
在Android服务的最佳实例中:https://www.cnblogs.com/hh8888-log/p/10300972.html,在最后运行的时候,点击Start Download按钮总是会报一 ...
- 学习CSS布局 - position
position 为了制作更多复杂的布局,我们需要讨论下 position 属性. 它有一大堆的值,名字还都特抽象,别提有多难记了. 让我们先一个个的过一遍,不过你最好还是把这页放到书签里. 先看下运 ...
- timeout可以实现当一个命令在规定时间内不返回就强制返回的功能 + 杀毒安装ClamAV nmap 速度 比Telnet 快
[root@xiaowei ~]# cat telnetport.sh #!/bin/bash Port=25223 timeout 2 ssh root@127.0.0.1 "telnet ...
- 解析LED发光效率
解析LED发光效率 来源:--作者:--浏览:532时间:2016-08-10 14:18 关键词: 发光效率为评测光源效率的指标,用光源发出的光通量 (lm)与向光源输入的电力(W)之比表示.单位为 ...
- Elasticsearch5.5.1插件开发指南
Elasticsearch5.5.1插件开发指南 原文地址: https://www.elastic.co/guide/en/elasticsearch/plugins/5.5/plugin-auth ...
- Jmeter(三十五)_精确实现网页爬虫
Jmeter实现了一个网站文章的爬虫,可以把所有文章分类保存到本地文件中,并以文章标题命名 它原理就是对网页提交一个请求,然后把返回的所有值提取出来,利用ForEach控制器去实现遍历.下面来介绍一下 ...
- asp.net mvc接收安卓post的json字符串
筛选器: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syst ...