Java – How to Store a List of Objects That Might Change

javaobject-oriented

I have set of Objects of the same class which have different values of their attributes.
and I need to find the best match from a function under given scenarios out of these objects.

In future these objects might increase as well.

Quite similar to the way we have Color class in awt.
we have some static color objects in the class with diff rgb values.
But in my case say, I need to chose the suitable color out of these static ones based on certain criteria.

So should I keep them in an arrayList or enum or keep them as static vars as in case of Colors.
because I will need to parse through all of them and decide upon the best match.
so I need them in some sort of collection.
But in future if I need to add another type I will have to modify the class and add another list.add(object) call for this one and then it will violate the open-close principle.
How should I go about it ?

EDIT:
To be more precise I have a roughly 7-8 branches of restaurants which wont increase that much..may be by 3-4 in few years and I need a function that will return the nearest Restaurant satisfying few other criteria.

and For that I need to parse through all of them to decide which one suits the customer the most.

Best Answer

Store the stores on disk. I would recommend using a database but for the small amount of data xml would do fine as well. On application start up read this list into a collection. You wouldn't need to create constants for each because you will just be querying the list when you need a store. This will keep you from having to update your code just to add a new store. Also since you are computing distances you will need a list of all the zip codes and their corresponding latitude and longitude so that you can compute the distance between two zipcodes. This could also be stored on disk and most likely will need updating more frequently than your store list. In addition, you could create an admin console that will let you add new stores and update zip codes without having to manually write to the storage mechanism you have chosen.

Related Topic