C#设计模式系列:策略模式(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)策略模式造成很多的策略类。
C#设计模式系列:策略模式(Strategy)的更多相关文章
- 乐在其中设计模式(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的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化. 策略模 ...
- 设计模式 笔记 策略模式 Strategy
//---------------------------15/04/28---------------------------- //Strategy 策略模式----对象行为型模式 /* 1:意图 ...
- 大熊君说说JS与设计模式之------策略模式Strategy
一,总体概要 1,笔者浅谈 策略模式,又叫算法簇模式,就是定义了不同的算法,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式和工厂模式有一定的类似,策略模式相对简单容易理解,并 ...
- 设计模式之策略模式Strategy
/** * 策略设计模式 * 策略模式:定义一系列的算法族,使他们之间可以相互转换,动态的改变其行为. * 问题:设计一个鸭子模拟游戏. * 现在有一群鸭子: * ①这些鸭可以有飞的行为(分为快和慢) ...
- 大话设计模式之策略模式(strategy)
策略模式:它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响使用算法的用户. 针对商城收银模式,打折,返现促销等的例子: 打折还是促销其实都是一些算法,可以用工厂模式来 ...
随机推荐
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- eclipse安装hibernate-Tools
启动eclipse 选择Help -> About Eclipse 记住自己的eclipse版本 访问http://download.jboss.org/jbosstools/updates/s ...
- mac osx 上面部署Django项目 apache+mysql+mod_wsgi
1.安装Xcode command line tools 首先,编译mysql和Homebrew需要用到Xcode command line tools,所以首先安装command line tool ...
- AR初体验:宣传G20
最近Pokemon Go太火,它基于LBS(Location Based Service)+AR(Augmented Reality)的一款游戏,这股风,一定会让国内的公司纷纷效仿,你懂的.不可否定的 ...
- python使用pdkdf2加盐密码
from werkzeug.security import generate_password_hash, check_password_hash pw = generate_password_has ...
- About_PHP读写文件
1.PHP部分文件操作函数:fopen ,fread ,filesize,fwrite,fclose 2.unlink() rmdir() 删除函数 unlink() 删除文件函数:unlink(路径 ...
- 最长下降子序列O(n^2)及O(n*log(n))解法
求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...
- ant的安装及项目的发布
1.安装ant1) 直接解压apache-ant-1.9.7-bin 2) 在环境变量中配置,ant_home的环境变量在 3) 在命令提示符中测试是否安装成功. 2 项目首次打包1) 写好打包的配置 ...
- c/c++头文件_string
string, cstring, string.h 一.string头文件 主要包含一些字符串转换的函数 // sto* NARROW CONVERSIONS// sto* WIDE CONVERSI ...
- JQM (功能栏、导航条)
在Mobile中导航条的基本结构: <div data-role="navbar"> ul>li>a </div> 其中含有“行(grid)”和 ...