一,什么是策略模式?

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. OGG-00664

    参数SID写错了 GGSCI (t2) > edit param exta extract exta setenv (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK) se ...

  2. mysql查询时间戳转换

    mysql查询时间戳转换 SELECT FROM_UNIXTIME(create_time) FROM tablename; 更新时间为七天以后 UPDATE t_rebate_trade_item ...

  3. [POJ3694]Network(Tarjan,LCA)

    [POJ3694]Network Description A network administrator manages a large network. The network consists o ...

  4. bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+贪心+二进制

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4811 题解 我现在为什么都写一题,调一天啊,马上真的退役不花一分钱了. 考虑这道题的弱化版 N ...

  5. Fabric的使用总结

    环境说明 建议python版本:2.7 fabric版本:最新版(当前为1.10.2) fabric安装 通过pip.py的方式安装(详细可以在fabric官网了解) fabric执行py文件的方法, ...

  6. Java浏览器弹出下载框,多个文件导出压缩包

    项目里一直有这个功能,也一直没怎么注意,今天研究了一下 依据逻辑往下走: 首先是要下载的ajax的Java方法,只有返回值需要设定一下,其他的不用管: Map<String, Object> ...

  7. python学习笔记(十三)处理时间模块

    import time time.sleep(2)#等待几秒 时间的三种表现方式: 1.格式化好的时间 2018-1-14 16:12 2.时间戳 是从unix元年到现在所有的秒数 3.时间元组 想时 ...

  8. php中间件是什么

    php中间件(middleware)是一个闭包,而且返回一个闭包. 中间件为过滤进入应用的HTTP请求提供了一套便利的机制,可以分为前置中间件和后置中间件.常用于验证用户是否经过认证,添加响应头(跨域 ...

  9. JAVA学习纲要

    这份面试题,包含的内容了十九了模块:Java 基础.容器.多线程.反射.对象拷贝.Java Web 模块.异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Spring ...

  10. [APIO2013]道路费用

    题目描述 幸福国度可以用 N 个城镇(用 1 到 N 编号)构成的集合来描述,这些城镇 最开始由 M 条双向道路(用 1 到 M 编号)连接.城镇 1 是中央城镇.保证一个 人从城镇 1 出发,经过这 ...