I am building a tool. I have a Collection Class that stores data gathered from files on network. (Why? because reading from network files was more time consuming than reading all data at once and storing data into an object)
I need to use the same instance in various different classes.
I looked over the pro’s and con’s of Singleton Classes, and decided to use it since I felt passing around a serialized object of my Collection Class was not a good idea.
Is this a good programming technique? or is there a better solution. I can provide more details if I am not being clear
Thanks,
– Ivar
P.S: I am not sure if this has been already asked on SO, please guide me to the right post if Yes.
It depends on what your singleton does. I’ll assume that our singleton gathers up files for some reason and then performs some sort of aggregation on them. If that’s the case, singleton migh be an OK idea.
If you’re using the singleton as a cache, that could make sense as well assuming the singleton object is the object responsible for maintaining the cache. Since I don’t know your platform, I can’t tell you to use an specific cache manager instead…
That being said, singletons are often an overused programming construct, and you should be somewhat wary of them. When making a decision about whether or kit to use one, ask yourself if the singleton manages a single constrained resource of some type. That’s usually the only reason I find myself using them.
It’s just about impossible to tell you I the desgn makes sense, though, I we don’t know what happens to the data and how it’s supposed to happen.