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

Meme singletonsAreJustGlobals

Post image
3.2k Upvotes

210 comments sorted by

View all comments

412

u/mateusfccp Sep 26 '26

That's not even what Singletons are for. The point of using it is not having two instances of the same object, but it doesn't mean it is a global, as the existence of the (unique) instance may still be scoped.

39

u/brazzy42 Sep 26 '26 edited Sep 26 '26

Exactly. It's the best proof that nobody actually read the GoF book. A static field and getter method were never the essence of the pattern, they're one possible way to implement the pattern.

103

u/AppleSmoker Sep 26 '26

Yeah wtf man this meme is completely wrong

27

u/Yashema Sep 26 '26

I'm still glad OP made it cause the comments are getting into juicy discussions about design patterns. 

25

u/high_throughput Sep 27 '26

r/ProgrammerHumor's tagline should be "if those kids could program, they'd be very upset"

3

u/irongi8nt Sep 26 '26

Let's talk static variables in that meme 

14

u/developer-mike Sep 26 '26

People are debating two different concepts for global here.

Global scope could mean globally public, or it could mean globally shared state. Globally shared state can be locally scoped, such as private static class members.

Singletons are by definition globally shared state, which is at least half of the problem of global variables -- any two systems that interact with the singleton are indirectly coupled and can't be uncoupled which can easily still manifest as spooky action at a distance.

Unless it's a DI system limiting to one object per module but you can have multiple modules. Which is different, of you can have two different instances in two different modules, it's not a strict singleton.

18

u/jaladreips271 Sep 26 '26

Wow, all these years and only this post gave me the enlightment. Noone ever pointed out to me that singletons can be scoped.

3

u/wobblyweasel Sep 26 '26

and the initialization is synchronized in the languages that natively support them

1

u/SamSlate Sep 27 '26

i still don't get it, isn't there always a scope?

2

u/jaladreips271 Sep 27 '26

I've never seen a singleton that didn't have a global scope. But frankly I don't see a lot of singletons in general.

8

u/irongi8nt Sep 26 '26

I don't think the meme author knows what static variables are or why you encapsulate a constructor with a Singleton 

10

u/danielcw189 Sep 26 '26

You are correct. But in practice I have only seen global ones.

2

u/Manueluz Sep 27 '26 edited Oct 14 '26

The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.

Point teapot versed yarn amusing paddle humor ripe

2

u/Arctos_FI Sep 26 '26

I know a language where globals are singletons, sqf (the scripting language for arma).

It doesn't have scope so if you use same name for global variable in two separate scripts they are the same variable. And globals are program wide singletons so even if you use the same name in two separate addons (addons are like what mods are for minecraft) they act as singleton. And because of dynamic typing if you use it as number in one (there isn't separate int, float and double types, everything is just a number) and string in other they will overwrite each other and throw errors just when trying to do something else with the value than set it. And there isn't any keywords (so no private or public, and technically there is one that makes it network wide singleton) but the name gets set by naming convention alone ("var" is always public and "_var" is always private)

1

u/Tyfyter2002 Sep 26 '26

If the instance is scoped there can be another scope with another instance, being global is necessary in order for it to be unique.

1

u/Manueluz Sep 27 '26 edited Oct 14 '26

The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.

Dam compare squash imminent square truck ten grasshopper compare expansion

-1

u/EishLekker Sep 26 '26

No, that doesn’t follow. Scoped can simply mean that it’s not globally accessible. It can still be just a single instance.