一,什么是策略模式?

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. python-使用list和tuple

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...

  2. 一个奇怪的问题:Last_Errno: 1264 Error 'Out of range value for column 0x322E36343030

    场景环境: 1. 主从都是:Server version: 5.7.16-log MySQL Community Server (GPL) 2.操作系统:CentOS release 6.7 (Fin ...

  3. Synchronized锁升级

    Synchronized锁升级 锁的4中状态:无锁状态.偏向锁状态.轻量级锁状态.重量级锁状态(级别从低到高) 为什么要引入偏向锁? 因为经过HotSpot的作者大量的研究发现,大多数时候是不存在锁竞 ...

  4. Linux下git安装配置

    一.Linux下git安装配置 2013-07-28 20:32:10|  分类: 默认分类 |  标签:linux  git  server  |举报|字号 订阅     http://abomby ...

  5. vue项目中router路由配置

    介绍 路由:控制组件之间的跳转,不会实现请求.不用页面刷新,直接跳转-切换组件>>> 安装 本地环境安装路由插件vue-router:    cnpm install vue-rou ...

  6. python学习笔记(十一)处理json

    json串就是字符串,json串里必须是双引号 d={'car':{'color':'red','price':100,'count':50}, '爱分叉':{'color':'red','price ...

  7. nginx之安装

    1.简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄 ...

  8. Android逆向之旅---解析编译之后的Resource.arsc文件格式

    一.前言 快过年了,先提前祝贺大家新年快乐,这篇文章也是今年最后一篇了.今天我们继续来看逆向的相关知识,前篇文章中我们介绍了如何解析Android中编译之后的AndroidManifest.xml文件 ...

  9. mybatis获取数据库自增id

    http://blog.csdn.net/dyllove98/article/details/8866357 http://www.iteye.com/problems/86864 insert标签中 ...

  10. C#如何获取系统downloads和documents路径

    https://stackoverflow.com/questions/7672774/how-do-i-determine-the-windows-download-folder-path 如果你通 ...