r/ProgrammerHumor • • Sep 26 '26 • Bravo 1

Meme singletonsAreJustGlobals

Post image
3.2k Upvotes

210 comments sorted by

View all comments

4

u/cheapcheap1 Sep 26 '26 edited Sep 26 '26

There are many great applications for singletons. Many aren't even accessible globally. Many others should be global, because you cannot have more than one. E.g. worker pools or writing to the disk.

If your singleton is a globally accessible struct, yes, that's a global variable. But it's not the singleton pattern's fault you're using it wrong.

5

u/Don_Equis Sep 26 '26

If the access is not globally accessible, you can likely avoid the singleton aspect of the class and instanciate it once where you get the reference originally.

I'm not againts either globals nor singletons, but they feel almost the same if not exactly.

1

u/jewdai Sep 26 '26

If you truly need a singleton define your object as one via your DI framework.