define

  • Strategic patterns are behavioral design patterns.
  • Implement a set of different policy classes that can be used interchangeably in various scenarios. Therefore, the policy mode focuses on the organization and invocation of the policy class, but does not focus on the specific implementation process of the corresponding policy.

Simple policy pattern code implementation

  • Start by creating a policy top-level interface
** @author: ZRH * @date: 2021/2/3 17:03 */ public interface Book<T> {** ** @param T */ void doSome(T T); }Copy the code
  • Create an implementation policy class
/** * @author: ZRH * @date: 2021/2/3 17:04 */ public class ChineseBook implements Book { @Override public void doSome(Object o) { System.err. Println (" read text, policy parameter = "+ o); }}Copy the code
/** * @author: ZRH * @date: 2021/2/3 17:03 */ public class EnglishBook implements Book { @Override public void doSome(Object o) { System.err. Println (" Read English book, policy parameter = "+ o); }}Copy the code
  • You need to create a controller class that provides the call policy class and decouples the call of the policy class from the actual scenario.
@author: ZRH * @date: 2021/2/3 17:05 */ public BookContextHandler {private Book Book; Public BookContextHandler(book book) {this.book = book; } /** @param object */ public void doSome(String object) {this.book.dosome (object); }}Copy the code
  • Test and running results
/** * @param args */ public static void main(String[] args) {Book englishBook = new EnglishBook(); Book chineseBook = new ChineseBook(); // Policy class handler executes policy new BookContextHandler(englishBook).dosome (" English "); new BookContextHandler(chineseBook).doSome("chinese"); }Copy the code
If you read English books, policy parameter = English If you read Chinese books, policy parameter = ChineseCopy the code

The actual scene

  • The policy pattern is a design pattern that is widely used in actual projects, such as JDK, Spring and other source code. There are also actual business requirements, such as commodity activity strategy, membership strategy, data source switch, third party channel switch and so on.

Advantages and disadvantages

  • advantages

    1. The advantage of the policy pattern lies in the decoupling of the corresponding algorithm policies. The implementation of each policy is a separate class, which conforms to the single responsibility principle.
    2. When a new policy is created, a new policy class is added, which does not affect the original policy class and complies with the principle of closing for modification and opening for expansion.
    3. The implementation of the policy algorithm is hidden, and the user only needs to invoke the corresponding policy without knowing the implementation details.
    4. Duplicate code can be placed in the policy top-level interface, reducing duplicate code.
  • disadvantages

    1. Increasing the complexity of the project and the number of classes increases the JVM class load and makes the project larger.
    2. To use the policy mode, users need to know all the policy implementations before they can correctly choose the specific policy class.
  • Learn with an open mind and make progress together