What's the point?

What's the point?

Attached: Screen Shot 2022-08-13 at 20.52.36.png (678x500, 165.99K)

Without a singleton you might create another instance of a class when you don’t want to.

...

Because static is too scary

This.

Singleton also prevents you from using memory before you need it. If I declare a static variable, I need to initialize it at declaration time (which may not be needed) or check whether it’s been initialized whenever I use it (which makes code more verbose). Singleton lets me “lazy load” the object if and when I need it, without caring about implementation details.

most easily abusable access pattern

Attached: getsomeobjectsfromanywhereyouwant.jpg (1104x1104, 134.2K)

It's so you have one instance of a service class. It made sense before dependency injection frameworks were a thing. Nowadays if you are adding a static get Instance method you are probably doing something wrong.

Singleton: a pattern that helps stupid programmers avoid doing something "accidentally," because they're so disorganized that it's apparently a common issue for them to accidentally do things like this.
Until the day they realize they actually want to do the thing singleton prevents them from doing. Then they realize how fucked they are.
"I like singleton because I don't have to worry when something is initialized." Sounds nice until the day you realize you need to initialize it at a certain point and you can't because all your interactions are now through the simpleton mechanism.
Any programmer who thinks singleton is a good thing is outright identifying themselves as incompetent. At best its a global variable with a bunch of stupid OOP boilerplate on top. So I can't even allow its use in small throwaway scenarios. In that case you could use a global variable and it would be even better.

>Singleton lets me “lazy load” the object if and when I need it, without caring about implementation details.
Wrong. You can do that without singleton. Singleton means that you bake the lazy initialization into the class definition so that's the only way the class can be used.
You can do lazy initialization without singleton.

>Sounds nice until the day you realize you need to initialize it at a certain point and you can't because all your interactions are now through the simpleton mechanism.
You're being a bit stupid here. The singleton in OP's diagram has exactly one method, which initializes it.
If you honestly can't see the value of loading static variables lazily, you might just be the bad programmer that you are talking about and your programs will probably have issues with long startup times at some point.

>You can do lazy initialization without singleton.
Example?

Singleton is only useful if there are other methods as well. It wouldn't make sense to only have an init function.
>If you honestly can't see the value of loading static variables lazily
You don't need singleton to load variables statically. Singleton just means you bake that into the class.
>you might just be the bad programmer that you are talking about and your programs will probably have issues with long startup times at some point.
Interesting that you assume the only two options are lazy initialization versus initializing everything at startup. You need to broaden your horizons a bit.
And finally, I must reiterate I am not even against lazy initialization in general (although I prefer not to use it myself). What I am against is the creations of classes which can only be lazy initialized. It's not a modular design. Classes shouldn't initialize and manage their own instances. It violates single responsibility.

Create a static object in function scope and return a reference to it from the function.
We're really at the point that singleton-ites are so deep into their pattern they can't see how basic things could be done outside of it?

>2022
>still not using dependency injection
ngmi

so dont

Retard proofing because globals are icky.

guessing you dont write tests either because you just dont make mistakes ?

Accidentally creating an instance is a very strange mistake to make, though. And you can also make your class not copyable, which eliminates the only real risk of doing it.
The biggest issue with singleton is it locks you into your design. Someday you will realize you actually need two of the object, or you need to initialize it in a certain way. In either of those cases you will get very stuck. The singleton api will be deeply embedded, and unpredictable because of the global accessibility. It's a maintenance nightmare.
I've worked at a company where people called the singleton access function just to initialize the objects, thereby overriding the lazy initialization. Even then they had trouble controlling initialization as singletons would internally initialize other sinerons during their initialization.

>Create a static object in function scope and return a reference to it from the function.
So in Java, something like this:
public class C {
private static final C INSTANCE = new C();
public static C getInstance() {
return INSTANCE;
}
}

Because that's just a singleton.

>You need to broaden your horizons a bit.
You either initialise something before you need it, or when you need it.
The only other option I can imagine is initialising it after you need it, which is not very useful.
>Singleton just means you bake that into the class.
This is only for cases where it only makes sense for there to be one of them.
It bakes it into the class in the same way that having a global variable bakes it into the variable, because a singleton is just a collection of global variables.
A singleton with only one attribute is literally the same thing as a global variable; there is no difference.

The risk isn't creating an instance. It's assigning the instance you just created to the global variable for the singleton.
Singleton solves this by making the global variable private to the singleton class.
>The biggest issue with singleton is it locks you into your design.
It doesn't do this. You can just factor all the fields out into another class, instance that class in the singleton (redirecting the singleton's methods to the new class), and then instance the other class as many times as you like.
No API change and the problem is solved.
Unless your issue is just having any global state, in which case, yes that can be a problem.

Honestly, I'm getting Poe's law. I can't tell if you're trolling or not because singleton-ites really are this stupid in general.

>Because that's just a singleton.
It's a singleton because the class is creating its own instance.
public class C {
public static B getInstance() {
static B instance = new B();
return instance;
}
}

Now do you understand the difference between singleton and general lazy initialization? Or is it still confusing for you?

>You either initialise something before you need it, or when you need it

I am not generally against lazy initialization as I have said in like 3 comments here so let's just drop it. But I do think you overemphasize the importance of the choice.

>This is only for cases where it only makes sense for there to be one of them.
Which is basically never.
>It bakes it into the class in the same way that having a global variable bakes it into the variable
Yes. See the difference between a CLASS and a VARIABLE? THAT'S THE WHOLE POINT, DUDE.

The entire issue with singleton is that prevents you from reusing a class because you've decided when you wrote it there should only be one. The issue is that often you eventually need more than one and in that case you're fucked.

>because a singleton is just a collection of global variables
Yeah, and I think it's been known for a while that global variables are not a good thing.

>A singleton with only one attribute is literally the same thing as a global variable; there is no difference.
Well, besides more boilerplate? Just use a global variable, that's what variables are for.

You are basically making a class into a variable with only drawbacks and no upsides. The whole point of classes that they can be reused (hence - a class of instances).

> It's assigning the instance you just created to the global variable for the singleton.

You mean the issue is modifying the singleton? Make it a constant?

>It doesn't do this. You can just factor all the fields out into another class, instance that class in the singleton (redirecting the singleton's methods to the new class), and then instance the other class as many times as you like.
lol, so the way to get around it is to remove it and use a bunch of boilerplate to maintain your singleton api? Really convincing.
Why not just start with the other class instead of starting with the singleton? Which by the way - is what I've been suggesting all along.

>It doesn't do this.
I forget to mention. All your call sites will be expecting singleton. So they are all locked into that api. Your solution just explains how to maintain the singleton api, but there's more to it than that.