Re:Singleton Is a Bad Idea
by nedbat on Sunday April 10, 2022 @03:21PM
(#30980695)
It's fine to have an object that is only instantiated once. But there's no reason to enforce that in the class.
[ Parent
| on HN ]
Re:Singleton Is a Bad Idea
by Etherlord87 on Sunday April 10, 2022 @03:47PM
(#30980989)
So, 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.