批评我的 JAXB 对象包装器
我有一个关于包装 jaxb 创建的类的问题,并且真的很想听听您的意见。
我的 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();
}
}
我的问题是:
上述方式是结束课程的首选方式吗?我的目标是不弄乱 xjc 创建的底层类;提供更好的命名 api(类和方法名称)。
我还想验证这些对象中的数据。因此我正在考虑使用 装饰器模式进一步包装 WrappedComplexObject1。这是推荐的方法吗?
最后,xsd 包含元素“Parameter”,其结构相同(仅包含一个值字段)。然而,当 xjc 创建 ComplexService 类时,为每个 ComplexObject 创建了一个新的 Parameter 类。
我应该担心只有一个“参数”包装类,还是应该为每个 ComplexObject 创建一个参数包装类。
任何建议、想法、代码示例都会非常有帮助。
谢谢
I have a question on wrapping a jaxb created class and would really like to hear your inputs.
My xsd looks a bit like:
<ComplexService>
<ComplexObject1>
<Element1></Element1>
<Parameter></Parameter>
</ComplexObject1>
<ComplexObject2>
<Element2> </Element2>
<Parameter> </Parameter>
</ComplexObject2>
...
<ComplexObject10>
<Element10> </Element10>
<Parameter> </Parameter>
</ComplexObjec10>
The class created after running the above xsd through xjc looks a bit like :
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
}
}
Now I want to create a wrapper around these CompleObjects as well the ComplexService class.
public class WrappedComplexObject1{
private final ComplexObject1;
public WrappedComplexObject1(){
complexObject1 = new ComplexObject1();
}
//Delegate calls to the underlying ComplexObject1
public String getServiceName(){
return complexObject1.getServiceName();
}
}
My questions are these:
Would the above way be the preferred way to wrap the class? My objectives are to not mess with the underlying classes created by xjc; to provide a better named api (Class as well as method names).
I also want to validate the data in these objects. Therefore I am thinking of using the
decorator pattern to further wrap WrappedComplexObject1. Would this be a recommended approach?Lastly, the xsd contains the element "Parameter" which is structurally the same (just contains one value field). However, when xjc created the ComplexService class, for every ComplexObject a new Parameter class was created.
Should I worry about just having one wrapper class for "Parameter" or should I simply create one Parameter wrapper classes per ComplexObject.
Any suggestions, ideas, code samples would be most helpful.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所看到的,没有任何回应,所以我不确定我的方式是否是“首选”方式。然而,这就是我最终所做的:
//Defined a Wrapper Super class。我封装的所有课程都将由此衍生。
//包装产品类
// 然后有一个工厂类来验证并创建特定的产品实例。
对于问题 3,我最终从主 xsd 中提取了“Parameter”元素,并为参数创建了一个新的 xsd。然后我引用了我的主 xjc 中的“参数”元素。
希望这有帮助。
Well as you can see there were no responses, so I am unsure if my way is the "preferred" way. However, this is what I ended up doing:
//Defined a Wrapper Super class. All the classes I wrapped would descend from this.
//Wrapper product class
// And then have a factory class that validates and creates the specific product instance.
For question 3, I ended up extracting the "Parameter" element from my main xsd and created a new xsd just for Parameter. I then referenced the "Parameter" element from my main xjc.
Hope this helps.