工厂设计的一个小DEMO,根据入参,工厂选择具体的实现类
首先看看效果
package com.dg.juanye.shop.controller;
import com.dg.juanye.shop.config.ThingsInfoSyncFactory;
import com.dg.juanye.shop.param.dto.Response;
import com.dg.juanye.shop.param.vo.ThingsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "JUANYE" +"/shopinfo")
public class ShopInfoController {
@Autowired
private ThingsInfoSyncFactory thingsInfoSyncFactory;
/**
* @return
*/
@PostMapping(value = "/getInfo")
public Response testFactory() {
ThingsInfo thingsInfo1 = new ThingsInfo("1", "1", "1");
System.out.println("=====>输入参数" + thingsInfo1);
thingsInfoSyncFactory.handle(thingsInfo1);
ThingsInfo thingsInfo2 = new ThingsInfo("2", "2", "2");
System.out.println("=====>输入参数" + thingsInfo1);
thingsInfoSyncFactory.handle(thingsInfo2);
ThingsInfo thingsInfo3 = new ThingsInfo("3", "3", "3");
System.out.println("=====>输入参数" + thingsInfo1);
thingsInfoSyncFactory.handle(thingsInfo3);
return Response.buildSuccess();
}
}
@Data
@AllArgsConstructor
public class ThingsInfo {
private String name;
private String stauts;
private String info;
}
public interface ShopInfoServer {
void doSomeThings(ThingsInfo thingsInfo);
}
@Service
public class ShopInfo1ServerImpl implements ShopInfoServer {
@Override
public void doSomeThings(ThingsInfo thingsInfo) {
System.out.println("------------->走了第一个实现 thingsInfo1");
System.out.println(thingsInfo);
}
}
@Service
public class ShopInfo2ServerImpl implements ShopInfoServer {
@Override
public void doSomeThings(ThingsInfo thingsInfo) {
System.out.println("------------->走了第二个实现 thingsInfo2");
System.out.println(thingsInfo);
}
}
@Service
public class ShopInfo3ServerImpl implements ShopInfoServer {
@Override
public void doSomeThings(ThingsInfo thingsInfo) {
System.out.println("------------->走了第三个实现 thingsInfo3");
System.out.println(thingsInfo);
}
}
@Component
@Lazy(false)
public class SpringContextUtil implements ApplicationContextAware, DisposableBean {
private static final Logger log = LoggerFactory.getLogger(SpringContextUtil.class);
private static ApplicationContext applicationContext = null;
public SpringContextUtil() {
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
public static void clearHolder() {
if (log.isDebugEnabled()) {
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
}
applicationContext = null;
}
public static void publishEvent(ApplicationEvent event) {
if (applicationContext != null) {
applicationContext.publishEvent(event);
}
}
public void destroy() {
try {
clearHolder();
} catch (Throwable var2) {
throw var2;
}
}
}
/**
* @version : 1.0
*/
public class ThingsInfoSyncFactory {
/**
* map获取对应的bean
*/
private ConcurrentHashMap<ThingsInfo, Class<? extends ShopInfoServer>> thingsInfoSyncHandlerBeanClassMap;
/**
* 缓存的map(不用再去执行getBean方法)
*/
private ConcurrentHashMap<ThingsInfo, ShopInfoServer> thingsInfoSyncHandlerMap;
/**
* 初始化集合(可以用枚举去定义数量)
*/
public ThingsInfoSyncFactory() {
thingsInfoSyncHandlerBeanClassMap = new ConcurrentHashMap<>(5);
thingsInfoSyncHandlerMap = new ConcurrentHashMap<>(5);
}
/**
* 获取impl.
*
*/
private ShopInfoServer getHandler(ThingsInfo thingsInfo) {
ShopInfoServer server =
thingsInfoSyncHandlerMap.getOrDefault(thingsInfo, SpringContextUtil.getBean(thingsInfoSyncHandlerBeanClassMap.get(thingsInfo)));
thingsInfoSyncHandlerMap.putIfAbsent(thingsInfo, server);
return server;
}
/**
* 根据入参,配置对应的选择具体实现的impl.
*/
public void addHandler(String infoName, String stauts, String info,
Class<? extends ShopInfoServer> beanClass){
ThingsInfo thingsInfo = new ThingsInfo(infoName, stauts, info);
thingsInfoSyncHandlerBeanClassMap.put(thingsInfo, beanClass);
}
/**
* 根据入参,选择实现的方法
*/
public String handle(ThingsInfo thingsInfo) {
try {
getHandler(thingsInfo).doSomeThings(thingsInfo);
} catch (Exception ex) {
return "Success";
}
return "Success";
}
}
@Configuration
public class ThingsInfoConfig {
/**
* 配置对应的入参与调用impl的关系
*/
@Bean
public ThingsInfoSyncFactory deviceSyncInfoSyncFactory (){
ThingsInfoSyncFactory factory = new ThingsInfoSyncFactory();
factory.addHandler("1", "1", "1", ShopInfo1ServerImpl.class);
factory.addHandler("2", "2", "2", ShopInfo2ServerImpl.class);
factory.addHandler("3", "3", "3", ShopInfo3ServerImpl.class);
return factory;
}
}
依赖的话正常springboot项目的就可以了。
有更好的方式,请多指点,谢谢!
记录一下,学习不断进步。