Strategy pattern of design pattern

Time:2020-11-21

Strategy

Intro

Strategy: it defines the algorithm family and encapsulates them separately, so that they can be replaced with each other. This mode makes the changes of algorithm not affect the algorithm usedContext

Policy pattern is a method to define a series of algorithms. From the concept, all these algorithms are the same work, but the implementation is different. It can call all algorithms in the same way, reducing the coupling between various algorithm classes and using classes.

Usage scenarios

Policy pattern is used to encapsulate algorithms, but in practice, it can also be used to encapsulate almost any type of rules. As long as you hear that different business rules need to be applied at different times in the analysis process, you can consider the possibility of using the policy pattern to handle this change.

advantage

  • The coupling between specific algorithm and algorithm class is reduced
  • The strategy class layer of the policy pattern isContextA series of reusable algorithms or behaviors are defined, and inheritance is helpful to extract common functions in these algorithms
  • Unit testing is simplified because each algorithm has its own class and can be tested independently through its own interface

Sample

public class Context
{
    private readonly Strategy _strategy;

    public Context(Strategy strategy) => _strategy = strategy;

    public void Implement()
    {
        _strategy.AlgorithmImplement();
    }
}

public abstract class Strategy
{
    public abstract void AlgorithmImplement();
}

public class ConcreteStrategyA : Strategy
{
    public override void AlgorithmImplement()
    {
        Console.WriteLine ("algorithm a implementation");
    }
}

public class ConcreteStrategyB : Strategy
{
    public override void AlgorithmImplement()
    {
        Console.WriteLine ("algorithm B implementation");
    }
}

public class ConcreteStrategyC : Strategy
{
    public override void AlgorithmImplement()
    {
        Console.WriteLine ("algorithm C implementation");
    }
}

More

The factory pattern is the creation and use of decoupled objects, and the observer pattern is to decouple the observer and the observed. Similar to the two, policy pattern can also decouple. However, it decouples the definition, creation and use of policies.

Policy patterns are used to decouple the definition, creation and use of policies. In fact, a complete strategy model is composed of these three parts.

The definition of policy class is relatively simple, including a policy interface and a group of policy classes that implement this interface.
The creation of policy is completed by the factory class, which encapsulates the details of policy creation.
The policy pattern includes a set of optional policies. There are two methods to determine which policy to use for client code: compile time static determination and runtime dynamic determination. Among them, “runtime dynamic determination” is the most typical application scenario of policy pattern

If the if else branch judgment is not complicated and the code is not too much, there is no problem. After all, if else branch judgment is a syntax provided by almost all programming languages, and there is a reason for its existence. The best design is to follow KISS principle and how to make it simple. We have to use the strategy model to create more than n categories, but it is an over design.

Policy creation can also be combined with dependency injection to create or obtain policies, which can be used flexibly.

Reference

  • https://github.com/WeihanLi/DesignPatterns/tree/master/BehaviorPattern/StrategyPattern