3 ms·
Singleton became the 'enemy' during the 'Dependency Injection' craze, spawned in the Java enterprise environment. Singletons are a tool, that at some point you
by ardit33 5y ago
Singleton became the 'enemy' during the 'Dependency Injection' craze, spawned in the Java enterprise environment.
Singletons are a tool, that at some point you just can't avoid. We can discuss the way to create or access them, but saying they are just all bad is a diservice to younger engineers that are learning proper paterns.
- emodendroket 5y agoIt's like when you teach students not to begin sentences with "and." Sure, in practice, very sparingly, that can make sense. But the problem is that beginners think that almost everything is a case for them.
- nedbat 5y agoIt's fine to have an object that is only instantiated once. But there's no reason to enforce that in the class.
- Etherlord87 5y agoSo, as per the argument in the submission, you should create a function that manages that. And then you want to make sure the class is not instantiated other than with that function. So you recreate a singleton, but one of the methods is outside the class, is harder to find, easier to forget about when moving code around etc. Yes, making sure no one makes more than one instance of the class is defensive programming and you could consider it bad, but a lot of techniques in programming are defensive like that, especially in OOP.