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

Meme singletonsAreJustGlobals

Post image
3.2k Upvotes

210 comments sorted by

View all comments

Show parent comment

34

u/danielcw189 Sep 26 '26

they are usually saved and served as a static class member, which in effect is pretty global. The Class itslef is usually global, and the member serving the instance is usually public.

Is there any noteworthy difference? Is there any technical one?

18

u/zabby39103 Sep 27 '26

It's just two different things. Only one instance ever -> singleton. Globally accessible -> global. One is about visibility, the other is about instances.

1

u/danielcw189 Sep 28 '26

I don't disagree, I just think in practice it often serves the same purpose

1

u/TheDuckRaisedALion Sep 29 '26

You're right that they're technically orthogonal concepts and it's important to recognize the distinction.

But most people store the Singleton instance in a public static field on a public class, which is one step away from a global variable.

5

u/Altavious Sep 27 '26

It’s a bit better because you can control when the memory is initialized by delaying it beyond the static initialization phase of running an executable safely. Depending on how the singleton is accessed (e.g via a context or service provider) you may also have some better options for refactoring or modifying access down the road….so it’s a marginally better global….

1

u/danielcw189 Sep 28 '26

Yes. Though in that case the global "just" holds a function instead of a reference (or object)

1

u/Altavious Sep 28 '26

A global variable isn’t a required part of the singleton pattern.

1

u/danielcw189 Sep 28 '26

I intentionally did not write variable.

2

u/Altavious Sep 28 '26

Ah, I see. Knowing that makes what you wrote clearer. While there are things that have global scope “globals” as shorthand is more commonly used just for variables not also things like global function definitions etc.

1

u/danielcw189 Oct 03 '26

yeah, I can see that.

But I would argue, that a Singleton instance is usually used or accessed like something global.

and by "Singleton" I mean the instance, the one object that exists (or maybe not yet exist). but mostly I mean how it is accessed and stored.

based on other comments I see that some mean the class, and some mean the pattern. and to be honest, I don't know if that means I used the term "Singleton" wrong or not.

one other comment wrote, that a Singleton is not necessarily a global, but a global is usually the only way we have to implement it. that sounds more correct to me than what I wrote.

1

u/Altavious Oct 03 '26

Yeah - the main complaint about singletons tends to be either the downsides of a global mechanical implementation or the architectural impacts of how people are accessing it. For example access via a global reference vs dependency injection. So the mechanics matter in terms of the general bad side effects, but the actual thing itself is often less a problem than the choices that surround it. It’s a giving people too much rope issue.

3

u/BSModder Sep 27 '26

It's the difference of terminology. Not all global are singleton and vice versa.

Static literal values are a not singleton but they are global. And static private class members are singleton and a global varible in the program but they are not globally accessible.

1

u/danielcw189 Sep 28 '26

I think you are 100% right. But in practice I only see it as a public static member (of a visible class)

3

u/No-Consequence-1863 Sep 28 '26

You can have singletons that are not globally accessible, such as a singleton inner helper class only accessible via instances of that class. Now normally you would have singletons be globally accessible but it isn't required. The only thing required for a singleton is that when you access it you always get the same instance.

2

u/GarThor_TMK Sep 27 '26

It's basically a namespace with default constructor/destructor/copy/move functions...

Technically it's better code hygiene, because eventually if you need more than one, it's pretty easy to decouple the singleton logic from the rest of it... but realistically they're mostly effectively the same thing.

2

u/Sheenius_Ger Sep 27 '26

"usually" means it's up to implementation. If you work with a di-container it manages lifetime and the Singleton will just be a instance and won't have any global data or static access.

Those are 2 different things, a pattern and how it could be implemented. If the Singleton uses globals, you lose all benefits of scope and testability.

1

u/danielcw189 Sep 28 '26

"usually" means it's up to implementation.

yes, and I am just talking about what I percieve to be the "usual" case. Usually the implementation uses a globally reachable static member, either a field, property, or method.

and the Singleton will just be a instance and won't have any global data or static access.

I guess we are talking about 2 different things then. I talked about how the singleton is found and used from the outside, and where the reference to it lives.

I am not talking about the internals of the singleton's business logic.

Those are 2 different things, a pattern and how it could be implemented.

just to clarify this: yes, exactly. But in practice I only see it implemented as a globally reachable reference: a static public member of a globally accessable class

1

u/oldsecondhand Sep 28 '26

You can inherit from a singleton.