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

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);
    }
}
相关推荐
21号 117 分钟前
9.进程间通信
linux·运维·服务器
EndingCoder1 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客2 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro3 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom3 小时前
javaweb -html -CSS
前端·javascript·html
朝新_3 小时前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir3 小时前
Calendar类日期设置进位问题
java·开发语言
打小就很皮...4 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡5 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
季鸢5 小时前
Java设计模式之状态模式详解
java·设计模式·状态模式