代码之家  ›  专栏  ›  技术社区  ›  CaptainHastings

批评我的JAXB对象包装器

  •  1
  • CaptainHastings  · 技术社区  · 15 年前

    我的xsd看起来有点像:

    <ComplexService>
        <ComplexObject1>
            <Element1></Element1>
           <Parameter></Parameter>
        </ComplexObject1>
    
       <ComplexObject2>
            <Element2> </Element2>
            <Parameter> </Parameter>
       </ComplexObject2>
    
       ...
    
      <ComplexObject10>
            <Element10> </Element10>
            <Parameter> </Parameter>
      </ComplexObjec10>
    

    通过xjc运行上面的xsd之后创建的类看起来有点像:

    public class ComplexService{
    
    ComplexObject1 object1;
    ComplexObject2 object2;
    ...
    ComplexObject10 object10;
    
    public static class ComplexObject1{
    //Accessors and mutators on ComplexObject1
    }
    
    public static class ComplexObject2{
    //Accessors and mutators on ComplexObject1
    }
    
    ...
    
    public static class ComplexObject10{
    //Accessors and mutators on ComplexObject1
    }
    
    }
    

    现在我要为这些CompleObjects以及ComplexService类创建一个包装器。

    public class WrappedComplexObject1{
    
    private final ComplexObject1;
    
    public WrappedComplexObject1(){
    complexObject1 = new ComplexObject1();
    }
    
    //Delegate calls to the underlying ComplexObject1
    public String getServiceName(){
    return complexObject1.getServiceName();
    }
    
    }
    

    1. 以上的方式是不是最好的方式来结束课堂?我的目标是不破坏xjc创建的底层类;提供一个更好的命名api(类和方法名)。

    2. 我还想验证这些对象中的数据。因此,我正在考虑使用

    3. 最后,xsd包含在结构上相同的元素“Parameter”(只包含一个值字段)。但是,当xjc创建ComplexService类时,为每个ComplexObject都创建了一个新的参数类。

    我应该担心“Parameter”只有一个包装类,还是应该简单地为每个ComplexObject创建一个参数包装类。

    谢谢

    1 回复  |  直到 15 年前
        1
  •  1
  •   CaptainHastings    15 年前

    正如你所看到的,没有回应,所以我不确定我的方式是否是“首选”方式。然而,这就是我最终所做的:

    //定义了包装器超级类。我包的所有课程都是从这里下来的。

    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;     
    }
    

    希望这有帮助。