简体   繁体   English

如何存储大量对象的arguments?

[英]How to store the arguments for a large amount of objects?

What I plan to do is create a large set of objects (~500).我打算做的是创建一大组对象(~500)。 Currently I have a large Java file in which I create all objects in the following form:目前我有一个大的 Java 文件,我在其中创建以下形式的所有对象:

MyCollection.add(String name, int strength, int size, int price);

But with this it takes a lot of space and I don't think it is the best way.但是这样会占用很多空间,我认为这不是最好的方法。 I don't really know my way around in this area.我真的不知道我在这方面的路。 How is such a problem normally handled?此类问题一般如何处理? Would it make sense to create a CSV file or another database?创建 CSV 文件或其他数据库是否有意义?

Depends on where this data comes from.取决于这些数据的来源。 Do you generate it on runtime?你在运行时生成它吗? If so - consider abstraction , so that you define a proper structure of objects and encapsulate this data and the process of creating it properly.如果是这样 - 考虑抽象,以便您定义适当的对象结构并封装此数据以及正确创建它的过程。 If the data is static then yes, a database of any sort makes more sense.如果数据是 static 那么是的,任何类型的数据库都更有意义。 Specifically this means making your data persistent and simplifying the process of reading it as much as possible, eg reading the data via loop, encapsulating it in an objcect MyFancyEntry , then adding it to your collection.具体而言,这意味着使您的数据持久化并尽可能简化读取它的过程,例如通过循环读取数据,将其封装在一个对象MyFancyEntry ,然后将其添加到您的集合中。

You are in my opinion also right about 500 manual add calls on a Collection being bad design.在我看来,你对一个Collection的 500 次手动add调用也是正确的,这是一个糟糕的设计。 The reason is simple - maintainability.原因很简单——可维护性。

  1. It is hard to change the structure quickly, if upon a structural change you suddenly need to edit 500 lines by hand.如果在结构更改时突然需要手动编辑 500 行,则很难快速更改结构。
  2. There is also more room for mistakes, when you have so many identical repeating lines.当你有这么多相同的重复行时,也有更多的错误空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM