Policy mode: Different processing policies are used for different situations and scenarios. Different policies deal with different logic.

For example; In the THREAD pool configuration parameters of the JDK, there is a reject policy parameter. This rejection policy is the last insurance mechanism of the thread pool. It will trigger the rejection policy once the thread pool reaches the saturation of processing requests. The default rejection policy of the thread pool is AbortPolicy, in addition to the other three policies, as shown below:

These four rejection policies implement the RejectedExecutionHandler interface, which defines a rejectedExecution method where all specific rejection policies implement specific logic. As follows:

RejectedExecutionHandler is used as the argument type in the thread pool:

In combination with Richter’s substitution principle and polymorphism, a thread pool can pass in all RejectedExecutionHandler subclasses and exhibit different rejection behaviors.

Model analysis:

1, according to different scenarios and situations, develop different strategies to deal with. At the code level, one policy for each class, combined with Richter substitution and polymorphism, eliminates the need for if-else centralization.

This article was automatically published by ArtiPub