Hacker News new | ask | show | jobs
by jmount 3991 days ago
I thought there was a lot of literature pretty convincingly arguing that singleton is in fact an anti-pattern.
2 comments

There is, singletons are an anti-pattern, they're still interesting to discuss like this in terms of making them safe. In a language with both concurrency primitives and globals singletons have little (no) place anyway. I still learned from the post.
Shouldn't there be a disclaimer though?

Or, at least an example of when it would be a good idea to use a singleton over some other pattern?

Singletons are useful and wide spread concepts. What is an anti pattern is implementing them in a way that's not thread safe (which is made worse if your singleton is mutable).

Letting DI frameworks handle your singletons is the best way to get the best of both worlds.

No no, singletons are pathological liars http://misko.hevery.com/2008/08/17/singletons-are-pathologic...

Atomic mutable global state is still mutable global state. DI containers mean you don't need a singleton but can just create a single instance which lets you scope it and does not mix lifetime duration and object properties.