dimanche 21 août 2016

Would it be good or poor practice to make a class a singleton if its only purpose is to return a list of objects stored in a member HashMap?

Sorry if the title is a little unclear; allow me to elaborate. I have a class that essentially holds a few different HashMaps, mapping strings to lists of integers. This class has a method to build the different HashMaps (all from a parameter passed to the constructor). The only other method returns the appropriate values from one of the specified maps according to a key.

In essences, this is acting like a sort of "database." (For reasons, I cannot employ a standard DB)

My reasoning for thinking a singleton pattern could apply is because 1) this class may be "queried" from multiple other classes and 2) wouldn't each new instance recreate the maps all over again, wasting memory in duplication?

Would it make sense to simply make just the maps static if I can safely assume that I would not have to alter their contents once initialized? Please correct me if any assumptions I have made in this explanation are flawed.

Aucun commentaire:

Enregistrer un commentaire