一,什么是策略模式?

1,针对同一命令或行为,不同的策略做不同的动作。

2,比如针对一组算法,将每个算法封装到具有公共接口的独立的类中,从而使它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。

二,如下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace _19.策略模式
{
/// <summary>
/// 策略模式是对算法的包装,是把使用算法的责任和算法本身分割开,委派给不同的对象负责。
/// 策略模式通常把一系列的算法包装到一系列的策略类里面。
/// 用一句话慨括策略模式就是——“将每个算法封装到不同的策略类中,使得它们可以互换”。
/// </summary>
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("输入第一个数字(整数):");
string type = Console.ReadLine();
int iType = ;
if (!int.TryParse(type, out iType))
{
Console.WriteLine("输入数字无效,请重新输入");
continue;
}
///策略的调用
StrategyContext context = new StrategyContext();
context.SetStrategy(StrategyFortory.CreateInstance((StrategyType)iType));
context.Setup();
}
}
}
/// <summary>
/// 使用工厂模式管理策略
/// </summary>
public class StrategyFortory
{
public static AbstractStrategy CreateInstance(StrategyType type)
{
AbstractStrategy abstractStrategy = null;
switch (type)
{
case StrategyType.Chinese:
abstractStrategy = new ChineseStrategy();
break;
case StrategyType.Usa:
abstractStrategy = new UsaStrategy();
break;
}
return abstractStrategy;
}
}
/// <summary>
/// 抽象策略
/// </summary>
public abstract class AbstractStrategy
{
public abstract void Setup();
}
/// <summary>
/// 具体策略
/// </summary>
public class ChineseStrategy : AbstractStrategy
{
public override void Setup()
{
Console.WriteLine("中国人");
}
}
/// <summary>
/// 具体策略
/// </summary>
public class UsaStrategy : AbstractStrategy
{
public override void Setup()
{
Console.WriteLine("美国人");
}
}
/// <summary>
/// 策略的使用
/// </summary>
public class StrategyContext
{
AbstractStrategy strategy = null;
public void SetStrategy(AbstractStrategy strategy)
{
this.strategy = strategy;
}
public void Setup()
{
this.strategy.Setup();
}
}
/// <summary>
/// 策略枚举
/// </summary>
public enum StrategyType
{
Chinese = ,
Usa =
}
}

C#设计模式:策略者模式(Stragety Pattern)的更多相关文章

  1. 20.策略者模式(Stragety Pattern)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

  3. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

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

  4. 乐在其中设计模式(C#) - 状态模式(State Pattern)

    原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...

  5. 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

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

  6. 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

    原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...

  7. 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)

    原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) 作 ...

  8. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  9. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)

    原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...

  10. 乐在其中设计模式(C#) - 外观模式(Facade Pattern)

    原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...

随机推荐

  1. VPS建站

    参考腾讯云的教程 选择了 LAMP的方案,即Linux + Apache + MySQL + Php 参考链接 https://cloud.tencent.com/edu/learning/cours ...

  2. 一、ARM

    1.1 ARM 分类 1.1.1 版本号分类 以前分类的是 ARM7,ARM9... ARM11,在 ARM11 之后,就是以 Cortex 系列分类了: Cortex-R:应用在实时系统上的系列 C ...

  3. springboot cache---本地缓存的使用

    使用缓存的几个注解 什么时候需要使用缓存呢?一般是在一个方法的返回值需要被频繁用到.但是返回值很少改变而且执行这个方法会消耗较多的时间,这种情况我们可以考虑将返回值暂时存到内存中,需要时通过对应的唯一 ...

  4. 雷哥带你了解webSocket原理

    偶然在知乎上看到一篇回帖,瞬间觉得之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比较喜欢看这种博客,读起来很轻松,不枯燥,没有布道师的阵仗 ...

  5. HDU 1373 XYZZY (spfa的特殊用法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意:有n个房间,编号1-n,房间之间有单向门连接.某人初始位于1号房间,且具有100点能量 ...

  6. C++ decltype

    #include <iostream> using namespace std; int main() { int ia{3}; decltype(ia) varr[3]={1,2,3}; ...

  7. 5G即将到来!我们需要一部怎样的手机呢?

    随着5G技术研发试验即将于年底宣告完成,也就意味着2019年起,5G商用将会宣布启动,现在OPPO.vivo.小米.华为.一加等众多手机厂商也宣布启动5G计划,这时5G势必会掀起一股新鲜的血液,5G手 ...

  8. 《Javascript设计模式与开发实践》关于设计模式典型代码的整理:单例模式、策略模式、代理模式、迭代器模式、发布-订阅模式、命令模式、组合模式

    1.单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 使用闭包封装私有变量// 使用闭包创建单例var user = (function () { var _name = 'sven' ...

  9. Malformed UTF-8 characters, possibly incorrectly encoded 或中文乱码 (Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in)

    问题: Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in 是 ...

  10. Ceiling analysis

    Course note: Coursera Machine learning by Andrew Ng, 2014, week 10: Application example: photo OCR ( ...