news 2026/4/15 12:15:00

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

作者头像

张小明

前端开发工程师

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/4/14 19:23:47

Go基础之环境搭建

文章目录 1 Go 1.1 简介 1.1.1 定义1.1.2 特点用途 1.2 环境配置 1.2.1 下载安装1.2.2 环境配置 1.2.2.1 添加环境变量1.2.2.2 各个环境变量理解 1.2.3 验证环境变量 1.3 包管理工具 Go Modules 1.3.1 开启使用1.3.2 添加依赖包1.3.3 配置国内包源 1.3.3.1 通过 go env 配置1.…

作者头像 李华
网站建设 2026/4/15 12:14:59

clickhouse-介绍、安装、数据类型、sql

1、介绍 ClickHouse是俄罗斯的Yandex于2016年开源的列式存储数据库&#xff08;DBMS&#xff09;&#xff0c;使用C语言编写&#xff0c;主要用于在线分析处理查询&#xff08;OLAP&#xff09;&#xff0c;能够使用SQL查询实时生成分析数据报告。 OLAP&#xff08;On-Line Ana…

作者头像 李华
网站建设 2026/3/21 12:28:35

GO 快速升级Go版本

由于底层依赖升级了&#xff0c;那我们也要跟着升&#xff0c;go老版本已经不足满足需求了&#xff0c;必须要将版本升级到1.22.0以上 查看当前Go版本 命令查看go版本 go version [rootlocalhost local]# go version go version go1.21.4 linux/amd64 [rootlocalhost local]# …

作者头像 李华
网站建设 2026/4/4 12:21:06

GaussDB数据库中SQL诊断解析之配置SQL限流

配置SQL限流 GaussDB提供SQL限流功能&#xff0c;当存在异常SQL&#xff08;如存在不优索引&#xff09;、SQL并发量上升时&#xff0c;通过SQL限流功能控制异常SQL的访问量或者并发量&#xff0c;保障服务的可用性。 前提条件 登录账号需要具备授权项“gaussdb:instance:listF…

作者头像 李华
网站建设 2026/4/7 7:26:52

交通仿真软件:Aimsun_(5).交通流量模拟

交通流量模拟 1. 交通流量的基本概念 交通流量是指在一定时间内通过某一道路断面的车辆数量。在交通仿真软件中&#xff0c;交通流量的模拟是核心功能之一&#xff0c;它可以帮助交通工程师和规划人员评估道路网络的性能&#xff0c;优化交通管理策略&#xff0c;预测交通拥堵等…

作者头像 李华
网站建设 2026/4/13 11:26:45

Flask:后端框架使用

文章目录 1、介绍2、demo演示3、Flask请求和响应 3.1 演示demo3.2 request获取请求体数据3.3 requests发送请求3.4 响应返回和接收 4、特殊路由 4.1 路由重定向4.2 路由拦截器 1、介绍 Flask是由python语言编写的轻量级Web应用框架&#xff0c;主要应用于后端框架&#xff…

作者头像 李华