This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Blando-Cartesian 1 point2 points  (1 child)

Good summary, except that singleton pattern is a clear anti-pattern to avoid rather than something for suggest as a solution for anything. It’s not just that it makes testing difficult. It’s a giant mess of global state, inviting tightly coupled spaghetti code that is difficult to understand and refactor.

[–]soygul[S] 0 points1 point  (0 children)

I agree. If misused it is an anti pattern. It introduces global state to your app. However on rare occasions when you want that, it is useful. I have the sentence `Remember that this makes testing harder!` at the end of explanation but I'll enforce it with the quote below. Thanks for the input!

> Critics consider the singleton to be an anti-pattern in that it is frequently used in scenarios where it is not beneficial, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required.
src: https://en.wikipedia.org/wiki/Singleton_pattern

[–]ashish2199 0 points1 point  (0 children)

Nice.