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 Java Policy Mode instead of if else

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

Share

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

Editor to share with you how to use Java strategy mode to replace if else, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Don't use a lot of if else, the code is too simple and poor readability.

Definition of policy pattern

....

To put it simply, each module is independent of each other and does not affect each other to improve the scalability of the code!

Hold the chestnut, the bad code is as follows

If ("flower payment" .equals (type)) {/ / business logic processing of flower payment} else if ("WeChat Pay" .equals (type)) {/ / WeChat Pay business logic} else if ("cloud flash payment" .equals (type)) {/ / cloud flash payment business logic} else if ("..." .equals (type)) ) {/ /.}

Thinking: you can define a payment interface, payment methods, different payment methods to write different implementation classes, do not interfere with each other, do not affect each other, later add a new payment method, just add a new implementation class to implement the payment interface, rewrite his payment method without changing the previously written code.

It just so happens that I used this design pattern when I typed the code today. I'll share it briefly.

Define Interfac

Define different implementation classes

Each implementation class handles its own business, does not interfere with each other and is isolated from each other. For example, the following two implementation classes implement the same interface to handle different business

Define Interfac

Is it much simpler than a lot of if else?

Extract each different module, get the implementation class of the interface through the name of bean, and execute the method.

The above is all the content of the article "how to use Java Strategy Mode to replace if else". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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