1 Concept
The factory pattern is to decouple the creation and use of objects, and the observer pattern is to decouple the observer and the observed. The strategy mode is similar to the two, and can also play a decoupling role. However, it == decoupling is the definition, creation, and use of the three parts of the strategy ==
2 Realization
A strategy for calculating the order price is implemented here. The product may perform various activities, and the discount for each activity is different. This is implemented through the strategy mode, the code is simple, and the original code does not need to be modified when adding activities later.
2.1 Policy interface definition
/**
* 策略定义
*
* @Author: prepared
* @Date: 2021/6/21 15:04
*/
public interface DiscountStrategy {
double calDiscount(Order order);
}
2.2 Implementation of specific strategies
The specific implementations of GrouponDiscountStrategy and PromotionDiscountStrategy are the same, so I won't write them here.
@Service
public class NormalDiscountStrategy implements PushStrategyI{
private Logger logger = LoggerFactory.getLogger(NormalDiscountStrategy.class);
@Override
public Response calDiscount(Long userId) {
}
}
2.3 Strategy Factory
Here, you can also inject specific strategies in through attributes to obtain the method of strategy, and obtain the corresponding strategy through if/else judgment.
/**
* 使用工厂模式 创建策略
*
* @Author: prepared
* @Date: 2021/6/21 15:04
*/
public class DiscountStrategyFactory {
private static final Map<OrderType, DiscountStrategy> strategies = new HashMap<>();
static {
strategies.put(OrderType.NORMAL, new NormalDiscountStrategy());
strategies.put(OrderType.GROUPON, new GrouponDiscountStrategy());
strategies.put(OrderType.PROMOTION, new PromotionDiscountStrategy());
}
public static DiscountStrategy getDiscountStrategy(OrderType type) {
return strategies.get(type);
}
}
2.4 Use of strategies
/**
* 策略的使用
*
* @Author: prepared
* @Date: 2021/6/21 15:04
*/
public class OrderService {
public double discount(Order order) {
OrderType type = order.getType();
DiscountStrategy discountStrategy = DiscountStrategyFactory.getDiscountStrategy(type);
return discountStrategy.calDiscount(order);
}
}
3 application scenarios
The most common application scenario is to use it to avoid lengthy if-else or switch branch judgments. The strategy mode is suitable for an application scenario of deciding which strategy to use according to different types of dynamics.
The main function of the strategy mode is to define, create and use decoupling strategies, to control the complexity of the code, so that each part is not too complicated and the amount of code is too much.
4 Actual usage scenarios
When promoting the mall, some needs are to send text messages to users, some need to call users, and still others need to send WeChat notification messages or other notifications to users.
The strategy mode is used here, the code is concise, and it is convenient to extend other strategies, such as the need to nail notification messages, user-accurate advertisements, and so on.
Otherwise, every time the demand is increased, the original code needs to be modified on a large scale, which is inconsistent with the design principle [open for extension, closed for modification].
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。