The strategy pattern

Definition: defines a set of algorithms, encapsulates each algorithm, and converts them to each other

Type: Behavior class pattern

Class diagram:

It can be seen from the class diagram that the actual algorithm can be accessed by accessing the Context. In actual development, Strategy can be defined as abstract class if there are repetitive logic codes in the algorithm, after all, the purpose of design is to reduce repetitive codes.

The structure of the policy pattern:

Encapsulating classes: Also called contexts, encapsulate policies twice to avoid direct calls to policies by higher-level modules

Abstract strategy:

The policy pattern looks more like the template method pattern when the common code is encapsulated by abstract classes when there is repeated logic in each implementation class.

Concrete policy: The concrete policy role is typically filled by a set of classes that encapsulate the algorithm and are freely interchangeable as needed

AlgorithmInterface (); // algorithmInterface(); } // AdapteStrategya implements Strategy {private double price; public ConcreteStrategyA(double price){ this.price = price; } @Override public doublealgorithmInterface() {

        returnthis.price; }} // Class ConcreteStrategyB implements Strategy{private double moneyCondition; private double moneyReturn; public ConcreteStrategyB(double moneyCondition ,double moneyReturn){ this.moneyCondition = moneyCondition; this.moneyReturn = moneyReturn; } @Override public doublealgorithmInterface() {
        returnmoneyCondition * moneyReturn; }} c class ConcreteStrategyC implements Strategy {private double moneyCondition; private double moneyReturn; private double money; @Override public doublealgorithmInterface() {

        if(money >= moneyCondition){
            return money -  moneyReturn;
        }
        returnmoney; }} /** * class ContextStrategy {private Strategy Strategy; public Strategy contextInterface(Strategy strategy){return this.strategy = strategy;
    }

    public void execute(){ strategy.algorithmInterface(); }}Copy the code


Advantages and disadvantages of strategic patterns

Advantages:

  • Since the same abstraction is implemented, all can be freely transformed
  • Easy to extend, only need to implement the same interface can add corresponding policies

Disadvantages:

  • Policy maintenance problems, if there are too many policy algorithms to maintain tedious
  • Scenario callers and algorithms using the policy pattern must be transparent, and the implementation of the policy is meaningless if not understood

Applicable scenario

The strategic pattern of object-oriented design is essentially inheritance and polymorphism in object-oriented, after looking at the general code of the strategic pattern, I thought, even if I had never heard of the strategic pattern before, I must have used it in the development process? There are at least two situations where you can consider using the policy pattern,

A situation in which several classes share the same main logic but differ slightly in the algorithm and behavior of some of the logic. There are several similar behaviors, or algorithms, and the client needs to dynamically decide which one to use, so the policy pattern can be used to encapsulate these algorithms for the client to invoke. Policy mode is a simple and commonly used mode. We often use it intentionally or unintentionally during development. Generally speaking, policy mode is not used alone, but mixed with template method mode, factory mode and so on.


Resources: W3C