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

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);
    }
}
相关推荐
LYFlied2 分钟前
【每日算法】 LeetCode 394. 字符串解码
前端·数据结构·算法·leetcode·面试·职场和发展
Dylan的码园4 分钟前
栈与stack
java·数据结构·链表
董世昌415 分钟前
break和continue的区别是什么?
java·jvm·算法
Chase_______7 分钟前
【JAVA基础指南(一)】快速掌握基础语法
java·开发语言
老蒋新思维9 分钟前
创客匠人:工作流嵌入式智能体,重构知识变现的效率底层
大数据·服务器·人工智能·重构·创始人ip·创客匠人·知识变现
小张帅三代10 分钟前
华为昇腾服务器ubuntu安装Anaconda步骤
运维·服务器·ubuntu
陈逸轩*^_^*12 分钟前
微服务常见八股(分布式seat, 网关,服务注册与发现、负载均衡、断路器、API 网关、分布式配置中心)
java·微服务
硕子鸽12 分钟前
UniApp + Dify 实战:详解 SSE 流式响应的解析与前端渲染
前端·uni-app·dify
爱笑的眼睛1112 分钟前
MLflow Tracking API:超越实验记录,构建可复现的机器学习工作流
java·人工智能·python·ai
lxh011312 分钟前
复原IP地址
前端·数据结构·算法