4 ms·
It's fine to have an object that is only instantiated once. But there's no reason to enforce that in the class.
by nedbat 4y ago
It's fine to have an object that is only instantiated once. But there's no reason to enforce that in the class.
- Etherlord87 4y 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.