This article describes the principle and usage of Java design pattern. Share it for your reference, as follows:
The policy pattern defines a series of algorithms and encapsulates each algorithm, making them replaceable with each other. The policy pattern allows the algorithm to change independently from the customers using it. Among them, the TreeSet class and TreeMap class in JDK use the policy pattern. These two classes are sorted collection classes, and the sorting rules are equivalent to a series of algorithms defined in the policy pattern, and the collection class is equivalent to an environment class in the policy pattern for users to use. Only knowing that TreeSet and TreeMap are sorted. As for how to sort, it is determined by the sorting algorithm.
The strategy model consists of three parts: abstract policy role, specific policy role, and environmental role.
Abstract policy role : a policy class, usually implemented by an interface or abstract class.
Specific strategy role : The abstract strategy class is implemented and related algorithms and behaviors are packaged.
Environment role : Holds a reference to a policy class and is eventually called to the client.
Here is a simple policy pattern code example:
1. Abstract strategy role : define an interface Strategy.
package com.test;/** * Strategy pattern abstract role* @author zhouwen * */public interface Strategy { public int calculate(int a, int b);}2. Specific strategy role : Implement the Strategy interface. In the example, the operation of addition, subtraction, multiplication and division is implemented.
package com.test;/** * Policy mode specific strategy class* Implement addition operations* @author zhouwen * */public class AddStrategy implements Strategy { @Override public int calculate(int a, int b) { return a + b; }} package com.test;/** * Policy mode specific strategy class* Implement subtraction operation* @author zhouwen * */public class SubtractStrategy implements Strategy { @Override public int calculate(int a, int b) { return a - b; }} package com.test;/** * Policy mode specific strategy class* Implement multiplication operation* @author zhouwen * */public class MultiplyStragety implements Strategy { @Override public int calculate(int a, int b) { return a * b; }} package com.test;/** * Policy mode specific strategy class* Implement division operation* @author zhouwen * */public class DivideStrategy implements Strategy { @Override public int calculate(int a, int b) { return a / b; }}3. Environment role : Define an abstract role reference in the environment role and expose the calculation method for the client to call.
package com.test;/** * Policy mode environment class* @author zhouwen * */public class Environment { private Strategy strategy; public Environment(Strategy strategy){ this.strategy = strategy; } //Use to change the policy public void setStrategy(Strategy strategy){ this.strategy = strategy; } public Strategy getStrategy(){ return strategy; } //Expose the computing interface for the client to call public int calculation(int a, int b){ return strategy.calculate(a, b); }}4. Finally write the client code:
package com.test;public class Client { public static void main(String[] args) { AddStrategy addStrategy = new AddStrategy(); Environment environment = new Environment(addStrategy); System.out.println(environment.calculate(3, 4)); SubtractStrategy subStrategy = new SubtractStrategy(); environment.setStrategy(subStrategy); System.out.println(environment.calculate(3, 4)); MultiplyStragety multiplyStrategy = new MultiplyStragety(); environment.setStrategy(multiplyStrategy); System.out.println(environment.calculate(3, 4)); DivideStrategy divideStrategy = new DivideStrategy(); environment.setStrategy(divideStrategy); System.out.println(environment.calculate(3, 4)); }}Summarize:
With the client's System.out.println(environment.calculate(3, 4)); this code, we do not know which implementation class is called. Only by knowing the specific policy class passed to the environment role, we know which class to call. This can achieve the separation of the client and the specific policy algorithm, making the program easy to switch, understand, and expand.
Advantages and disadvantages of strategy model:
advantage:
1. The purpose of the policy pattern is to encapsulate each algorithm into an independent class with a common interface for a set of algorithms, so that they can be replaced by each other. As provided in the example, the policy is replaced by the set method.
2. The policy pattern allows the algorithm to change without affecting the client. Use policy patterns to separate behavior from environment.
3. The environment class is responsible for maintaining and querying behavior classes, and various algorithms are provided in specific strategies. Since the algorithm and the environment are independent, the modification of the algorithm will not affect the environment and the client.
shortcoming:
1. The client must know all policy classes and decide which policy class to use at its own discretion.
2. It will cause many strategies.
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.