There are a couple of issues with Singletons. I will describe them below and then suggest alternative solutions. I'm not “Never use a singleton, or you're just a shitty coder,” I suppose they have their own capabilities. But this use is rare.
Thread safety. If you have a globally static Singleton, then it should be thread safe, because everyone can access it at any time. This is an additional overhead.
Unit testing is more complicated with Singletons.
This is a cheap replacement for global variables (I mean, one single at the end of the day, although it can have methods and other fancy things).
You see, this is not that Singleton is “terrible abominations”, but it is the first design that many new programmers encounter, and its “convenience” confuses its “pitfalls” (just put on some mechanisms on it and name it steam punk).
In your case, you mean models, and they are always “instances,” since they naturally reflect data. You may be worried about the cost of getting these cases. Believe me, they should be insignificant (up to the design of access to data, obviously).
So alternatives? Pass the model to places that require it. This simplifies unit testing and allows you to change the fundamentals of this model in the heart. It also means that you can take a look at the interfaces - this is a contract. Then you can create specific objects that implement these interfaces, and voila - you can easily test and modify the code.
In the singleton world, a single change to this singleton can fundamentally break everything in the code base. Not good.
source share