The examples in this article share with you the Java strategy model for your reference. The specific content is as follows
1. Strategy Pattern is a relatively simple model, also called Policy Pattern.
Definition is as follows:
Define a family of algorithms,encapsulate each one, and make them interchangeable.
(Define a set of algorithms, encapsulate each algorithm, and make them interchangeable.)
The general class diagram of the policy pattern is as follows:
Three roles of the policy model:
● Context encapsulation role, also known as context role, plays a role of encapsulation that connects the past and the future, blocks the direct access of high-level modules to policies and algorithms, and encapsulates possible changes.
● Strategy Abstract strategy role strategy and algorithm family abstraction, usually an interface, defines the methods and attributes that each strategy or algorithm must have,
● Concrete Strategy The specific strategy role implements operations in abstract policies, and this class contains specific algorithms.
The policy pattern is to pass a specific policy in the constructor of the encapsulated role and then execute this policy.
The general source code of the policy mode is as follows:
public class Test3 { public static void main(String[] args) { //Declare a specific strategy Strategy strategy = new ConcreteStrategy1(); //Declare the context object Context context = new Context(strategy); //Execute the encapsulated method context.doAnythinig(); } } interface Strategy { //Arithmetic rules of policy mode public void doSomething(); } class ConcreteStrategy1 implements Strategy { public void doSomething() { System.out.println("Arithmetic Rules for Specific Strategy 1"); } } class ConcreteStrategy2 implements Strategy { public void doSomething() { System.out.println("Arithmetic Rules for Specific Strategy 2"); } } class Context { //Abstract Strategy private Strategy strategy; //Constructor sets specific strategies public Context(Strategy _strategy){ this.strategy = _strategy; } //Encapsulated strategy method public void doAnythinig(){ this.strategy.doSomething(); } }Advantages of strategy model:
● The algorithm can be switched freely
This is defined by the policy model itself. As long as the abstract strategy is implemented, it becomes a member of the policy family, encapsulating it through encapsulation roles, ensuring that it is "freely switchable" strategy is provided to the outside world.
● Avoid using multiple conditions to judge
If there is no policy model, let's think about what it would look like? A strategy family has 5 strategy algorithms. You need to use strategy A at some time and then use strategy A at some time.
How to design strategy B? Use multiple conditional statements? Multi-conditional statements are not easy to maintain, and the probability of errors is greatly enhanced. After using the policy mode, other modules can decide which strategy to adopt. The access interface provided by the policy family to the outside is the encapsulation class, which simplifies operations and avoids conditional statement judgment.
● Good scalability
This doesn't even have to say what it has to do, because it's too obvious. It is too easy to add a strategy to the existing system. Just implement the interface and there is no need to modify anything else. It is similar to a plug-in that can be disassembled repeatedly, which greatly conforms to the OCP principle.
Disadvantages of policy patterns:
● The number of policy classes increases. Each strategy is a class, and the possibility of reuse is very small, and the number of classes increases.
● All strategy classes need to be exposed
Use scenarios for policy mode:
● Multiple classes have only slightly different scenarios in algorithms or behaviors.
● Scenarios where the algorithm needs to be switched freely.
● Scenarios where algorithm rules need to be blocked.
2. Extension of policy mode - policy enumeration
The policy enumeration is defined as follows:
● It is an enum.
● It is an enumeration of concentrated strategy patterns.
Examples are as follows:
Question: Enter 3 parameters and perform addition and subtraction operations. Two of the parameters are int type, and the remaining parameter is String type. Only two symbols "+" and "-" can be selected. Don't consider any complex verification. What we are doing is a white box test, and what we enter is the standard int type and compliant String type.
import java.util.Arrays; public class Test3 { public static void main(String[] args) { //The two parameters entered are the number int a = Integer.parseInt(args[0]); String symbol = args[1]; //The symbol int b = Integer.parseInt(args[2]); System.out.println("The input parameter is: "+Arrays.toString(args)); System.out.println("The running result is: "+a+symbol+b+"="+Calculator.ADD.exec(a,b)); } } enum Calculator { //Add operation ADD("+"){ public int exec(int a,int b){ return a+b; } }, //Subtraction operation SUB("-"){ public int exec(int a,int b){ return a - b; } }; String value = ""; //Define member value type private Calculator(String _value){ this.value = _value; } //Get the value of enumeration members public String getValue(){ return this.value; } //Declare an abstract function public abstract int exec(int a,int b); }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.