当前位置: 首页 > news >正文

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

controller

@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"));


    }

}

第一种方式

 策略工厂

@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接口

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

TestService 实现类1

@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

@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接口

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

 Test1Service 实现类1

@Service
public class TestOne1ServiceImpl implements Test1Service{


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

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

第二种方式

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);
    }
}

http://www.dtcms.com/a/108672.html

相关文章:

  • DataFrame行索引操作以及重置索引
  • 第二期:深入理解 Spring Web MVC [特殊字符](核心注解 + 进阶开发)
  • Golang封装Consul 服务发现库
  • Linux进程管理与进程间通信
  • 如何将本地项目上传到Gitee的指定分支
  • 【2-6】数字调制
  • 蓝桥杯2024JavaB组的一道真题的解析
  • 云计算:基础、概念与未来展望
  • vue2拖拉拽做个模拟公式工具
  • 计算机视觉算法实战——基于YOLOv8的行人流量统计系统
  • 缺页异常导致的iowait打印出相关文件的绝对路径
  • Linux红帽:RHCSA认证知识讲解(十)使用 tar创建归档和压缩文件
  • RAG库搭建:从零开始,开启智能问答新世界
  • OpenCV 图形API(15)计算两个矩阵(通常代表二维向量的X和Y分量)每个对应元素之间的相位角(即角度)函数phase()
  • Ubuntu换Windows磁盘格式化指南
  • 二,<FastApi>FastApi的两个核心组件
  • JavaScript基础-window.sessionStorage
  • 通信算法之255:无人机频谱探测设备技术详解
  • 使用Kafka和kafkajs构建示例项目
  • 前端面试题(三):axios有哪些常用的方法
  • Linux上位机开发实践(从用板子到自己做板子)
  • 针对 SQL 查询中 IN 子句性能优化 以及 等值 JOIN 和不等值 JOIN 对比 的详细解决方案、代码示例及表格总结
  • Webpack vs Vite:现代前端构建工具的巅峰对决与选型指南
  • Linux学习七——进程回收
  • 一文详解QT环境搭建:Windows平台Qt安装配置指南
  • react 15-16-17-18各版本的核心区别、底层原理及演进逻辑的深度解析--react18
  • 电脑异常关机导致oracle监听器启动后自动停止
  • 蓝桥杯 web 请到下一步
  • Spread使用 配合report使用前篇
  • python爬虫基础讲解