策略模式实际用处,改吧改吧直接用,两种方式

controller

java 复制代码
@RestController
@RequestMapping("admin/test")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {
    @Autowired
    private VideoFactory VideoFactory;

    @GetMapping("getList")
    public R getList(){

        // 第一种方式
        TestService testService = VideoFactory.chooseStrategy("2");
        System.out.println(testService.fetchVideo("1"));

        Test1Service testService1 = VideoFactory.chooseStrategy1("1");
        System.out.println(testService1.fetchVideo("dddd"));
        return R.ok().result(testService.fetchVideo("1"));

       // 第二种方式
        System.out.println(VideoServiceFactory.getService("1").fetchVideo("1"));


    }

}

第一种方式

策略工厂

java 复制代码
@Component
public class VideoFactory implements InitializingBean {
    @Resource
    private ApplicationContext applicationContext;

    private final Map<String, TestService> videoFactory = new ConcurrentHashMap<>();
    private final Map<String, Test1Service> videoFactory1 = new ConcurrentHashMap<>();

    public TestService chooseStrategy(String type) {
        return videoFactory.get(type);
    }

    public Test1Service chooseStrategy1(String type) {
        return videoFactory1.get(type);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        Map<String, TestService> videoFactoryMap = applicationContext.getBeansOfType(TestService.class);
        videoFactoryMap.forEach((key, val) -> videoFactory.put(val.supports(), val));

        Map<String, Test1Service> videoFactory1Map = applicationContext.getBeansOfType(Test1Service.class);
        videoFactory1Map.forEach((key, val) -> videoFactory1.put(val.supports1(), val));
    }
}

service接口

java 复制代码
public interface TestService {
     // 策略标识
     String supports();
     // 策略抽象接口
     String fetchVideo(String videoId);
}

TestService 实现类1

java 复制代码
@Service
public class TestOneServiceImpl implements TestService{


    @Override
    public String supports() {
        return "1";
    }

    @Override
    public String fetchVideo(String videoId) {
        System.out.println("1111111111111111");
        return "第一个"+videoId;
    }
}

TestService 实现类2

java 复制代码
@Service
public class TestTwoServiceImpl implements TestService{

    @Override
    public String supports() {
        return "2";
    }

    @Override
    public String fetchVideo(String videoId) {
        System.out.println("22222222222222");
        return "第二个"+videoId;
    }
}

service1接口

java 复制代码
public interface Test1Service {
     // 策略标识
     String supports1();
     // 策略抽象接口
     String fetchVideo1(String videoId);
}

Test1Service 实现类1

java 复制代码
@Service
public class TestOne1ServiceImpl implements Test1Service{


    @Override
    public String supports1() {
        return "1";
    }

    @Override
    public String fetchVideo1(String videoId) {
        System.out.println(videoId);
        return "最新的"+videoId;
    }
}

第二种方式

java 复制代码
public class VideoServiceFactory {
    private static final Map<String, TestService> serviceMap = new HashMap<>();

    static {
        serviceMap.put("1", new TestOneServiceImpl());
        serviceMap.put("2", new TestTwoServiceImpl());
    }

    public static TestService getService(String ip) {
        return serviceMap.get(ip);
    }
}
相关推荐
东风破_12 分钟前
danci 项目(三):从 JSON 数据到 AI Coding,真实项目里的数据清洗、Prompt 和工程规范
前端·后端·node.js
y = xⁿ21 分钟前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现
android·java·学习
东风破_33 分钟前
danci 项目(一):从需求到架构,一个单词学习系统为什么会这样设计
前端·后端·node.js
蒲公英eric1 小时前
从客户端到服务端:DVWA DOM 型 XSS 模块完整漏洞分析教程
前端·web安全·ai·xss·dvwa·ai安全
侧耳倾听1111 小时前
jwt使用简介
java·jwt
单线程_011 小时前
从案例分析 Vue3 Tokenizer+Parser 源码三
前端·javascript·vue.js
顶点多余1 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
AI人工智能+电脑小能手2 小时前
大白话说Java设计模式-46-责任链模式(业务实战篇)
java·设计模式·责任链模式·风控校验·servlet filter·spring interceptor·链式处理
Setsuna_F_Seiei2 小时前
前端转型 Agent 开发 03 之 Agent Tools - 给 Agent 装上手脚
前端·agent·ai编程
jay神2 小时前
【计算机毕业设计】基于SpringBoot的程序教学辅助系统
java·前端·vue.js·spring boot·后端·毕业设计·课程设计