工厂设计的一个小DEMO

工厂设计的一个小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项目的就可以了。
有更好的方式,请多指点,谢谢!
记录一下,学习不断进步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值