当前位置: 首页> 房产> 家装 > 免费建网站的app_网站建设外包价格_今天国际新闻最新消息_网站开发公司

免费建网站的app_网站建设外包价格_今天国际新闻最新消息_网站开发公司

时间:2025/7/9 14:19:39来源:https://blog.csdn.net/xnian_/article/details/146983628 浏览次数:1次
免费建网站的app_网站建设外包价格_今天国际新闻最新消息_网站开发公司

controller

@RestController
@RequestMapping("admin/test")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {@Autowiredprivate 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 {@Resourceprivate 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);}@Overridepublic 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{@Overridepublic String supports() {return "1";}@Overridepublic String fetchVideo(String videoId) {System.out.println("1111111111111111");return "第一个"+videoId;}
}

 TestService 实现类2

@Service
public class TestTwoServiceImpl implements TestService{@Overridepublic String supports() {return "2";}@Overridepublic 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{@Overridepublic String supports1() {return "1";}@Overridepublic 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);}
}

关键字:免费建网站的app_网站建设外包价格_今天国际新闻最新消息_网站开发公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: