4 ms·
I thought there was a lot of literature pretty convincingly arguing that singleton is in fact an anti-pattern.
by jmount 11y ago
I thought there was a lot of literature pretty convincingly arguing that singleton is in fact an anti-pattern.
- inglor 11y agoThere 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.
- jsprogrammer 11y agoShouldn'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?
- incepted 11y agoSingletons 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.
- inglor 11y agoNo no, singletons are pathological liars http://misko.hevery.com/2008/08/17/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.
- incepted 11y ago> No no, singletons are pathological liars http://misko.hevery.com/2008/08/17/singletons-are-pathologic.. http://misko.hevery.com/2008/08/17/singletons-are-pathologic.... No, only badly implemented ones are. Besides, this article is seven years old and it was written way before DI with Guice or Dagger became a thing. It's a very outdated perspective (and you will notice that AngularJS, which Misko contributes to, supports both DI and singletons).
- incepted 11y ago> DI containers mean you don't need a singleton but can just create a single instance So... a singleton.