C#设计模式:策略者模式(Stragety Pattern)
一,什么是策略模式?
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)的更多相关文章
- 20.策略者模式(Stragety Pattern)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 状态模式(State Pattern)
原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...
- 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)
原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) 作 ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)
原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...
- 乐在其中设计模式(C#) - 外观模式(Facade Pattern)
原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...
随机推荐
- IDEA中项目引入独立包打包失败问题解决(找不到包)
在terminal中执行以下命令:mvn install:install-file -DgroupId=ocx.GetRandom -DartifactId=GetRandom -Dversion=1 ...
- MacOS系統Flutter打包apk
一.打包前需要做一些基本设置的确认 1.应用名 2.权限设置 3.applicationId:应用唯一标识符 4.versionCode:版本号 5.versionName:版本名称 6.APP应用图 ...
- 使用Node,Vue和ElasticSearch构建实时搜索引擎
(译者注:相关阅读:node.js,vue.js,Elasticsearch) 介绍 Elasticsearch是一个分布式的RESTful搜索和分析引擎,能够解决越来越多的用例. Elasticse ...
- django之模型类在视图中的应用
一:模型类直接从把前端表单传入的值,进行存储. @csrf_exempt def regist(request): if request.method == 'POST': form = UserFo ...
- pandas.DataFrame.drop_duplicates 用法说明
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False) subset考虑重复发生在哪一列,默认考虑所有列,就是在任何一列 ...
- 阿里云Kubernetes服务 - Service Broker快速入门指南
4月底阿里云容器服务上线了基于Kubernetes集群的服务目录功能.阿里云的容器的服务目录遵循Open Service Broker API标准,提供了一系列的服务代理组件,实现了对主流开源服务如M ...
- 在Mac电脑上使用NTFS移动硬盘遇到问题
1.sudo nano/etc/fstab 回车 输电脑密码 2.进入文本插入页面 编入: LABEL=硬盘名字 NONE ntfs rw,auto,nobrowse 3.ctrl + X 退出 选 ...
- Strassen __128int
题目链接 题意思路很简单,递归求最小就好了.但__128int没见过.故写博客记下.__128int如果输入输出就要自己写函数. #include<bits/stdc++.h> using ...
- [CSP-S模拟测试]:引子(大模拟)
题目描述 网上冲浪时,$Slavko$被冲到了水箱里,水箱由上而下竖直平面.示意图如下: 数字$i$所在的矩形代表一个编号为$i$的水箱.1号水箱为水箱中枢,有水管连出.除了$1$号水箱外,其他水箱上 ...
- Python分布式爬虫打造搜索引擎完整版-基于Scrapy、Redis、elasticsearch和django打造一个完整的搜索引擎网站
Python分布式爬虫打造搜索引擎 基于Scrapy.Redis.elasticsearch和django打造一个完整的搜索引擎网站 https://github.com/mtianyan/Artic ...