但是,如果类处理多种类型,则 load 方法需要具有不同的名称,因为不能仅在返回类型上重载方法。 save 可以重载不同类型的参数,但为了保持一致性,两者应使用相同的命名约定。
If the class is only responsible for loading/saving TestPlan objects, and it has no other similar methods to load/save objects of other classes, it could be
class TestPlanXmlMarshaller {
public TestPlan load(InputStream xmlFile) {
...
}
public OutputStream save(TestPlan testPlan) {
...
}
}
This way, in accordance with the DRY principle, TestPlan and XML are not repeated unnecessarily in the method names.
However, if the class is dealing with multiple types, the load methods need to have distinct names since a method can't be overloaded on the return type only. save would be fine to overload with different type parameters, but for consistency the same naming convention should be used for both.
Personally I don't believe in artifically shortening names of my classes, so you may find this suggestion to be to long.
TestPlanToXMLFileProcessor
I considered Writer instead of Processor but would want to avoid potential confusion with the likes of FileWriter etc.
Ideally your class would be more generic and could used for some Objects other than a TestPlan Object, then you could reuse it in other projects, but we all know thats not always possible or even sensible.
我还建议在设计上稍作改变:您的两个方法可以读取和写入 XML 字符串;这将使它们更加通用和灵活。然后您还可以有两个相应的方法:
一个将 XML 文件读入 XML 字符串,然后对该字符串调用 parseTestPlanXml,
另一个调用 writeTestPlanXml,然后将生成的 XML 字符串写入 XML 文件
How about TestPlan2XmlConverter?
I would also recommend a slight change in design: Your two methods could read from and write to XML strings; this would make them a little more generic and flexible. And then you could have two more corresponding methods:
one to read an XML file into an XML string and then call parseTestPlanXml on that string
the other to call writeTestPlanXml and then write the resulting XML string to an XML file
发布评论
评论(6)
如果该类只负责加载/保存
TestPlan
对象,并且它没有其他类似的方法来加载/保存其他类的对象,那么可以这样,按照DRY原则,
TestPlan
和XML
不重复不必要地出现在方法名称中。但是,如果类处理多种类型,则
load
方法需要具有不同的名称,因为不能仅在返回类型上重载方法。save
可以重载不同类型的参数,但为了保持一致性,两者应使用相同的命名约定。If the class is only responsible for loading/saving
TestPlan
objects, and it has no other similar methods to load/save objects of other classes, it could beThis way, in accordance with the DRY principle,
TestPlan
andXML
are not repeated unnecessarily in the method names.However, if the class is dealing with multiple types, the
load
methods need to have distinct names since a method can't be overloaded on the return type only.save
would be fine to overload with different type parameters, but for consistency the same naming convention should be used for both.对我来说,这看起来非常像序列化器,所以如果您想具体的话,可以使用 TestPlanSerializer 或 TestPlanXmlSerializer 。
That looks very much like a serializer to me, so TestPlanSerializer or TestPlanXmlSerializer if you want to be specific.
两个类:
TestPlanXMLReader
和TestPlanXMLWriter
Two classes:
TestPlanXMLReader
andTestPlanXMLWriter
测试计划XMLStreamer
TestPlanXMLStreamer
就我个人而言,我不相信人为地缩短我的类的名称,因此您可能会发现这个建议太长。
我考虑了 Writer 而不是 Processor,但希望避免与 FileWriter 等类似的潜在混淆。
理想情况下,您的类将更加通用,并且可以用于除 TestPlan 对象之外的某些对象,然后您可以在其他项目中重用它,但是我们都知道这并不总是可能的,甚至是不明智的。
Personally I don't believe in artifically shortening names of my classes, so you may find this suggestion to be to long.
I considered Writer instead of Processor but would want to avoid potential confusion with the likes of FileWriter etc.
Ideally your class would be more generic and could used for some Objects other than a TestPlan Object, then you could reuse it in other projects, but we all know thats not always possible or even sensible.
TestPlan2XmlConverter 怎么样?
我还建议在设计上稍作改变:您的两个方法可以读取和写入 XML 字符串;这将使它们更加通用和灵活。然后您还可以有两个相应的方法:
How about TestPlan2XmlConverter?
I would also recommend a slight change in design: Your two methods could read from and write to XML strings; this would make them a little more generic and flexible. And then you could have two more corresponding methods: