正如你所看到的,没有回应,所以我不确定我的方式是否是“首选”方式。然而,这就是我最终所做的:
//定义了包装器超级类。我包的所有课程都是从这里下来的。
public abstract class WrappedSuperComplexObject{
protected boolean isValid;
protected String name;
public boolean isValid(){
return isValid;
}
public String getName(){
return name;
}
}
public class WrappedComplexBondObject extends WrappedSuperComplexObject{
//ComplexObject is an internal object created by JAXB
private final JAXBElement<Product> productElement;
public WrappedComplexBondObject(JAXBElement<Product> productElement) {
this.isValid = true;
this.name = ProductEnum.BOND;
this.productElement= productElement;
}
//Delegate all get/set class to the internal object
public String getElement1() {
return productElement.getElement1();
}
public String getParameter1() {
return productElement.getParameter1();
}
}
//然后有一个工厂类来验证和创建特定的产品实例。
public WrappedSuperComplexObject createWrappedInstance(JAXBElement<DataDocument> jaxbElement) throws WrappedException{
DataDocument document = jaxbElement.getValue();
WrappedValidationResult result = WrappedValidator.validate(document);
if ( !result.isValid() ){
throw new WrappedException(result.getMessage());
}
JAXBElement<Product> productElem = (JAXBElement<Product>) trade.getProduct();
String productName = productElem.getName().getLocalPart().toUpperCase();
WrappedSuperComplexObject product = null;
switch( WrappedProductEnum.valueOf(productName) ){
case BOND:
product = new WrappedComplexBondObject(productElem);
break;
default:
product = new UnsupportedProduct("Product is not Supported.");
break;
}
return product;
}
希望这有帮助。