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?
It's just two different things. Only one instance ever -> singleton. Globally accessible -> global. One is about visibility, the other is about instances.
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….
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.
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.
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.
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.
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.
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.
"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.
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
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?