Design Patterns "" _ policy mode

Definitions :

  Strategy Mode (Strategy Pattern), a class of behavior or algorithm can be changed at runtime. This type of design pattern belongs behavioral patterns. Each method of packaging, flexible calling methods required for processing business scenarios.

Scenario:

   1, if there is a system in which many classes, the difference between them only in their behavior, then use the strategy pattern allows an object can be dynamically select an action in a number of behavior.

   2, a system need to dynamically choose one of several algorithms.

   3. If an object has a lot of behavior, if that is not appropriate mode, these acts had to use multiple conditional selection statement to achieve.

   Here that the algorithm is not "bubble", "fast" .. kind of algorithm, for a period of code to solve some business scenarios, operation.

Common situations:

  If..else multiple scenes, meet the conditions simply execute a certain method, and more difficult to maintain if..else brought, as well as the business changes brought about code changes.

 

Practical examples:

  

Operation Interface:

public interface Operation {

    public int doCompute(int num1,int num2);
}

Implementation class, addition and subtraction:

public class OperationSub implements Operation {
    @Override
    public int doCompute(int num1, int num2) {
        return num1 - num2;
    }
}
public class OperationAdd implements Operation {
    @Override
    public int doCompute(int num1, int num2) {
        return num1 + num2;
    }
}

Calculators categories:

public class Calculator {
        private Operation operation;

        private void setOperation(Operation operation){
                this.operation = operation;
        }

        private int doOperation(int num1,int num2){
                return this.operation.doCompute(num1,num2);
        }   
}

Test categories:

public static void main(String[] args) {
                Calculator calculator = new Calculator();
                calculator.setOperation(new OperationAdd());
                int result = calculator.doOperation(21,4);
                System.out.println(result);
        }

If necessary add other methods, it is necessary to create a new class that implements the interface Operation then serOperation (new ...).


Advantages:

  follow the principle of opening and closing, good scalability.
Disadvantages:
  1. With your strategy to increase your class will be more and more.
  2. All of the Strategy should be exposed to, so if you use a strategy pattern in the actual development, we must remember to write the document so that your partner know which strategies have been. Like Shiro default provides three authentication strategy, it must be clearly written in the document, otherwise we do not know how to use.

To write articles based on personal understanding, if wrong, please correct me.
Source: https: //github.com/RecursionHs/uareright/tree/master/design_mode

Guess you like

Origin www.cnblogs.com/recurision/p/11127336.html