将用户数据保存在文本文件或序列化对象中
我需要为我正在制作的程序保存书签。书签是 X 位置、Y 位置和缩放。我应该读取所有书签并将其写入文本文件,还是应该将它们放入 Bookmarks 对象中并序列化它。我感兴趣的是哪一种被认为是更好的面向对象设计。
I need to save bookmarks for a program I am making. The bookmarks are Xposition, Yposition, and zoom. Should I just read and write all of the bookmarks to a text file, or should I put them into an Bookmarks object and Serialize it. I am interested in which one would be considered better OO design.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这可能会回答您的问题。
我建议文本文件采用 XML 或 JSON 格式,以便更轻松地实现跨系统兼容性。
序列化很棒,但如果您决定更改正在序列化的对象,就会变得很棘手。
如果您决定序列化,请确保为您的对象定义一个
SERIAL_VERSION_UID
。您可以在此处阅读更多详细信息。This might answer your question.
I would recommend the text file to be XML or JSON format for easier cross system compatibility.
Serialization is great, but it gets tricky if you decide to ever change the object you're serializing.
If you decide on serializing make sure you define a
SERIAL_VERSION_UID
for your object. You can read about it in more detail here.我建议保存到具有跨平台的文本文件。事实上,这不是面向对象的考虑,因为当您使用文本文件方法时,您仍然可以在 oo 中进行编程,例如,具有文本文件源的数据访问对象不会违反 oo 概念。
i'll suggest saving to a text file with crossing platform. in fact it isn't an object oriented consideration since you can still programing in oo when you're using the text file approach, say, a data access object with text file source doesn't violate the oo concepts.
java序列化是脆弱的,所以如果您的目的是长时间保留一些配置数据,这些数据可能可以存在于程序的多个版本中,那么文本文件可能是您最好的选择。
格式实际上取决于你,xml,json,csv,
如果你决定使用java序列化,我建议将你的数据存储到HashMap中
并将你的访问器包装在地图上
java serialization is fragile, so if your intention is to keep some config data for long time, which possibly can live across multiple versions of your program, then probably text file is your best bet.
format is really up to you, xml, json, csv
if you decided to use java serialization i would recommend to store your data into a HashMap
and wrap you accessor around the map
无论如何我都会创建类书签。然后,我将使用
serialize()
和deserialize()
方法创建一个接口 BookmarkSerializer。然后我可以创建 BookmarkSerializer 的第一个实现。例如,它可能基于本机 java 序列化、纯文本、csv、xml 等。因此,我可以选择其中之一,实现它,然后在我的应用程序中使用它。当一种实现不能满足我的需要时,我可以添加其他实现。重点是,在这种情况下,除了序列化器本身的初始化之外,我不必更改应用程序中的任何内容。
我认为,在您的情况下,您应该从最简单的 CSV 实现开始,如果还不够,请根据需要添加尽可能多的实现。
I'd create class Bookmark anyway. Then I'd create a interface BookmarkSerializer with to methods
serialize()
anddeserialize()
. Then I can create my first implementation of BookmarkSerializer. It for example may be based on native java serialization, plain text, csv, xml etc. So, I can choose one, implement it and then use it in my application.When one implementation does not serve me enough I can add other. The point is that in this case I will not have to change anything in my application except initialization of the serializer itself.
I think that in your case you should start from the simplest CSV implementation and if it is not enough add as many implementations as you need.
使用对象通常是最好的 OO 设计。
为了便于支持,我使用文本文件,因为它易于查看(检查正确)和编辑(修复它)
Using Objects is usually the best OO design.
For ease of support, I use a text file as its easy to view (to check it right) and edit (to fix it)