当前位置: 首页 > news >正文

记录一本设计模式的书

常用设计模式有哪些?

策略设计模式


这里有一个 策略模式 C# 示例代码

using System;
using System.Collections.Generic;namespace RefactoringGuru.DesignPatterns.Strategy.Conceptual
{// The Context defines the interface of interest to clients.class Context{// The Context maintains a reference to one of the Strategy objects. The// Context does not know the concrete class of a strategy. It should// work with all strategies via the Strategy interface.private IStrategy _strategy;public Context(){ }// Usually, the Context accepts a strategy through the constructor, but// also provides a setter to change it at runtime.public Context(IStrategy strategy){this._strategy = strategy;}// Usually, the Context allows replacing a Strategy object at runtime.public void SetStrategy(IStrategy strategy){this._strategy = strategy;}// The Context delegates some work to the Strategy object instead of// implementing multiple versions of the algorithm on its own.public void DoSomeBusinessLogic(){Console.WriteLine("Context: Sorting data using the strategy (not sure how it'll do it)");var result = this._strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });string resultStr = string.Empty;foreach (var element in result as List<string>){resultStr += element + ",";}Console.WriteLine(resultStr);}}// The Strategy interface declares operations common to all supported// versions of some algorithm.//// The Context uses this interface to call the algorithm defined by Concrete// Strategies.public interface IStrategy{object DoAlgorithm(object data);}// Concrete Strategies implement the algorithm while following the base// Strategy interface. The interface makes them interchangeable in the// Context.class ConcreteStrategyA : IStrategy{public object DoAlgorithm(object data){var list = data as List<string>;list.Sort();return list;}}class ConcreteStrategyB : IStrategy{public object DoAlgorithm(object data){var list = data as List<string>;list.Sort();list.Reverse();return list;}}class Program{static void Main(string[] args){// The client code picks a concrete strategy and passes it to the// context. The client should be aware of the differences between// strategies in order to make the right choice.var context = new Context();Console.WriteLine("Client: Strategy is set to normal sorting.");context.SetStrategy(new ConcreteStrategyA());context.DoSomeBusinessLogic();Console.WriteLine();Console.WriteLine("Client: Strategy is set to reverse sorting.");context.SetStrategy(new ConcreteStrategyB());context.DoSomeBusinessLogic();}}
}
 Output.txt: 执行结果
Client: Strategy is set to normal sorting.
Context: Sorting data using the strategy (not sure how it'll do it)
a,b,c,d,eClient: Strategy is set to reverse sorting.
Context: Sorting data using the strategy (not sure how it'll do it)
e,d,c,b,a

原文链接:C# 策略模式讲解和代码示例

仅供学习参考,如有侵权联系我删除

http://www.dtcms.com/a/294395.html

相关文章:

  • vue3与ue5通信-工具类
  • [C/C++内存安全]_[中级]_[安全处理字符串]
  • ctfshow pwn40
  • 保护板测试仪:守护电池安全的“幕后卫士”
  • 关于SPring基础和Vue的学习
  • Docker 容器中的 HEAD 请求缺失 header?从 Content-MD5 缺失聊起
  • 超声原始数据重构成B扫成像的MATLAB实现
  • 【AI News | 20250722】每日AI进展
  • now能减少mysql的压力吗
  • 【Android】用 ViewPager2 + Fragment + TabLayout 实现标签页切换
  • linux性能调整和故障排查
  • LeetCode热题100--24. 两两交换链表中的节点--中等
  • Linux文件——Ext2文件系统(3)_软硬链接
  • Ubuntu 1804 编译ffmpeg qsv MediaSDK libva 遇到的问题记录
  • #Linux内存管理# 详细介绍madvise函数的工作原理
  • Elasticsearch(ES)安装
  • 分布式电商系统:缓存策略、负载均衡与容灾方案
  • 解决 Electron 中 window.open 打开新窗口的各种“坑”
  • Python 程序设计讲义(6):Python 的基本用法——运算符与表达式
  • API 汇总:ONLYOFFICE 文档最近更新
  • 背包DP之0/1背包
  • 11-1 浅层神经网络及计算前向传播
  • 局部重要性注意力LIA,通过区域重要性图与门控机制实现高阶信息交互,自适应增强有用特征、抑制冗余信息,平衡模型性能与效率。
  • VR-Doh: 革新3D建模的虚拟现实体验
  • DPVR亮相青岛品牌日,崂山科创力量引领AI眼镜新浪潮
  • 基于PLC的轨检小车控制器设计
  • .NET-键控服务依赖注入
  • 【实战】Dify从0到100进阶--文档解读(13)API前端再开发
  • 苍穹外卖DAY11
  • 【LeetCode数据结构】栈和队列的应用——设计循环队列问题详解