Strategy strategy model (behavioral model)

Motivation

In the process of software construction, the algorithms used by some objects may be varied and often changed. If these algorithms are coded into the object, the object will become extremely complicated; and sometimes it is also a kind of algorithm that supports unused algorithms. Performance burden.

How to transparently change the algorithm of an object as needed at runtime? Decouple the algorithm from the object itself to avoid the above problems?

Intent

Define a series of algorithms, encapsulate them one by one, and make them interchangeable. This mode allows the algorithm to change independently of the customers who use it.

Structure (Structure)

among them,

Context: Use a ConcreteStrategy to configure and maintain a reference to the Strategy object.

ConcreteStrategy specific strategy class: encapsulates specific algorithms or behaviors and inherits from Strategy.

Strategy class: Strategy class, which defines the public interface of many supported algorithms.

Code

The Strategy class defines the public interface of all supported algorithms, which provides an abstract method of the algorithm

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

The ConcreteStrategy class encapsulates specific algorithms or behaviors and inherits from Strategy. The following is specific algorithm A. The code of specific algorithm B is the same as that of A, which rewrites the abstract methods defined in the Strategy abstract class.

    class ConcreteStrategyA : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法A实现");
        }
    }

Context context, use a ConcreteStrategy to configure, maintain a reference to the Strategy object

    class Context
    {
        Strategy strategy;
        //初始化时,传入具体的策略对象
        public Context(Strategy strategy)
        {
            this.strategy = strategy;
        }
        //上下文接口,根据具体的策略对象,调用其算法的方法
        public void ContextInterface()
        {
            strategy.AlgorithmInterface();
        }
    }

Client code

First instantiate a context

Context context;

Instantiate different specific strategies, when calling context.ContextInterface(), we will get different results

Instantiation strategy A

context = new Context(new ConcreteStrategyA());
context.ContextInterface();

Instantiation strategy B

context = new Context(new ConcreteStrategyB());
context.ContextInterface();

advantage

1. The strategy mode provides a family of algorithms. The proper use of inheritance can transfer common code to the parent class, thereby avoiding duplicate code.

2. Hide the implementation details of specific strategies (algorithms) from customers, completely independent of each other.

Disadvantage

1. The strategy mode requires customers to know all algorithm strategy classes in order to make the right choice.

2. There are many strategy classes in the strategy mode. Every time an algorithm strategy is added, a new strategy class will be generated. The strategy class can be designed to be shared, so that the strategy class can be used by different clients.

Guess you like

Origin blog.csdn.net/wtt15100/article/details/105937524