“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
define
The policy pattern defines a series of algorithms and encapsulates each algorithm so that they are interchangeable without affecting the clients that use the algorithm.
Usage scenarios
- A system needs to dynamically select one of several algorithms, and each algorithm can be encapsulated into a specific policy class
- Multiple behaviors defined in a class can be used to replace conditional transitions and reduce hard coding
- Each algorithm or function in the system is independent of each other, and it is required to hide the implementation details of the algorithm from the customer
- Multiple classes differ only in the behavior they represent, and you can use the policy pattern to dynamically select the behavior to perform at run time
case
demand
According to different fruit flavors, make different cakes, such as apple and banana flavor cake, add apple flavor, then make apple flavor cake; Add banana flavor, then make banana flavor cake
Implementation scheme
Define an abstract class for making a cake
/** * cake making abstract class *@author:liyajie
* @createTime: 2022/2/24 10:53 *@version: 1.0 * /
public abstract class CakeHandler {
/** * make a cake *@author: liyajie
* @date: 2022/2/24 10:54
* @param
* @return void
* @exception:
* @update:
* @updatePerson: * * /
public abstract void makeCake(a);
}
Copy the code
Define the strategy class of making apple-flavored cake, inherit the abstract class of making cake, rewrite makeCake method
/** ** Apple cake strategy *@author:liyajie
* @createTimeSqlstate 2022/2/24: *@version: 1.0 * /
public class AppleCakeHandler extends CakeHandler{
@Override
public void makeCake(a) {
System.out.println("Making apple cake."); }}Copy the code
Define the strategy class of making banana flavor cake, inherit the abstract class of making cake, rewrite makeCake method
/** * Banana cake strategy *@author:liyajie
* @createTimeSqlstate 2022/2/24: *@version: 1.0 * /
public class BananaCakeHandler extends CakeHandler{
@Override
public void makeCake(a) {
System.out.println("Making a banana cake."); }}Copy the code
Define enumeration classes to create cake strategies
/** * make cake enumeration *@author:liyajie
* @createTime: 2022/2/24 10:57 *@version: 1.0 * /
public enum CakeEnum {
APPLE(AppleCakeHandler.class.getSimpleName(),new AppleCakeHandler()),
BANANA(BananaCakeHandler.class.getSimpleName(),new BananaCakeHandler());
private final String cakeType;
private final CakeHandler cakeHandler;
CakeEnum(String cakeType, CakeHandler cakeHandler){
this.cakeType = cakeType;
this.cakeHandler = cakeHandler;
}
// Match the policy class
public static CakeEnum match(String cakeType){
CakeEnum[] values = CakeEnum.values();
for (CakeEnum cakeEnum : values) {
if(cakeType.equals(cakeEnum.cakeType)){
returncakeEnum; }}return null;
}
public String getCakeType(a){
return cakeType;
}
public CakeHandler getCakeHandler(a){
returncakeHandler; }}Copy the code
Defining test classes
/** * Test class *@author:liyajie
* @createTime: 2022/2/24 11:07 *@version: 1.0 * /
public class Test {
public static void main(String[] args) { String cakeType = AppleCakeHandler.class.getSimpleName(); CakeEnum cakeEnum = CakeEnum.match(cakeType); CakeHandler cakeHandler = cakeEnum.getCakeHandler(); cakeHandler.makeCake(); cakeType = BananaCakeHandler.class.getSimpleName(); cakeEnum = CakeEnum.match(cakeType); cakeHandler = cakeEnum.getCakeHandler(); cakeHandler.makeCake(); }}Copy the code
Viewing test results
Project analysis
Through the implementation of this case, we can see that the hard coding of if–else is replaced by policy pattern + enumeration, and different policies are encapsulated in a separate implementation class, which prevents the problems of one policy from affecting other policies, improves the scalability of the system, and achieves the open-close principle to the maximum extent.
conclusion
- advantage
- Multi-conditional statements are hard coded and difficult to maintain, and using policy patterns can avoid them
- The policy pattern provides a set of algorithms that, with appropriate use of inheritance or implementation, can bring common code from the algorithm family into the parent class. This avoids duplicate code
- The policy pattern can provide different implementations of the same behavior, and the client can choose the appropriate one based on the specific business logic
- The policy mode perfectly fits the open and close principle, and can flexibly add, subtract, and modify new algorithms without modifying or less modifying source code
- The strategy pattern places the use of algorithms in the environment class, while the implementation of algorithms in the concrete policy class, which realizes the separation of the two
- disadvantage
- When the actual business logic is very complex, this results in many policy classes
- The client must use the right algorithm class at the right time to properly complete the business logic
trailer
Stay tuned for the decorator pattern in the next installment
Need source code can pay attention to the public number [review old know new Java], more work to share with you.