使用策略模式 + 自动注册机制来构建旅游点评系统的搜索模块


✅ 目标:

  • 搜索模块支持不同内容类型(攻略、达人、游记等)
  • 每种搜索逻辑用一个策略类表示
  • 自动注册(基于注解 + Spring 容器)
  • 新增搜索类型时,只需添加一个类 + 一个注解,无需改工厂、注册表等!

🛠️ 技术方案:

  • Spring Boot
  • 自定义注解 @SearchType("guide") 进行标记
  • 启动时由 Spring 自动扫描并注册到 Map<String, SearchStrategy>

📁 项目结构如下(Spring Boot)

复制代码
search-system-springboot/
├── SearchSystemApplication.java
├── annotation/
│   └── SearchType.java
├── strategy/
│   ├── SearchStrategy.java
│   ├── GuideSearch.java
│   ├── ExpertSearch.java
│   ├── TravelNoteSearch.java
│   └── SearchStrategyFactory.java
└── controller/
    └── SearchController.java

✅ 关键文件内容如下:


🔹 SearchStrategy.java(策略接口)

java 复制代码
package strategy;

public interface SearchStrategy {
    void search(String keyword);
}

🔹 @SearchType 注解(用于标记策略类型)

java 复制代码
package annotation;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SearchType {
    String value(); // 类型名称,如 "guide"
}

🔹 SearchStrategyFactory.java(策略注册器)

java 复制代码
package strategy;

import annotation.SearchType;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SearchStrategyFactory {

    @Autowired
    private ApplicationContext context;

    private final Map<String, SearchStrategy> strategyMap = new HashMap<>();

    @PostConstruct
    public void init() {
        Map<String, Object> beans = context.getBeansWithAnnotation(SearchType.class);
        for (Object bean : beans.values()) {
            Class<?> clazz = bean.getClass();
            SearchType annotation = clazz.getAnnotation(SearchType.class);
            String type = annotation.value().toLowerCase();
            strategyMap.put(type, (SearchStrategy) bean);
        }
    }

    public SearchStrategy getStrategy(String type) {
        SearchStrategy strategy = strategyMap.get(type.toLowerCase());
        if (strategy == null) {
            throw new IllegalArgumentException("不支持的搜索类型: " + type);
        }
        return strategy;
    }
}

🔹 示例策略类 GuideSearch.java

java 复制代码
package strategy;

import annotation.SearchType;
import org.springframework.stereotype.Component;

@Component
@SearchType("guide")
public class GuideSearch implements SearchStrategy {
    public void search(String keyword) {
        System.out.println("搜索攻略:" + keyword);
    }
}

🔹 示例策略类 ExpertSearch.java

java 复制代码
package strategy;

import annotation.SearchType;
import org.springframework.stereotype.Component;

@Component
@SearchType("expert")
public class ExpertSearch implements SearchStrategy {
    public void search(String keyword) {
        System.out.println("搜索达人:" + keyword);
    }
}

🔹 控制器 SearchController.java

java 复制代码
package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import strategy.SearchStrategy;
import strategy.SearchStrategyFactory;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchStrategyFactory factory;

    @GetMapping
    public String search(@RequestParam String type, @RequestParam String keyword) {
        SearchStrategy strategy = factory.getStrategy(type);
        strategy.search(keyword);
        return "搜索成功:" + type + " -> " + keyword;
    }
}

🔹 启动类 SearchSystemApplication.java

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SearchSystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(SearchSystemApplication.class, args);
    }
}

✅ 如何运行:

  1. 新建 Spring Boot 项目(Maven/Gradle 都可以)

  2. 创建上述包和类(annotationstrategycontroller

  3. 启动项目后访问:

    http://localhost:8080/search?type=guide&keyword=台北夜市

输出:

复制代码
搜索攻略:台北夜市

🚀 如何新增一个搜索类型?

比如新增 TravelNoteSearch

java 复制代码
@Component
@SearchType("travelnote")
public class TravelNoteSearch implements SearchStrategy {
    public void search(String keyword) {
        System.out.println("搜索游记:" + keyword);
    }
}

就完事了,无需改动任何已有类!开闭原则 ✔️


相关推荐
爱学习 爱分享2 天前
简单工厂模式和策略模式的区别
简单工厂模式·策略模式
2301_764441332 天前
python与Streamlit构建的旅游行业数据分析Dashboard项目
python·数据分析·旅游
ocr_ww3 天前
证件阅读器:更多文旅场景,持续赋能美好出行除了历史文化地标、自然奇观、邮轮码头、文博场馆、全域旅游五大核心场景
旅游
vx_biyesheji00013 天前
Python 全国城市租房洞察系统 Django框架 Requests爬虫 可视化 房子 房源 大数据 大模型 计算机毕业设计源码(建议收藏)✅
爬虫·python·机器学习·django·flask·课程设计·旅游
xcntime5 天前
Python中print函数如何实现不换行输出?
策略模式
q_35488851535 天前
计算机毕业设计源码:Python动漫智能推荐与可视化分析系统 Django框架 协同过滤推荐算法 可视化 数据分析 大数据 大模型(建议收藏)✅
python·scrapy·数据分析·django·课程设计·旅游·推荐算法
青春易逝丶6 天前
策略模式
java·开发语言·策略模式
恒星科通6 天前
旅游景区大功率高清晰应急疏散广播技术方案
安全·旅游·广播·应急广播
sg_knight6 天前
设计模式实战:策略模式(Strategy)
java·开发语言·python·设计模式·重构·架构·策略模式
liangshanbo12156 天前
[特殊字符] macOS 上的 zoxide:智能目录跳转终极指南
macos·策略模式