策略模式

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 @ 2023-06-03 21:31  云霄宇霁  阅读(2)  评论(0编辑  收藏  举报