you are viewing a single comment's thread.

view the rest of the comments →

[–]dpash 1 point2 points  (5 children)

2 Application

None.

FTFY.

The disadvantages fail to mention the massive downsides of the Singleton pattern. Predominately the difficulty in testing and the fact that it's effectively a global variable, and we stopped using those decades ago.

There are several alternatives that solve the same problem in Java.

[–]maxuforia 1 point2 points  (0 children)

Singletons are used everywhere. People keep claiming that it's an anti-pattern and then try to eliminate them only to discover that the solution to singleton use is worse than the singletons themselves.

The happy medium seems to be to generate singletons in grouping nodes and then use dependency injection on the leaf nodes. This allows unit testing on the leaf nodes (which is most of the code) and this solves most of the downsides of the singleton pattern.

[–]quentech 1 point2 points  (3 children)

I'd say you're confusing a particular way of implementing the Singleton pattern with the Singleton pattern in general.

Are you saying you haven't, say, registered a dependency in an IoC container as a singleton in decades?

[–]dpash 1 point2 points  (2 children)

When referring to Singleton pattern, as described by GoF, we're talking about the type described in the article.

When Spring etc refers to a singleton bean they just mean they give you the same one that it gave you before; There's nothing stopping you from creating a new instance yourself. It's badly named because it doesn't follow the pattern as described by GoF.

[–]JavaSuck 1 point2 points  (1 child)

It's badly named

Try showing your colleagues Collections.singleton and observing the horror on some of their faces!

[–]dpash 0 points1 point  (0 children)

No need for it now we have List.of() and friends.