Java策略模式案例详解
策略模式是一种行为设计模式,它允许在运行时选择算法的行为,我将通过一个完整的案例来展示如何实现策略模式。

基础结构
策略模式包含三个核心角色:
- 策略接口:定义所有策略的共同方法
- 具体策略类:实现具体的算法
- 上下文类:维护策略对象的引用
完整案例:支付系统
假设我们要实现一个支付系统,支持多种支付方式(支付宝、微信、银行卡)。
1 定义策略接口
// 支付策略接口
public interface PaymentStrategy {
void pay(double amount);
String getPaymentMethod();
}
2 实现具体策略类
// 支付宝支付
public class AlipayStrategy implements PaymentStrategy {
@Override
public void pay(double amount) {
System.out.println("使用支付宝支付:" + amount + "元");
// 实际的支付宝支付逻辑
}
@Override
public String getPaymentMethod() {
return "支付宝";
}
}
// 微信支付
public class WechatPayStrategy implements PaymentStrategy {
@Override
public void pay(double amount) {
System.out.println("使用微信支付:" + amount + "元");
// 实际的微信支付逻辑
}
@Override
public String getPaymentMethod() {
return "微信支付";
}
}
// 银行卡支付
public class BankCardStrategy implements PaymentStrategy {
@Override
public void pay(double amount) {
System.out.println("使用银行卡支付:" + amount + "元");
// 实际的银行卡支付逻辑
}
@Override
public String getPaymentMethod() {
return "银行卡";
}
}
3 创建上下文类
// 支付上下文
public class PaymentContext {
private PaymentStrategy strategy;
public PaymentContext() {
// 默认使用支付宝
this.strategy = new AlipayStrategy();
}
// 设置支付策略
public void setPaymentStrategy(PaymentStrategy strategy) {
this.strategy = strategy;
}
// 执行支付
public void executePayment(double amount) {
if (strategy == null) {
throw new IllegalStateException("请先设置支付方式");
}
System.out.println("当前支付方式:" + strategy.getPaymentMethod());
strategy.pay(amount);
}
}
4 使用示例
public class StrategyPatternDemo {
public static void main(String[] args) {
// 创建支付上下文
PaymentContext paymentContext = new PaymentContext();
// 使用支付宝支付
paymentContext.setPaymentStrategy(new AlipayStrategy());
paymentContext.executePayment(100.0);
System.out.println("-------------------");
// 切换到微信支付
paymentContext.setPaymentStrategy(new WechatPayStrategy());
paymentContext.executePayment(200.0);
System.out.println("-------------------");
// 使用银行卡支付
paymentContext.setPaymentStrategy(new BankCardStrategy());
paymentContext.executePayment(300.0);
}
}
进阶版:带工厂模式的策略模式
// 策略工厂
public class PaymentStrategyFactory {
private static final Map<String, PaymentStrategy> strategies = new HashMap<>();
static {
strategies.put("alipay", new AlipayStrategy());
strategies.put("wechat", new WechatPayStrategy());
strategies.put("bankcard", new BankCardStrategy());
}
public static PaymentStrategy getStrategy(String type) {
PaymentStrategy strategy = strategies.get(type.toLowerCase());
if (strategy == null) {
throw new IllegalArgumentException("不支持的支付方式:" + type);
}
return strategy;
}
}
// 使用示例
public class FactoryStrategyDemo {
public static void main(String[] args) {
PaymentContext context = new PaymentContext();
// 通过工厂获取策略
context.setPaymentStrategy(PaymentStrategyFactory.getStrategy("wechat"));
context.executePayment(150.0);
}
}
使用Lambda简化(Java 8+)
// 使用函数式接口简化
public class LambdaStrategyDemo {
public static void main(String[] args) {
PaymentContext context = new PaymentContext();
// 使用Lambda表达式直接定义策略
PaymentStrategy vipStrategy = amount ->
System.out.println("VIP会员支付:" + amount * 0.9 + "元(9折优惠)");
context.setPaymentStrategy(vipStrategy);
context.executePayment(500.0);
}
}
最佳实践建议
-
何时使用策略模式:
- 需要在不同时间应用不同的业务规则
- 系统有很多相似的类,仅行为不同
- 需要避免使用多重条件语句
-
注意事项:
- 客户端必须了解不同的策略
- 策略类数量可能过多
- 考虑使用享元模式策略对象
-
与其他模式结合:
- 结合工厂模式创建策略对象
- 结合单例模式管理策略实例
- 结合模板方法处理共同逻辑
这就是一个完整的Java策略模式案例,从基础到进阶,覆盖了实际开发中常见的场景。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播