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

一个接口的多个实现

作者: 燃灯道童 | 来源:发表于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时应该还有其它比较好的方法

相关文章

  • Java的接口

    接口将方法声明与实现分离,一个接口可以有多个实现类,一个类也可以实现多个接口。 定义接口 使用interface关...

  • Java_类和对象(多态 接口)

    接口 多个无关的类可以实现同一个的接口. 一个类可以实现多个无关的接口.但只能继承一个抽象类. 接口中的属性只能是...

  • 接口

    一个类实现多个接口,用,分开. 父类 对象名 = new 子类(); 接口 名字 = new接口的实现类 接口与抽...

  • 工厂模式

    一个接口,多个实现类,根据工厂类获取接口的实例。

  • Java基础-接口

    基础:声明接口关键字interface,实现接口关键字implements。一个类可以实现多个接口。 接口里内容:...

  • Castle Windsor Ioc 一个接口多个实现解决方案

    介绍 在Castle Windsor Ioc 一个接口多个实现解决方案中,介绍了三种解决一个接口多个实现的方案,在...

  • spring之接口实现类排序

    spring中接口的实现类排序 应用场景是项目中有个接口,这个接口有多个实现类,对这个多个实现类进行排序 举例一 ...

  • 接口

    接口 必须知道的接口特性 接口不可以被实例化 实现类必须实现接口的所有方法 实现类可以实现多个接口 接口中的变量都...

  • 默认接口方法

    简介 一个类可以实现多个接口,当一个类实现了多个接口,而这些接口中存在两个或两个以上方法签名相同的默认方法时就会产...

  • Android设计模式之(5)----抽象工厂模式

    抽象工厂模式 一个抽象的接口可以实现多个产品 一个抽象工厂可以实现多个具体工厂 例:一个公司有多个程序猿,有多个产...

网友评论

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

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