有没有比使用反射在继承层次结构中通过工厂方法实例化元素更好的方法?

Is there a better way than using reflection to instantiate elements by a factory method in an inheritance hierarchy?

我构建了一个继承层次结构,其中一堆具体的 classes 继承自一个抽象的超级 class AA 具有强制属性 String a 和可选 Map b 以及 xml 模型规范的模型元素。 ab 中可能的键值对都是 jaxp.NamedNodeList 的一部分。因此,要设置 ab 的值,我总是需要遍历列表并检查当前属性是否具有名称 "id" 并分别设置 [=14] 的值=] 或将键值对添加到 b。显然有人想将其外包给工厂方法等。但是,在抽象 super class A 中实现静态工厂方法显然是不够的,因为通过返回 A 的新实例,我需要在使用工厂方法。所以我想出了一个使用反射的解决方案,但我真的很不安全,因为没有更简单的方法来解决一个看似如此普遍的问题。

是否有更简单的解决方案?

这是我的工厂模式,在将 A 向下转换为 B 时会导致 ClassCastException SubclassB b = (SubclassB) AbstractSuperClassA.createWith(attributes); :

public static AbstractSuperClassA createWith(NamedNodeMap attributes) {
    Map<String, String> attributeMap = new HashMap<>();
    String a= null;
    for (int i = 0; i < attributes.getLength(); i++) {
        if (attributes.item(i).getNodeName().equals("id")) {
            a = attributes.item(i).getNodeValue();
        }
        attributeMap.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue());
    }
    if (a == null) {
        // throw RuntimeException
    }
    return new AbstractSuperClassA (identifier, attributeMap);
}

这是通用反射实现:

public static <T extends AbstractSuperClassA > T init(NamedNodeMap attributes, Class<T> clazz) {
    Map<String, String> attributeMap = new HashMap<>();
    String a= null;
    for (int i = 0; i < attributes.getLength(); i++) {
        if (attributes.item(i).getNodeName().equals("id")) {
            a = attributes.item(i).getNodeValue();
        }
        attributeMap.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue());
    }
    if (a== null) {
        // throw RuntimeException
    }
    try {
        Constructor<T> constructor = clazz.getConstructor(String.class);
        T newElement = constructor.newInstance(a);
        newElement.setAttributes(attributeMap);
        return newElement;
    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
        log.error(e.getMessage(), e);
    }
    return null;
}

您的 init 方法似乎需要一种方法来创建基于 given class 的 实例 在单个 String 值上。

在这种情况下,您不需要反射。不需要传入Class来实例化和初始化,你可以实现一种'strategy pattern'的形式,其中策略是可变的,只定义如何创建一个新的 ID-initialized 对象。

在 Java 8 及更高版本中,您可以为此使用函数式接口和 Lambda:

private <T extends AbstractSuperClassA > T init(NamedNodeMap attributes, Function<String,T> creator) {
  ...
  T newElement = creator.apply(identifier);
  ...
}

然后适当使用它,例如

B someB = init(attrs, B::new);
C someC = init(attrs, id -> {C c = new C(); c.setId(id); return c;});
...

然而,问题是您如何决定应该实例化哪个具体 class。在任何情况下,该逻辑都必须在某个地方进行编码,因此可能有更好的方法来连接逻辑以收集值和初始化新实例的逻辑。

是否要求实例在构造函数中接收id?还是可以稍后设置?