3 ms·
Singletons are globals, aren't they?
by sagichmal 5y ago
Singletons are globals, aren't they?
- dcow 5y agoKinda depends. Single instance things are usually globally referenced, but as long as you're referencing an instance and not a static thing they're not hard to convert to an "injected" dependency if you need to provide a test impl to some thing being tested. The real problem is static things that can't be swapped out because you're actually calling static methods on some class rather than sharing an instance and calling instance methods.
- Chris2048 5y agoI see your point, but aren't all those things architecture dependent i.e. dependent on the implementation of the prog-lang environment?
- dcow 5y agoYes.
- sagichmal 5y agoI understand "singleton" as "only one exists" and "it exists in the global scope". That is, if I create one instance of something in my `int main()` that's not enough for it to be a singleton. That's important, because there's more than one way to enter your program. Notably, tests don't flow through your main entrypoint.