策略模式
The Strategy design pattern defines a familiy of algorithms, encapsulate each one, and make them interchangeable. This pattern lets the algorithm vary independently from client that use it.
策略模式定义一系列算法,封装它,使他们可以互换,这种设计模式使算法独立于客户端的使用。
The Strategy Design Pattern is used when we have multiple algorighms(solutions) for a specific task and the client decides on the actual implementation to be used at runtime. In simple words, we can say that the Strategy Design Pattern (also called Policy Pattern) attempts to solve the issue when you need to provide multiple solutions for the same problem so that one can be selected at runtime.
UML Class Diagram
Strategy: declares an interface common to all supported algorithms.Context uses this interface to call the algorithm defined by a ConcreteStrategy.
ConcreteStrategy: There are going to be concrete classes and they must implement the Strategy interface and provide implementations for the algorighm.
Context: This is going to be a class that maitains a reference to a Strategy object, and then uses that reference to call the algorithm defined by a particular ConcreteStrategy.
Structure Code IN C#

ublic interface IStrategy { void AlgorithmInterface(); } public class ConcreteStrategyA : IStrategy { public void AlgorithmInterface() { Console.WriteLine($"{typeof(ConcreteStrategyA).Name} action."); } } public class ConcreteStrategyB : IStrategy { public void AlgorithmInterface() { Console.WriteLine($"{typeof(ConcreteStrategyB).Name} action."); } }

public class StrategyContext { private IStrategy? _strategy; public StrategyContext() { } public StrategyContext(IStrategy strategy ) { this._strategy = strategy; } public void SetStrategy(IStrategy strategy ) { this._strategy = strategy; } public void ContextInterface() { _strategy?.AlgorithmInterface(); } }

var context = new StrategyContext(); context.SetStrategy(new ConcreteStrategyA()); context.ContextInterface(); context.SetStrategy(new ConcreteStrategyB()); context.ContextInterface();
When do we need to use the Strategy Design Pattern in Real-Time Applications?
- When there are multiple solutions for a given task and the selection criteria of a solution are defined at run-time.
- When you want different variants of an algorithm
- When many related classes differ only in their behavior.
- When a class defines many behaviors and these appear as multiple conditional statements in tis operations. Instead of many conditonal statements, move-related conditonal branches into their own strategy class.
- When an algorithm uses data that the client shouldn't know about. Use the Strategy Design Pattern to avoid exposing complex and algorithm-specific data structures.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)