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

一、实现思路

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

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

二、整体设计结构

bash 复制代码
AbstractProductRecommendTemplate
        ↓
  filterByThreshold()   ← 策略①
        ↓
  groupByBrand()
        ↓
  selectByPriceLevel()  ← 策略②
        ↓
  buildResult()

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

java 复制代码
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、 策略接口

java 复制代码
public interface ThresholdMatchStrategy {
    boolean match(Product product, UserProfile user);
}

2、实现类

java 复制代码
@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、 策略接口

java 复制代码
public interface PriceSelectStrategy {

    List<Product> select(List<Product> products);
}

2、 高 / 中 / 低 价格策略

java 复制代码
@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个策略

java 复制代码
@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 使用方式

java 复制代码
@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);
}

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

  • 听力产品推荐

  • 活动商品推荐

  • 套餐组合

  • 分档定价

相关推荐
蜡笔小谢10 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
唐老板11 小时前
Spring AI 2.0 只支持 Boot 4
java
琳少_14 小时前
java程序设计笔记
java·开发语言·数据库·笔记
小荷才露尖尖角,早有蜻蜓立上头15 小时前
SpringBoot 定时任务
java·spring boot·后端
Helix25016 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
CoderYanger17 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠19 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
吃我一窝jio19 小时前
Java多线程核心知识点梳理
java·开发语言·笔记
这个昵称不容易1 天前
在线考试系统设计与实现:14 张表 158 个接口,组卷、答题、判分一条线走通
java·springboot·毕设
码云社区1 天前
Spring Boot上门按摩服务系统源码:上门到店服务目录与门店协同方案
java·spring boot·后端·上门服务·上门按摩·上门小程序