简体   繁体   中英

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

What I plan to do is create a large set of objects (~500). Currently I have a large Java file in which I create all objects in the following form:

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?

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. 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.

You are in my opinion also right about 500 manual add calls on a Collection being bad design. 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.
  2. There is also more room for mistakes, when you have so many identical repeating lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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