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 介绍 ...
随机推荐
- 记一次 gunicorn 启动 flask 出问题的经历
出错现象: gunicorn+nginx+flask 部署项目, 部署过程没问题,项目也正常启动了,但是一旦访问接口,就会报错: Traceback (most recent call last): ...
- Java反编译工具Luyten-0.5.3
Luyten是一款很强大的反编译工具包,是一款github的开源工具,软件功能非常强大,界面简洁明晰.操作方便快捷,设计得很人性化. 工具软件下载路径:https://github.com/death ...
- Promise.race 的原理
// race的原理 Promise.race = function(values){ return new Promise((resolve,reject)=>{ for(let i = 0 ...
- bootstrap模态框模板代码
模态框模板 模板代码 <!-- 添加员工的模态框 start --> <div class="modal fade" id="empAddModal&q ...
- 06.队列、python标准库中的双端队列、迷宫问题
class QueueUnderflow(ValueError): """队列为空""" pass class SQueue: def __ ...
- 【leetcode】307. Range Sum Query - Mutable
题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...
- python学习笔记(十)常用模块
import os print(os.getcwd())#取当前工作目录,绝对路径 print(os.chdir("../"))#更改当前目录,.代表当前目录,..代表上一级目录 ...
- OC + RAC(七) RACSubject和RACSignal的区别
-(void)_test8{ /// RACSubject继承自RACSignal 但是RACSubject和RACSignal的区别? //1能接收1,2 //但是2只能接收2 RACSubject ...
- [CSP-S模拟测试]:凤凰院凶真(LCIS)
题目描述 $\alpha$世界线.凤凰院凶真创立了反抗$SERN$统治的组织“瓦尔基里”.为了脱离$\alpha$线,他需要制作一个世界线变动率测量仪.测量一个世界线相对于另一个世界线的变动率,实质上 ...
- java微信扫码支付Native(模式二)
官方开发文档模式二的地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5 pom文件的依赖: <?xml versio ...