SoFunction
Updated on 2025-04-07

Detailed explanation of the strategy mode of Android design mode

Strategy Mode

The effect of a function has different algorithms and strategies, and different results are selected according to different choices.

Simply put, as long as you have written a program, you have used the strategy mode. Don’t say that you have never used it. Has if-else (switch) not used it...

if-else is actually a manifestation of a strategy model, and different results are processed according to different choices.

question

If all methods are processed with if-else (switch), there is no problem in terms of function, but in terms of maintenance and use at the code level, if-else is too much, the class will become too large, which is not good at reading and is difficult to modify.

Solve the problem

Use the policy pattern to define a unified interface. Each different function (if-else) implements an interface to make a specific class, and externally call specific classes to achieve different results.

Use scenarios

There are different solutions to the same problem
A class has multiple if-else judgment processing results
When the upper-level processing results are returned when encapsulating the SDK, the caller cares about the results and does not pay attention to the implementation process.
TimeInterpolator, ListView adapter included in the animation in Android source code

Code implementation

There is a product that should be sold. During the sale process, different prices should be given according to different users (half-price, 20% off, 30% off, etc.). How to directly give the price while knowing the user?

(I) Implementation of price interface

public interface PriceStrategy {
 int setPrice(int price);
}

(II) Realize specific price categories

30% off:

public class seventPriceStrategy implements PriceStrategy {
 @Override
 public Double setPrice(int price) {
  return 0.7 * price;
 }
}


50% off:

public class HalfPriceStrategy implements PriceStrategy {

 @Override
 public Double setPrice(int price) {
 return 0.5 * price;
 }
}

(III) Price algorithm management category

public class PriceAlgorithm {
 private PriceStrategy priceStrategy;


 public PriceStrategy getPriceStrategy() {
 return priceStrategy;
 }

 public void setPriceStrategy(PriceStrategy priceStrategy) {
  = priceStrategy;
 }

 public Double getPrice(int price) {
 if(priceStrategy!=null){
  return (price);
 }
 return null;
 }
}

Pass in a specific implementation class to get the return interface

(IV) Call method

 PriceAlgorithm priceAlgorithm = new PriceAlgorithm();
 (new HalfPriceStrategy());
 ("\n" + "1 dollar" + "Price after 50% off:" + ((1)));


 PriceAlgorithm priceAlgorithm2 = new PriceAlgorithm();
 (new seventPriceStrategy());
 ("\n" + "2 dollars" + "Price after 30% off:" + ((2)));


(V) Display results

Price after 50% off: 0.5
Price after 2 yuan 30% off: 1.4

Summarize

Maintenance after using the policy model only requires maintenance of specific implementation classes. If there is a new method, only the implementation specific classes need to be expanded to facilitate maintenance and use.

github code address

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.