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

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);
    }
}
相关推荐
xifangge202518 分钟前
AGENTS.md 怎么写?涵盖 Java、Python、Vue、Go 的 8 套开箱即用模板
java·vue.js·python
几何心凉20 分钟前
没有万能模型:我用 AiiOnly Token Plan,把多款大模型的长处拼进同一个项目
前端
Wang's Blog22 分钟前
Java框架快速入门: Spring Security+OAuth2之云服务集成与多因子认证设计
java·开发语言·spring
kyriewen23 分钟前
Claude Code 的额度今天缩水了17%——官方公告上写的是"永久提高25%"
前端·ai编程·claude
liangsheng_g1 小时前
SpringAOP拦截器链递归与事务钩子补偿源码实战
java·spring
雪芽蓝域zzs1 小时前
vue解构平铺VS对象包裹
前端·javascript·vue.js
SL_staff1 小时前
制造业私有化文档平台的技术实践:从知识孤岛到可追溯知识资产
java·spring·开源
风骏时光牛马2 小时前
从零到实战,完整AI学习路线
前端
SL_staff2 小时前
3天上线OKR系统:一名HR与1名工程师如何用JVS完成全栈交付
java·低代码·开源
三十而立洋3 小时前
深入理解 Monorepo:子包安装依赖
前端·前端工程化