文章目录
策略模式
- 策略模式
- 策略模式实战
- 不使用策略模式的写法
- 使用策略模式的写法
策略模式解决的场景是具有同类课替代的行为逻辑算法场景, 比如不同类型的交易方式, 不同类型的登录方式等. 使用策略模式进行行为的包装, 供外部统一调用.
策略模式实战模拟电商网站的各种不同的优惠, 满减, 直减, 折扣, n元购.
不使用策略模式的写法写法如下 . 直接if else判断即可.
public class CouponDiscountService {
public double discountAmount(Integer type, double typeContent, double skuPrice, double typeExt) {
// 1. 直减券
if (1 == type) {
return skuPrice - typeContent;
}
// 2. 满减券
if (2 == type) {
if (skuPrice
关注
打赏