Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Policy pattern to realize condition judgment in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use the strategy pattern to achieve condition judgment in Java". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the strategy pattern to achieve condition judgment in Java" can help you solve your doubts.

Define

The policy pattern defines a series of algorithms and encapsulates each algorithm so that they can replace each other, and the changes in the algorithm will not affect the client that uses the algorithm.

Working with scen

A system needs to dynamically choose one of several algorithms, and each algorithm can be encapsulated into a specific policy class.

Multiple behaviors are defined in a class, which can replace conditional transfer statements and reduce hard coding.

When the algorithms or functions in the system are independent of each other and are required to hide the implementation details of the algorithm from the customer

Multiple classes only show differences in behavior. You can use the policy pattern to dynamically select the behavior to be executed at run time.

Case requirement

Make different cakes according to different fruit tastes, such as apple-flavored and banana-flavored cakes, and banana-flavored cakes

Realization scheme

Define abstract classes for making cakes

/ * author:liyajie * @ createTime:2022/2/24 10:53 * @ version:1.0 * / public abstract class CakeHandler {/ * make cakes * @ author:liyajie * @ date: 2022-2-24 10:54 * @ param * @ return void * @ exception: * @ update: * @ UpdatePerson: * * / public abstract void makeCake () }

Define the strategy class for making apple cake, inherit the abstract class for making apple cake, and rewrite the makeCake method.

/ * * Apple cake making strategy * @ author:liyajie * @ createTime:2022/2/24 10:55 * @ version:1.0 * / public class AppleCakeHandler extends CakeHandler {@ Override public void makeCake () {System.out.println ("making apple cake");}}

Define the strategy class for making banana-flavored cake, inherit the abstract class for making cake, and rewrite the makeCake method.

/ * * Banana cake making strategy * @ author:liyajie * @ createTime:2022/2/24 10:55 * @ version:1.0 * / public class BananaCakeHandler extends CakeHandler {@ Override public void makeCake () {System.out.println ("making banana egg cakes");}}

Define enumeration classes to make cake strategies

/ * enumeration of cake making * @ 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 } / / matching policy class public static CakeEnum match (String cakeType) {CakeEnum [] values = CakeEnum.values (); for (CakeEnum cakeEnum: values) {if (cakeType.equals (cakeEnum.cakeType)) {return cakeEnum;}} return null;} public String getCakeType () {return cakeType } public CakeHandler getCakeHandler () {return cakeHandler;}}

Define test classes

/ * 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 ();}}

View test results

Scheme analysis

Through the implementation of this case, we can see that the policy pattern + enumeration is used to replace the hard coding of if--else, and different policies are encapsulated into separate implementation classes, which prevents the problems of one policy from affecting other policies, improves the scalability of the system, and realizes the opening and closing principle to the greatest extent.

Summary

advantage

Multiple conditional statements are hard-coded and difficult to maintain, but the use of policy mode can avoid using multiple conditional statements

The policy pattern provides a series of algorithms, using inheritance or implementation appropriately, to add common code in the algorithm family to the parent class. This avoids duplicate code

Policy patterns can provide different implementations of the same behavior, and clients can choose appropriate ones according to specific business logic.

The strategy pattern fits perfectly with the opening and closing principle, and the new algorithm can be flexibly added, reduced and modified without modifying or less modifying the source code.

The policy pattern puts the use of the algorithm in the environment class, and the implementation of the algorithm in the specific policy class realizes the separation of the two.

Inferior position

When the actual business logic is very complex, it will lead to the emergence of many policy classes

The client must use the appropriate algorithm class at the right time in order to reasonably complete the business logic.

After reading this, the article "how to use the strategy pattern to achieve condition judgment in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report