Java设计问题:这是一个好的设计吗?
我正在编写一个通过 HTTP/XML 与远程服务器通信的 Java 客户端。
服务器以 XML 格式向我的客户端发送命令,如下所示:
<command>
<name>C1</name>
<param>
.....
</param>
</command>
大约有 10 个或更多不同的命令(C1、C2...),每个命令都有不同的参数集。
我的客户端将处理该命令,然后用执行结果响应服务器,如下所示:
<C1>
<code>200</code>
<desc>xxx</desc>
</C1>
我只熟悉 C,但对 Java 和 OOP 很陌生,
所以,我的问题很简单,如何以OOP的方式优雅地设计以下逻辑?
1.将XML字符串转换为XML对象
2.根据XML的name元素找到对应的执行器,并解析参数
3.与参数一起执行命令
4.将结果转换为XML对象
5.将XML对象转换为XML字符串
这是一个好的设计吗?
1.为每个命令定义一个抽象基类和许多子类,其中包括以下方法:
void parseCommand(MyXMLObject obj);
void execute();
MyXMLObject generateResult();
或者只是一个简单的方法:
MyXMLObject execute(MyXMLObject obj);
以及这些字段:
String mCommandName;
int mRetCode;
String mRetDesc;
2.然后定义一个工厂类以返回其中一个子类的实例基于 XML 对象。
3.逻辑部分代码:
MyXMLObject obj = MyXMLUtil.getXMLObject(XMLString);
MyCommand command = MyCommandFactory.getCommand(obj);
MyXMLObject retObj = command.execute();
String retStr = MyXMLUtil.getString(retObj);
...//network operation
I am writing a Java client that communicates with a remote server via HTTP/XML.
Server sends commands to my client in XML format, like this:
<command>
<name>C1</name>
<param>
.....
</param>
</command>
There is about 10 or more different commands (C1, C2, ...), each of them has different set of params.
My client will process the command, then response server with a execute result, looks like this:
<C1>
<code>200</code>
<desc>xxx</desc>
</C1>
I am only familiar with C, but very new to Java and OOP,
so, my question is simple, how to design the following logic gracefully in a OOP way?
1.Convert the XML string to an XML Object
2.Find correspoding executor based on 'name' element of the XML, and parse the params
3.Execute the command along with the params
4.Convert the result to an XML Object
5.Convert the XML Object to an XML string
Is this a good design?
1.Define an abstract base class and many sub-classes for each command, which include the following method:
void parseCommand(MyXMLObject obj);
void execute();
MyXMLObject generateResult();
or just a simple method:
MyXMLObject execute(MyXMLObject obj);
and these fields:
String mCommandName;
int mRetCode;
String mRetDesc;
2.Then define a factory class to return an instance of one of the sub-classes based on the XML Object.
3.The logic part code:
MyXMLObject obj = MyXMLUtil.getXMLObject(XMLString);
MyCommand command = MyCommandFactory.getCommand(obj);
MyXMLObject retObj = command.execute();
String retStr = MyXMLUtil.getString(retObj);
...//network operation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原则上,是的,事情就是这样运作的。但我认为你应该区分不同的关注点。
基本上我会说你有一个需要执行命令并返回结果的服务层。该层应该对 XML 一无所知,因此我会设计纯 Java 对象模型,然后担心将其绑定到 XML 基础结构,可能会使用现有的 XML 映射技术(如 XStream)。
In principle, yes, that's how things work. But I think you should separate the different concerns.
Basically I'd say you have a service layer that needs to execute commands and return the results. This layer should know nothing about XML, so I'd design the plain Java Object model and then worry about tying it to your XML infrastructure, probably using an existing XML mapping technology like XStream.
正如其他人所说,设计看起来相当不错。为了让您的生活更轻松,您应该看看 Simple XML 来解析 XML -> Java 并从 Java 对象创建 XML。
As the others said, the design looks pretty good. To make your life easier, you should have a look at Simple XML to parse XML -> Java and to create XML from Java objects.
总的来说,这是一个很好的设计(你可能需要更多的接口等,并且有各种改进)。
更大的问题是,这在很多方面都是对轮子的重新发明。有许多框架可以处理从 POJO(java 对象)到结构化文本格式(例如 XML 或 JSON)的映射。最重要的是,命令框架有许多通用的实现。在提供您自己的实现之前,可能值得研究可用的选项。
Generally speaking, it is a good design (you probably need more interfaces and such, and there are various refinements).
The greater problem is that this is, in many ways, a reinvention of the wheel. There are numerous frameworks that handle mapping from POJOs (java objects) to structured textual formats (such as XML or JSON). On top of that, there are many general implementations of command frameworks. It might be worth investigating available options before you provide your own implementation.