news 2026/2/17 5:33:46

使用模板模式+策略模式实现产品推荐

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
使用模板模式+策略模式实现产品推荐

一、实现思路

  • 模板方法:固定推荐流程

  • 策略模式:听阈规则 / 价格规则可替换

二、整体设计结构

AbstractProductRecommendTemplate ↓ filterByThreshold() ← 策略① ↓ groupByBrand() ↓ selectByPriceLevel() ← 策略② ↓ buildResult()

三、第一步:定义“推荐模板”(流程写死)

public abstract class AbstractProductRecommendTemplate { public final List<Product> recommend(List<Product> products, UserProfile user) { // 1、 听阈过滤(最关键) List<Product> available = filterByThreshold(products, user); if (available.isEmpty()) { return Collections.emptyList(); } // 2、 按品牌分组 Map<String, List<Product>> brandMap = groupByBrand(available); // 3、 每个品牌选高 / 中 / 低 3种价格的产品,对应不同价格需求的用户群体 return selectByPriceLevel(brandMap, user); } protected abstract List<Product> filterByThreshold( List<Product> products, UserProfile user); protected Map<String, List<Product>> groupByBrand(List<Product> products) { return products.stream() .collect(Collectors.groupingBy(Product::getBrand)); } protected abstract List<Product> selectByPriceLevel( Map<String, List<Product>> brandMap, UserProfile user); }

四、第二步:听阈匹配策略

1、 策略接口

public interface ThresholdMatchStrategy { boolean match(Product product, UserProfile user); }

2、实现类

@Component("DEFAULT_THRESHOLD") public class DefaultThresholdStrategy implements ThresholdMatchStrategy { @Override public boolean match(Product p, UserProfile user) { return p.getMinHz() <= user.getMinHz() && p.getMaxHz() >= user.getMaxHz(); } }

五、第三步:价格选择策略

1、 策略接口

public interface PriceSelectStrategy { List<Product> select(List<Product> products); }

2、 高 / 中 / 低 价格策略

@Component("HIGH_MID_LOW") public class HighMidLowPriceStrategy implements PriceSelectStrategy { @Override public List<Product> select(List<Product> products) { if (products.size() <= 3) { return products; } products.sort(Comparator.comparing(Product::getPrice)); Product low = products.get(0); Product mid = products.get(products.size() / 2); Product high = products.get(products.size() - 1); return List.of(low, mid, high); } }

六、模板的实现类组合使用上面的2个策略

@Autowired private ThresholdMatchStrategy thresholdStrategy; @Autowired private PriceSelectStrategy priceSelectStrategy; @Override protected List<Product> filterByThreshold(List<Product> products, UserProfile user) { return products.stream() .filter(p -> thresholdStrategy.match(p, user)) .collect(Collectors.toList()); } @Override protected List<Product> selectByPriceLevel( Map<String, List<Product>> brandMap, UserProfile user) { List<Product> result = new ArrayList<>(); brandMap.forEach((brand, list) -> { result.addAll(priceSelectStrategy.select(list)); }); return result; }

七、Controller / Service 使用方式

@Autowired private ProductRecommendService recommendService; public List<Product> recommend(Long userId) { UserProfile user = userService.getProfile(userId); List<Product> products = productRepository.findAll(); return recommendService.recommend(products, user); }

这套设计特别适用于以下场景:

  • 听力产品推荐

  • 活动商品推荐

  • 套餐组合

  • 分档定价

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/15 6:49:50

交通仿真软件:Aimsun_(8).公交系统模拟

公交系统模拟 在交通仿真软件Aimsun中&#xff0c;公交系统模拟是一个重要的模块&#xff0c;用于评估和优化公共交通系统的性能。本节将详细介绍如何在Aimsun中进行公交系统模拟&#xff0c;包括公交线路的创建、公交车辆的配置、公交站台的设置以及公交调度策略的实施。我们将…

作者头像 李华
网站建设 2026/2/9 6:08:40

AuxiliaryDisplayApi.dll文件丢失找不到 打不开问题 免费下载方法分享

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华
网站建设 2026/2/13 0:06:00

当数据开始为Agent而生:IDC FutureScape 2026 给中国企业的十个关键信号

数据不再仅仅是人工智能的输入&#xff0c;而是企业智能的基石。 一个正在到来的“数据重构”拐点 随着AI Agent从概念走向实际部署&#xff0c;数据正在经历一场根本性的角色转变。对中国企业而言&#xff0c;问题已不再是“是否拥有数据”&#xff0c;而是&#xff1a;现有的…

作者头像 李华