In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to achieve the strategic pattern in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Android article on how to achieve the strategic pattern. Let's take a look at it.
Strategy mode
The effect of a function, there are different algorithms and strategies, according to different choices to choose different results.
To put it simply, as long as you have written a program, you have used strategic mode. Don't say you haven't used it. Haven't you used if-else (switch)?
If-else is actually the embodiment of a strategic model, dealing with different results according to different choices.
problem
If all the methods are handled with if-else (switch), there is no problem in terms of function, but in terms of maintenance and use at the code level, too much if-else will make the class too large, difficult to read and difficult to modify.
Solve the problem
Using the policy pattern, define a unified interface, each different function (if-else) implementation interface to do a specific class, external calls to specific classes to achieve different results.
Working with scen
There are different solutions to the same problem
The judgment processing result of a class with multiple if-else
When encapsulating SDK, the upper layer returns the result. The caller cares about the result, not the implementation process.
Adapter for animated TimeInterpolator,ListView included in the Android source code
Code implementation
There is a commodity for sale. In the process of selling, different users should be given different prices (half price, 20% discount, 30% discount, etc.). Under the premise of knowing the user, how to give the price directly?
(1) implementation of price interface
Public interface PriceStrategy {int setPrice (int price);}
(2) implement specific price categories
30% discount:
Public class seventPriceStrategy implements PriceStrategy {@ Override public Double setPrice (int price) {return 0.7 * price;}}
50% discount:
Public class HalfPriceStrategy implements PriceStrategy {@ Override public Double setPrice (int price) {return 0.5 * price;}}
(3) Price algorithm management class
Public class PriceAlgorithm {private PriceStrategy priceStrategy; public PriceStrategy getPriceStrategy () {return priceStrategy;} public void setPriceStrategy (PriceStrategy priceStrategy) {this.priceStrategy = priceStrategy;} public Double getPrice (int price) {if {return priceStrategy.setPrice (price);} return null;}}
Pass in the specific implementation class to get the return interface
(4) method of calling
PriceAlgorithm priceAlgorithm = new PriceAlgorithm (); priceAlgorithm.setPriceStrategy (new HalfPriceStrategy ()); System.out.print ("\ n" + "1 yuan" + "50% discount price:" + String.valueOf (priceAlgorithm.getPrice (1); PriceAlgorithm priceAlgorithm2 = new PriceAlgorithm (); priceAlgorithm2.setPriceStrategy (new seventPriceStrategy ()); System.out.print ("\ n" + "2 yuan" + "30% discount price:" + String.valueOf (priceAlgorithm2.getPrice (2)
(5) display the results
Price after 50% discount of 1 yuan: 0.5
Price after 2 yuan 30% discount: 1.4
This is the end of the article on "how to implement strategic patterns in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to implement the strategic model in Android". If you want to learn more, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.