#Singleton vs "static" Classes

1 messages · Page 1 of 1 (latest)

glass wigeon
#

Why would I use a Singleton instead of a simple:
1. final
2. private constructor
3. static methods
Class?

I've heard that "static classes" cannot implement interfaces, but Singletons can... I don't know the language the article was talking about, but, in my code (Java), I can easily implement any interface and restrict acess directly into this class.

Here is an example of my code:

public final class CsvRandom {
    private CsvRandom(){}
    public static String getRandomValue(@NotNull String path) throws IOException, IllegalArgumentException { 
        ... 
    }
}
brave starBOT
#

<@&987246841693360200> please have a look, thanks.

brave starBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

warm turret
#

why would i use a singleton
u don't. singletons are considered an anti pattern and pretty much the whole community aligns on that - which is quite rare

#

the motivation for singletons usually stems from people being too lazy to specifiy dependencies properly and passing then around through a possibly long chain of objects

#

they abuse them as a tool for easy public access from anywhere

#

eventhough that's not actually what they are supposed to be used for to begin with

#

but thats how people use them and why they create them

#

that comes as a high cost though and usually it hits u back hard quite soon

#

and then people regret their choice

#

the proper alternative that fixes the same problems, without having am the downsides, is dependency injection

#

an idea that is very prominent and fleshed out in most frameworks, such as spring

glass wigeon
#

Omg, didn't know it is an anti-pattern

#

At my university, this "pattern" is very common to be taught

glass wigeon
#

Or even this is not used

warm turret
#

its a common pattern and probably the most prominent pattern in interviews when they ask u whether u know any antipatterns lol

warm turret
#

u also didn't restrict anything in ur example

#

the point of a singleton is to ensure that only a single instance of a class can exist, ever

#

people abuse it for other things, but thats what a singleton is meant for

#

that being said, it's not simple to create a true singleton. the only correct implementation is the enum-pattern, which is very rarely seen

glass wigeon
#

I mean, my point was:

Singleton restricts access to the only instance of some class.

So, it is kinda the same as declaring a class with static methods and acessing them directly. Since I have just one Singleton class to access the methods, I should access them directly into the class, instead of making this anti-pattern

#

I hope I've expressed myself well

#

I'm still learning

warm turret
#

ur talking about an utility class. a class with only static methods that can't be instantiated

#

like Math

glass wigeon
#

Yes

warm turret
#

but a singleton, being an actual instance, can have state

#

i. e. fields

#

which ur thing can't

glass wigeon
#

I mean, if it was possible, I could create a Singleton of Math class

#

But the only way is to access the things directly inside the Math class

warm turret
#

sure. but math has no state

#

there are no fields in it

#

a singleton can have state

glass wigeon
#

Doesn't it have like PI, e? Aren't that fields?

warm turret
#

no. those are constants

glass wigeon
#

Oh, now I think it makes sense

#

You're talking about mutability, right?

warm turret
#

point is, u could create a BankAccount singleton and it could have ur amount as a field

glass wigeon
#

Singletons can Have

warm turret
#

yes

#

also, the users of the singleton don't necessarily know that they are dealing with a singleton, as the interface looks like a regular object to them

glass wigeon
#

Uh Thinkfused But couldn't I go to the class with static methods and those kind of things and change those things there?

warm turret
#

with static fields? u could

glass wigeon
#

Instead of changing singleton's states

glass wigeon
warm turret
#

but what if u want to extend a class and implement 2 interfaces

glass wigeon
#

As Singleton only has one instance, I see no diference between that and static fields beeing changed

warm turret
#

i. e. u need to make an object

#

if ur forced to make an object bc u want to extend a class and implement a few interfaces, u have no choice

glass wigeon
warm turret
#

no

glass wigeon
warm turret
#

static methods are not part of inheritance or interfaces

#

if u want to implement an interface CanEat, u must implement it's non static eat() method

glass wigeon
#

Ohhh

#

Ohhh ok

#

Now it makes sense

warm turret
#

anyways, the part that's flawed is: why would u want to ensure that only a single instance can exist

#

singletons ensure that u can't create 2 instances of them, or 0

#

its always 1

#

and that's kinda flawed

#

there's no reason to enforce that

#

and if u do, it usually hits u hard a week later

glass wigeon
#

Got it

#

#

Can I save this? If I close will this be deleted?

warm turret
#

threads are never deleted

#

and u can use /bookmark to find it easily again

glass wigeon
#

Okay

#

Thanks, helped a lod

#

10/10

#

Legend

#

Bye, thanks

#

/help-thread close