策略模式

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.");
        }
    }
Strategy
复制代码
复制代码
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();
        }
    }
Context
复制代码
复制代码
var context = new StrategyContext();
context.SetStrategy(new ConcreteStrategyA());
context.ContextInterface();
context.SetStrategy(new ConcreteStrategyB());
context.ContextInterface();
Client
复制代码

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.

 

posted @   云霄宇霁  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示