This article describes the use of strategy mode for Java to solve the problem of shopping mall promotional products. Share it for your reference, as follows:
A pattern definition
Policy mode: Define a series of algorithms, encapsulate each algorithm and can be used interchangeably, and the policy mode allows the algorithm to change independently of the customer application that uses it.
Examples of the second mode
1 Pattern Analysis
We borrow mall promotional products to illustrate this model.
2 Policy mode static class diagram
3 Code Examples
3.1 Create a policy interface-IStrategy
package com.demo.strategy;/** * Policy interface* * @author * */public interface IStrategy { /** * Method for calculating actual price* * @param consumptionPrice * Consumption amount* @return */ public double realPrice(double consumptionPrice);}3.2 20% off promotion strategy-RebateStrategy
package com.demo.strategy;/** * 20% off product promotion strategy* * @author * */public class RebateStrategy implements IStrategy { private final double rate; /** * Construct method set discount rate*/ public RebateStrategy() { this.rate = 0.8; } /** * Calculate actual price method* * @param consumptionPrice * Consumption amount* @return */ public double realPrice(double consumptionPrice) { return consumptionPrice * this.rate; }}3.3 Promotional Strategy for 200 off for over 1000 yuan, ReduceStrategy
package com.demo.strategy;/** * 200 off for purchases over 1000 Product promotion strategy* * @author * */public class ReduceStrategy implements IStrategy { /** * Method for calculating actual price* * @param consumptionPrice * Consumption amount* @return */ public double realPrice(double consumptionPrice) { if (consumePrice >= 1000) { return consumptionPrice - 200; } else { return consumptionPrice; } }}3.4 20% off promotional strategy for more than 200 yuan PromotionalStrategy
package com.demo.strategy;/** * 200 or more, 20% off for product promotion strategy for parts above 200* * @author * */public class PromotionalStrategy implements IStrategy { /** * Method for calculating actual price* * @param consumptionPrice * Consumption amount* @return */ public double realPrice(double consumptionPrice) { if (consumePrice > 200) { return 200 + (consumePrice - 200) * 0.8; } else { return consumptionPrice; } }}3.5 Creating a Context
package com.demo.context;import java.math.BigDecimal;import com.demo.strategy.IStrategy;/** * Context environment* * @author * */public class Context { // Current policy private IStrategy strategy; // Set the current policy public void setStrategy(IStrategy strategy) { this.strategy = strategy; } // Use strategy to calculate the price public double cul(double consumptionPrice) { // Use specific product promotion strategy to obtain the actual consumption amount double realPrice = this.strategy.realPrice(consumePrice); // Format the 1 digit after the decimal point, that is: accurate to the angle BigDecimal bd = new BigDecimal(realPrice); bd = bd.setScale(1, BigDecimal.ROUND_DOWN); return bd.doubleValue(); }}3.6 Consumer Shopping and Consumption Client
package com.demo;import java.util.Random;/** * Client application* * @author * */public class Client { /** * @param args */ public static void main(String[] args) { // Create an instance of the up and down environment object// Context context = new Context(); // Random number object Random random = new Random(); for (int i = 0; i < 10; i++) { // How to generate random numbers determine which promotion strategy to use int x = random.nextInt(3); // Consumer price is also generated by random numbers (cannot be 0) double consumptionPrice = 0; while ((consumePrice = random.nextInt(2000)) == 0) { } double realPrice = consumptionPrice; switch (x) { case 0: // 20% off products// context.setStrategy(new RebateStrategy()); realPrice = consumptionPrice * 0.8; break; case 1: // 20% off products above 200// context.setStrategy(new PromotionalStrategy()); if (consumePrice > 200) { realPrice = 200 + (consumePrice - 200) * 0.8; } break; case 2: // 200 off for purchases over 1000// context.setStrategy(new ReduceStrategy()); if (consumePrice >= 1000) { realPrice = consumptionPrice - 200; } break; } System.out.print("【" + (x == 0 ? "20% off for purchases over 200" : (x == 1 ? "20% off for purchases over 1000" : ""))) + "】Product: "); System.out.println("Original price:" + consumptionPrice + " - Price after discount: " + realPrice); } }}4 Running results
【200 off for purchases over 1000】Product: Original price: 908.0 - Price after discount: 908.0
【200 off for purchases over 1000】Product: Original price: 1129.0 - Price after discount: 929.0
【200 off for purchases over 1000】Product: Original price: 829.0 - Price after discount: 829.0
[20% off] Product: Original price: 518.0 - Price after discount: 414.4000000000000003
【200 off for purchases over 1000】Product: Original price: 1230.0 - Price after discount: 1030.0
【20% off】Product: Original price: 106.0 - Price after discount: 84.8000000000000001
【200 off for purchases over 1000】Product: Original price: 1134.0 - Price after discount: 934.0
[20% off for parts above 200] Product: Original price: 664.0 - Price after discount: 571.2
【200 off for purchases over 1000】Product: Original price: 564.0 - Price after discount: 564.0
【200 off for purchases over 1000】Product: Original price: 730.0 - Price after discount: 730.0
Three principles of design of this pattern
1 "Open-Close" principle
2 Single responsibility principle
Four usage occasions
1 When the performance behaviors of multiple classes are different, and it is necessary to dynamically select the specific execution behavior at the run time.
2 It is necessary to use different strategies in different situations, or the strategies may be implemented in other ways in the future.
3 When it is necessary to hide the implementation details of specific strategies, each specific strategy is independent of each other.
4 When multiple behaviors occur in a class and multiple conditional branches are used in an operation to judge the use of multiple behaviors, the policy pattern can be used to implant the actions of each conditional branch into a specific strategy.
Five-strategy mode static class diagram
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.