设计模式中,策略模式(Strategy Pattern)的概念解释,Java代码示例详解。

策略模式(Strategy Pattern)

一、概念

策略模式:定义一系列算法,把它们分别封装起来,使它们可以互相替换。本模式使得算法可独立于使用它的客户端而变化。

换句话说:
👉 把容易变化的算法提取出来,作为策略传入,从而避免在主流程里写死逻辑。

二、特点

  • 封装性:每个算法单独成类,彼此独立,便于扩展。
  • 灵活性:运行时可以动态切换不同策略,而不需要修改核心逻辑。
  • 开闭原则:新增策略只需添加类,不必改动已有逻辑。

三、适用场景

  • 算法需要经常切换时(排序、优惠计算、日志格式化等)。
  • 客户端希望在运行时自由选择某种算法。
  • 系统存在大量的 if...else... 判断,且逻辑相似但实现细节不同。

四、练习:购物车折扣系统

需求:

  • 普通会员:打9折。
  • Prime会员:打8折。
  • 满100减20。
  • 新增策略:在满100减20基础上,再对Prime会员打7折。

五、完整代码

import java.math.BigDecimal;
import java.math.RoundingMode;

// 策略接口
interface DiscountStrategy {
    BigDecimal getDiscount(BigDecimal total);
}

// 普通会员折扣(九折)
class UserDiscountStrategy implements DiscountStrategy {
    public BigDecimal getDiscount(BigDecimal total) {
        return total.multiply(new BigDecimal("0.1")).setScale(2, RoundingMode.DOWN);
    }
}

// Prime会员折扣(八折)
class PrimeDiscountStrategy implements DiscountStrategy {
    public BigDecimal getDiscount(BigDecimal total) {
        return total.multiply(new BigDecimal("0.2")).setScale(2, RoundingMode.DOWN);
    }
}

// 满100减20
class OverDiscountStrategy implements DiscountStrategy {
    public BigDecimal getDiscount(BigDecimal total) {
        return total.compareTo(BigDecimal.valueOf(100)) >= 0 ? BigDecimal.valueOf(20) : BigDecimal.ZERO;
    }
}

// 满100减20 + Prime会员七折
class PrimeOverDiscountStrategy implements DiscountStrategy {
    public BigDecimal getDiscount(BigDecimal total) {
        BigDecimal overDiscount = total.compareTo(BigDecimal.valueOf(100)) >= 0 ? BigDecimal.valueOf(20) : BigDecimal.ZERO;
        BigDecimal afterOver = total.subtract(overDiscount);
        BigDecimal primeDiscount = afterOver.multiply(new BigDecimal("0.3")); // 30% discount
        return overDiscount.add(primeDiscount).setScale(2, RoundingMode.DOWN);
    }
}

// 上下文类
class DiscountContext {
    private DiscountStrategy strategy = new UserDiscountStrategy(); // 默认普通会员

    public void setStrategy(DiscountStrategy strategy) {
        this.strategy = strategy;
    }

    public BigDecimal calculatePrice(BigDecimal total) {
        return total.subtract(this.strategy.getDiscount(total)).setScale(2, RoundingMode.DOWN);
    }
}

// 测试
public class StrategyPatternDemo {
    public static void main(String[] args) {
        DiscountContext ctx = new DiscountContext();

        BigDecimal total = BigDecimal.valueOf(150);

        // 默认:普通会员
        System.out.println("User: " + ctx.calculatePrice(total));

        // Prime会员
        ctx.setStrategy(new PrimeDiscountStrategy());
        System.out.println("Prime: " + ctx.calculatePrice(total));

        // 满减
        ctx.setStrategy(new OverDiscountStrategy());
        System.out.println("Over 100 -20: " + ctx.calculatePrice(total));

        // 满减 + Prime会员七折
        ctx.setStrategy(new PrimeOverDiscountStrategy());
        System.out.println("Prime + Over: " + ctx.calculatePrice(total));
    }
}

六、运行结果示例

User: 135.00
Prime: 120.00
Over 100 -20: 130.00
Prime + Over: 91.00

这样,新的 Prime+满减七折策略 已经完美加入,而不用修改原有逻辑 🎯

posted @ 2025-09-09 16:33  AlphaGeek  阅读(29)  评论(0)    收藏  举报