对于可以在 xml 文件中存储某些对象并读取它的类来说,合适的名称是什么?

发布于 2024-09-28 17:59:44 字数 1432 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我恋#小黄人 2024-10-05 17:59:44

如果该类只负责加载/保存TestPlan对象,并且它没有其他类似的方法来加载/保存其他类的对象,那么可以

class TestPlanXmlMarshaller {
    public TestPlan load(InputStream xmlFile) {
        ...
    }

    public OutputStream save(TestPlan testPlan) {
        ...
    }
}

这样,按照DRY原则TestPlanXML不重复不必要地出现在方法名称中。

但是,如果类处理多种类型,则 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.

薄凉少年不暖心 2024-10-05 17:59:44

对我来说,这看起来非常像序列化器,所以如果您想具体的话,可以使用 TestPlanSerializer 或 TestPlanXmlSerializer 。

That looks very much like a serializer to me, so TestPlanSerializer or TestPlanXmlSerializer if you want to be specific.

睡美人的小仙女 2024-10-05 17:59:44

两个类:TestPlanXMLReaderTestPlanXMLWriter

Two classes: TestPlanXMLReader and TestPlanXMLWriter

影子是时光的心 2024-10-05 17:59:44

测试计划XMLStreamer

TestPlanXMLStreamer

风柔一江水 2024-10-05 17:59:44

就我个人而言,我不相信人为地缩短我的类的名称,因此您可能会发现这个建议太长。

测试计划到XML文件处理器

我考虑了 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.

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.

若水般的淡然安静女子 2024-10-05 17:59:44

TestPlan2XmlConverter 怎么样?

我还建议在设计上稍作改变:您的两个方法可以读取和写入 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文