当前位置: 首页 > wzjs >正文

网站电子报怎么做互联网营销专家

网站电子报怎么做,互联网营销专家,智慧团建团员登录入口,塘沽做网站一、传统策略模式的痛点与突破 1.1 传统策略实现回顾 // 传统支付策略接口 public interface PaymentStrategy {void pay(BigDecimal amount); }// 具体策略实现 public class AlipayStrategy implements PaymentStrategy {public void pay(BigDecimal amount) { /* 支付宝支…

一、传统策略模式的痛点与突破

1.1 传统策略实现回顾

// 传统支付策略接口
public interface PaymentStrategy {void pay(BigDecimal amount);
}// 具体策略实现
public class AlipayStrategy implements PaymentStrategy {public void pay(BigDecimal amount) { /* 支付宝支付逻辑 */ }
}// 策略上下文
public class PaymentContext {private PaymentStrategy strategy;public void setStrategy(PaymentStrategy strategy) {this.strategy = strategy;}public void executePayment(BigDecimal amount) {strategy.pay(amount);}
}

存在问题
❌ 策略类型固定,无法通用化
❌ 新增策略需修改上下文类
❌ 无法动态管理策略集合


二、泛型化策略工具类设计

2.1 核心接口定义

/*** 通用策略接口* @param <T> 策略参数类型* @param <R> 返回结果类型*/
public interface GenericStrategy<T, R> {/*** 是否支持当前策略类型*/boolean support(String strategyType);/*** 执行策略*/R apply(T param);
}

2.2 策略上下文工具类

public class StrategyContext<T, R> {private final Map<String, GenericStrategy<T, R>> strategyMap = new ConcurrentHashMap<>();/*** 注册策略*/public void registerStrategy(String strategyType, GenericStrategy<T, R> strategy) {strategyMap.put(strategyType, strategy);}/*** 执行策略*/public R execute(String strategyType, T param) {GenericStrategy<T, R> strategy = Optional.ofNullable(strategyMap.get(strategyType)).orElseThrow(() -> new IllegalArgumentException("未找到策略: " + strategyType));return strategy.apply(param);}/*** 批量执行策略*/public List<R> executeAll(T param) {return strategyMap.values().stream().map(s -> s.apply(param)).collect(Collectors.toList());}
}

三、Spring集成与自动装配

3.1 自动注册策略实现

@Configuration
public class StrategyAutoConfiguration {/*** 自动发现所有策略Bean并注册*/@Beanpublic <T, R> StrategyContext<T, R> strategyContext(List<GenericStrategy<T, R>> strategies) {StrategyContext<T, R> context = new StrategyContext<>();strategies.forEach(strategy -> context.registerStrategy(strategy.getClass().getAnnotation(StrategyType.class).value(),strategy));return context;}
}/*** 策略类型注解*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface StrategyType {String value();
}

3.2 策略实现示例

@StrategyType("alipay")
@Component
public class AlipayStrategy implements GenericStrategy<PaymentRequest, PaymentResult> {@Overridepublic boolean support(String strategyType) {return "alipay".equals(strategyType);}@Overridepublic PaymentResult apply(PaymentRequest request) {// 支付宝支付具体实现}
}

四、企业级应用案例

4.1 支付策略路由

@RestController
@RequestMapping("/payment")
public class PaymentController {@Autowiredprivate StrategyContext<PaymentRequest, PaymentResult> paymentContext;@PostMapping("/{type}")public PaymentResult pay(@PathVariable String type, @RequestBody PaymentRequest request) {return paymentContext.execute(type, request);}
}

4.2 动态折扣计算

public enum DiscountType {NEW_USER, FESTIVAL, VIP_LEVEL
}public class DiscountStrategy implements GenericStrategy<DiscountType, BigDecimal> {private static final Map<DiscountType, BigDecimal> DISCOUNT_MAP = Map.of(DiscountType.NEW_USER, new BigDecimal("0.9"),DiscountType.FESTIVAL, new BigDecimal("0.8"),DiscountType.VIP_LEVEL, new BigDecimal("0.7"));@Overridepublic boolean support(String strategyType) {return Arrays.stream(DiscountType.values()).anyMatch(e -> e.name().equals(strategyType));}@Overridepublic BigDecimal apply(DiscountType type) {return DISCOUNT_MAP.get(type);}
}

五、高级功能扩展

5.1 策略优先级控制

public class PriorityStrategyContext<T, R> extends StrategyContext<T, R> {private final PriorityQueue<GenericStrategy<T, R>> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(this::getPriority));private int getPriority(GenericStrategy<T, R> strategy) {return strategy.getClass().isAnnotationPresent(StrategyPriority.class) ?strategy.getClass().getAnnotation(StrategyPriority.class).value() : 0;}@Overridepublic void registerStrategy(String type, GenericStrategy<T, R> strategy) {super.registerStrategy(type, strategy);priorityQueue.offer(strategy);}public R executeFirst(T param) {return priorityQueue.peek().apply(param);}
}

5.2 策略执行监控

public class MonitoredStrategyContext<T, R> extends StrategyContext<T, R> {private final MeterRegistry meterRegistry;@Overridepublic R execute(String strategyType, T param) {Timer.Sample sample = Timer.start(meterRegistry);try {R result = super.execute(strategyType, param);sample.stop(meterRegistry.timer("strategy.execute.time", "type", strategyType));return result;} catch (Exception e) {meterRegistry.counter("strategy.error", "type", strategyType).increment();throw e;}}
}

六、最佳实践总结

  1. 合理定义策略边界:每个策略应聚焦单一职责

  2. 统一异常处理:定义策略执行异常体系

  3. 版本控制策略:支持多版本策略共存

  4. 动态配置支持:结合配置中心实现热更新

  5. 性能优化:缓存高频使用策略

http://www.dtcms.com/wzjs/125.html

相关文章:

  • 门户网站属于什么类型的模式seo搜索引擎优化总结
  • 重庆可做网站 APP外贸营销型网站制作公司
  • 主机服务器网站 怎么做正规推广平台有哪些
  • 两学一做考学网站网络推广公司网站
  • 搭建影视网站违法推广资源网
  • 用笔记本做网站种子搜索在线 引擎
  • 营销型网站建设思路网站优化课程培训
  • 做全屏的网站 一屛多高百度指数官网登录
  • 西安大型网站建设电商网址
  • 什么网站可以做外单广告接单平台有哪些
  • 国产比较好的精华长春网站优化流程
  • 做网站需要什么基础免费发布平台
  • 网站源码风险seo包年优化
  • 网站模板怎么用下载百度app最新版并安装
  • 个人个案网站 类型重大军事新闻最新消息
  • 离石做网站的公司百度明星搜索量排行榜
  • 门头沟做网站公司郑州seo优化大师
  • 网站开发遵循南宁seo推广外包
  • 网站设计时间免费二级域名分发网站
  • 深圳做响应式网站本周新闻热点
  • 苏州论坛型网站建设深圳全网营销型网站
  • c2c网站是什么百度网盘搜索引擎入口官网
  • 做天猫网站价格seo收录查询
  • 嘉兴丝绸大厦做网站的公司沈阳百度seo关键词排名优化软件
  • 网站开发做什么的强力搜索引擎
  • wordpress文章不登录看不到网络优化师是什么工作
  • 三门峡住房城乡建设局网站seo关键词优化怎么收费
  • 卡盟网站建设网站快速收录的方法
  • 房地产开发公司管理制度seo北京优化
  • 盘州市网站建设西安网站建设网络推广