app和微网站的区别是什么,做网站的时候会用 鸟瞰图吗,企业内容管理系统,丽水网站开发公司一、策略模式的本质#xff1a;面向接口的算法工厂
策略模式#xff08;Strategy Pattern#xff09;是行为型设计模式的典型代表#xff0c;其核心思想是将算法族抽象为独立对象#xff0c;使其能够相互替换。这种模式完美体现了以下面向对象设计原则#xff1a; 开闭原…一、策略模式的本质面向接口的算法工厂
策略模式Strategy Pattern是行为型设计模式的典型代表其核心思想是将算法族抽象为独立对象使其能够相互替换。这种模式完美体现了以下面向对象设计原则 开闭原则OCP新增策略无需修改已有代码 单一职责原则SRP每个策略只负责特定算法 依赖倒置原则DIP高层模块依赖抽象而非具体实现
UML类图 二、策略模式的三种典型实现方式
1. 基础版实现传统方式
// 策略接口
public interface DiscountStrategy {BigDecimal calculateDiscount(BigDecimal amount);
}// 具体策略
public class VipDiscount implements DiscountStrategy {Overridepublic BigDecimal calculateDiscount(BigDecimal amount) {return amount.multiply(new BigDecimal(0.8));}
}public class FestivalDiscount implements DiscountStrategy {Overridepublic BigDecimal calculateDiscount(BigDecimal amount) {return amount.subtract(new BigDecimal(50));}
}// 上下文环境
public class OrderContext {private DiscountStrategy strategy;public void setStrategy(DiscountStrategy strategy) {this.strategy strategy;}public BigDecimal executeDiscount(BigDecimal amount) {return strategy.calculateDiscount(amount);}
}
2. 枚举策略简化分支判断
public enum CalculatorStrategy {ADD {Overridepublic int execute(int a, int b) { return a b; }},SUBTRACT {Overridepublic int execute(int a, int b) { return a - b; }};public abstract int execute(int a, int b);
}
3. Spring集成版企业级实践
// 定义策略接口
public interface PaymentStrategy {void processPayment(BigDecimal amount);
}// 实现策略带Spring注解
Component(alipayStrategy)
public class AlipayStrategy implements PaymentStrategy {Overridepublic void processPayment(BigDecimal amount) {// 支付宝支付逻辑}
}Component(wechatPayStrategy)
public class WechatPayStrategy implements PaymentStrategy {Overridepublic void processPayment(BigDecimal amount) {// 微信支付逻辑}
}// 策略上下文自动注入策略集合
Service
public class PaymentContext {Autowiredprivate MapString, PaymentStrategy strategyMap;public void executePayment(String paymentType, BigDecimal amount) {PaymentStrategy strategy strategyMap.get(paymentType Strategy);if (strategy ! null) {strategy.processPayment(amount);} else {throw new IllegalArgumentException(Unsupported payment type);}}
} 三、策略模式的六大应用场景
场景1电商促销系统 满减策略 折扣策略 赠品策略 积分抵现策略
场景2支付网关路由 支付宝支付 微信支付 银联支付 数字货币支付
场景3日志处理系统 本地文件存储 云存储OSS/S3 消息队列转发 数据库存储
场景4数据校验引擎 手机号校验 身份证校验 邮箱校验 地址校验
场景5游戏AI系统 攻击策略 防御策略 逃跑策略 补给策略
场景6报表生成系统 PDF生成 Excel生成 HTML生成 CSV生成 四、策略模式与相关模式的深度对比
模式关注点与策略模式的关系工厂模式对象创建策略模式常配合工厂创建具体策略状态模式状态转换状态改变行为策略改变算法模板方法模式算法步骤策略替换整个算法模板方法替换步骤命令模式请求封装策略是主动选择命令是被动触发 五、企业级实战支付系统策略架构设计
架构图
复制
[支付请求] -- [支付网关]↓[策略路由中心]↓
--------------------------------
| 支付宝策略 | 微信支付策略 | 银联策略
--------------------------------↓[渠道适配层]↓[第三方支付平台]
代码实现Spring Boot 策略模式
java
复制
// 支付策略接口
public interface PaymentStrategy {PaymentResult pay(PaymentRequest request);
}// 支付宝策略实现
Component
public class AlipayStrategy implements PaymentStrategy {OverridePaymentType(PayChannel.ALIPAY)public PaymentResult pay(PaymentRequest request) {// 调用支付宝SDKreturn new PaymentResult(true, ALIPAY-123456);}
}// 策略工厂自动发现策略
Component
public class PaymentStrategyFactory {Autowiredprivate MapString, PaymentStrategy strategyMap;public PaymentStrategy getStrategy(PayChannel channel) {return strategyMap.values().stream().filter(s - s.getClass().isAnnotationPresent(PaymentType.class)).filter(s - s.getClass().getAnnotation(PaymentType.class).value() channel).findFirst().orElseThrow(() - new RuntimeException(未找到支付策略));}
}// 支付服务
Service
RequiredArgsConstructor
public class PaymentService {private final PaymentStrategyFactory strategyFactory;public PaymentResult processPayment(PaymentRequest request) {PaymentStrategy strategy strategyFactory.getStrategy(request.getChannel());return strategy.pay(request);}
} 六、策略模式的五个优化技巧
1. 策略预热缓存
public class StrategyCache {private static final MapString, Strategy cache new ConcurrentHashMap();public static Strategy getStrategy(String type) {return cache.computeIfAbsent(type, t - {// 动态加载策略类try {return (Strategy) Class.forName(t).newInstance();} catch (Exception e) {throw new RuntimeException(策略加载失败);}});}
}
2. 策略权重配置
# application.yml
payment:strategies:alipay: weight: 60enable: truewechat: weight: 30 enable: trueunionpay: weight: 10enable: false
3. 策略性能监控
public class MonitoredStrategy implements Strategy {private final Strategy delegate;private final MeterRegistry registry;public MonitoredStrategy(Strategy delegate, MeterRegistry registry) {this.delegate delegate;this.registry registry;}Overridepublic void execute() {Timer.Sample sample Timer.start(registry);try {delegate.execute();} finally {sample.stop(registry.timer(strategy.execution.time, type, delegate.getClass().getSimpleName()));}}
} 七、常见陷阱与解决方案
陷阱现象解决方案策略状态共享线程安全问题使用ThreadLocal或每次新建策略实例策略膨胀失控类数量爆炸使用DSL动态生成策略类策略切换开销大频繁切换影响性能引入策略缓存池策略配置错误运行时找不到策略增加策略fallback机制策略执行顺序依赖策略之间存在依赖关系引入策略责任链模式 文章转载自: http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn http://www.morning.smry.cn.gov.cn.smry.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.mwwnz.cn.gov.cn.mwwnz.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.ygxf.cn.gov.cn.ygxf.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn