Strategy Mode 1 Shopping Mall Promotional Products

a schema definition

Strategy mode: Define a series of algorithms, encapsulate each algorithm and use it interchangeably. The strategy mode allows the algorithm to change independently of the client application that uses it.

 

Two-mode example

1 Pattern Analysis

We illustrate this pattern by borrowing items from shopping malls.



 

2 Strategy pattern static class diagram



 

3 Code Examples

3.1 Create a strategy interface - IStrategy

package com.demo.strategy;

/**
 * Policy interface
 *
 * @author
 *
 */
public interface IStrategy {
	/**
	 * Calculate the actual price method
	 *
	 * @param consumePrice
	 * Amount of consumption
	 * @return
	 */
	public double realPrice(double consumePrice);
}

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;

	/**
	 * The construction method sets the discount rate
	 */
	public RebateStrategy() {
		this.rate = 0.8;
	}

	/**
	 * Calculate the actual price method
	 *
	 * @param consumePrice
	 * Amount of consumption
	 * @return
	 */
	public double realPrice(double consumePrice) {
		return consumePrice * this.rate;
	}

}

3.3 1000 minus 200 promotion strategy - ReduceStrategy

package com.demo.strategy;

/**
 * Over 1000 minus 200 product promotion strategy
 *
 * @author
 *
 */
public class ReduceStrategy implements IStrategy {
	/**
	 * Calculate the actual price method
	 *
	 * @param consumePrice
	 * Amount of consumption
	 * @return
	 */
	public double realPrice(double consumePrice) {
		if (consumePrice >= 1000) {
			return consumePrice - 200;
		} else {
			return consumePrice;
		}
	}
}

3.4 20% off promotion strategy for more than 200 parts - PromotionalStrategy

package com.demo.strategy;

/**
 * Over 200, more than 200 parts will get 20% off product promotion strategy
 *
 * @author
 *
 */
public class PromotionalStrategy implements IStrategy {
	/**
	 * Calculate the actual price method
	 *
	 * @param consumePrice
	 * Amount of consumption
	 * @return
	 */
	public double realPrice(double consumePrice) {
		if (consumePrice > 200) {
			return 200 + (consumePrice - 200) * 0.8;
		} else {
			return consumePrice;
		}

	}
}

3.5 Create a Context - Context

package com.demo.context;

import java.math.BigDecimal;

import com.demo.strategy.IStrategy;

/**
 * context
 *
 * @author
 *
 */
public class Context {
	// current strategy
	private IStrategy strategy;

	// set the current strategy
	public void setStrategy(IStrategy strategy) {
		this.strategy = strategy;
	}

	// Calculate price using strategy
	public double cul(double consumePrice) {
		// Use the specific product promotion strategy to get the actual consumption amount
		double realPrice = this.strategy.realPrice(consumePrice);
		// Formatting retains 1 decimal place, that is: accurate to the corner
		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 context object
		// Context context = new Context();
		// random number object
		Random random = new Random();
		for (int i = 0; i < 10; i++) {
			// How to generate random numbers to determine which promotion strategy to use
			int x = random.nextInt(3);
			// The consumer price is also generated by a random number (cannot be 0)
			double consumePrice = 0;
			while ((consumePrice = random.nextInt(2000)) == 0) {
			}

			double realPrice = consumePrice;
			switch (x) {
			case 0:
				// 20% off items
				// context.setStrategy(new RebateStrategy());
				realPrice = consumePrice * 0.8;
				break;
			case 1:
				// Over 200, 20% off for items above 200
				// context.setStrategy(new PromotionalStrategy());
				if (consumePrice > 200) {
					realPrice = 200 + (consumePrice - 200) * 0.8;
				}
				break;
			case 2:
				// 1000 minus 200 items
				// context.setStrategy(new ReduceStrategy());
				if (consumePrice >= 1000) {
					realPrice = consumePrice - 200;
				}
				break;
			}
			System.out.print("【"
					+ (x == 0 ? "20% off" : (x == 1 ? "20% off for parts above 200"
							: (x == 2 ? "Full 1000 minus 200" : ""))) + "] Commodity: ");

			System.out.println("Original price: " + consumePrice + " - Price after discount: " + realPrice);
		}
	}
}

4 Running results

[200 off over 1000] Commodity: Original Price: 908.0 - Discounted Price: 908.0

[200 off over 1000] Commodity: Original Price: 1129.0 - Discounted Price: 929.0

【Discount 200 over 1000】Commodity: Original Price: 829.0 - Discounted Price: 829.0

[20% off] Product: Original price: 518.0 - Discounted price: 414.400000000000003

【200 off over 1000】Commodity: Original price: 1230.0 - Discounted price: 1030.0

[20% off] Product: Original Price: 106.0 - Discounted Price: 84.800000000000001

【Discount 200 over 1000】Commodity: Original Price: 1134.0 - Discounted Price: 934.0

[20% off for parts above 200] Commodity: original price: 664.0 - discounted price: 571.2

[200 off over 1000] Commodity: Original Price: 564.0 - Discounted Price: 564.0

【Discount 200 for over 1000】Commodity: Original Price: 730.0 - Discounted Price: 730.0

 

Three principles of pattern design

1 "Open-close" principle

2 Single Responsibility Principle

 

Four use cases

1 When the performance behavior of multiple classes is different, it is necessary to dynamically select the specific execution behavior at runtime.

2 Different strategies need to be used in different situations, or strategies may also be implemented in other ways in the future.

3 When the implementation details of specific strategies need to be hidden, each specific strategy is independent of each other.

4 When there are multiple behaviors in a class, and multiple conditional branches are used in an operation to judge the use of multiple behaviors, the strategy pattern can be used to implant the actions of each conditional branch into a specific strategy.

 

Five-Strategy Pattern Static Class Diagram



 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690736&siteId=291194637