美文网首页
一个接口的多个实现

一个接口的多个实现

作者: 燃灯道童 | 来源:发表于2023-10-02 21:21 被阅读0次

    一个接口,根据不同的平台有不同的实现类。

    接口

    public interface QuotationTransformService {
    
    //    /**
    //     * 获取来源
    //     * @param vehicleQuotation
    //     * @return String
    //     */
    //    String getPlatform(VehicleQuotation vehicleQuotation);
    
        /**
         * 复制保单前的属性备份,根据不同的平台,备份不同的字段
         * @param vehicleQuotation
         * @return map
         */
        Map backUpQuotation(VehicleQuotation vehicleQuotation) ;
    
        /**
         * 还原备份的字段
         * @param vehicleQuotation
         * @param attrMap
         * @return
         */
        void restoreQuotation(VehicleQuotation vehicleQuotation,Map attrMap);
    
    }
    

    实现类

    public class GuangDongQuotationTransformServiceImpl implements QuotationTransformService {
    
        @Override
        public Map backUpQuotation(VehicleQuotation vehicleQuotation) {
            Map attributeMap = new HashMap();
            attributeMap.put("DealerType",vehicleQuotation.getVehicle().getDealerType());
            attributeMap.put("DealerCityCode",vehicleQuotation.getVehicle().getDealerCityCode());
            attributeMap.put("DealerName",vehicleQuotation.getVehicle().getDealerName());
            return attributeMap;
        }
    
        @Override
        public void restoreQuotation(VehicleQuotation vehicleQuotation, Map attrMap) {
            if(!attrMap.isEmpty()){
                if(attrMap.containsKey("DealerType")){
                    vehicleQuotation.getVehicle().setDealerType(attrMap.get("DealerType").toString());
                }
                if(attrMap.containsKey("DealerCityCode")){
                    vehicleQuotation.getVehicle().setDealerCityCode(attrMap.get("DealerCityCode").toString());
                }
                if(attrMap.containsKey("DealerName")){
                    vehicleQuotation.getVehicle().setDealerName(attrMap.get("DealerName").toString());
                }
            }
        }
    }
    

    工厂

    public class QuotationTransformServiceFactory {
        private static final Map<String, QuotationTransformService> quotationTransformServiceMap = new HashMap<>();
    
        static {
            quotationTransformServiceMap.put("GUANGDONG", new GuangDongQuotationTransformServiceImpl());
        }
    
        public static QuotationTransformService createQuotationTransformService(String platform) {
            QuotationTransformService quotationTransformService = quotationTransformServiceMap.get(platform);
            if (quotationTransformService == null) {
                throw new IllegalArgumentException("Invalid product type");
            }
            return quotationTransformService;
        }
    
    }
    
    

    调用

     Map attrMap = QuotationTransformServiceFactory.createQuotationTransformService(to.getPlateForm()).backUpQuotation(to);
    QuotationTransformServiceFactory.createQuotationTransformService(to.getPlateForm()).restoreQuotation(to, attrMap);
    

    如果只有一个实现的话,在工厂类中就不应该,为null时抛异常。添加map时应该还有其它比较好的方法

    相关文章

      网友评论

          本文标题:一个接口的多个实现

          本文链接:https://www.haomeiwen.com/subject/ibnqvdtx.html