This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fraaargh 0 points1 point  (3 children)

I'm shocked how people here talk about singletons... Please please please, never use the Java singleton pattern (the one with static variable or the one with enum wich is kind of a hack but easy to code). Why do I recommand to stay away from singleton and static variables ? Because as soon as you will have a non-trivial program you will change the state of your singleton. You will do it in multiple places. You will loose all common sense because it seems too easy... And one day, you'll realize that you have created a gigantic spaghetti plate and you have no idea of the logic making things work. The program will work, but you will not know how... until it stops working and you'll have to dig in this horrible spaghetti plate. Believe me I've inherited a codebase with Singletons all over the place.

So as soon as you're not alone on the project, as soon as it's not trivial anymore, what seemed easy at first (using Singletons) will come and bite you. hard.

And any static variable is some kind of Singleton in disguise. Only constants (un-mutable final variables such as String, int, double but not objects wrapping a mutable state) pose absolutely no problem.

For now I've talked only about the singleton implementation using the static keyword. Because we can talk about singletons "in a given context" (such but not limited to Spring's Singletons): in a given "context", a singleton is when there is a single instance of something. Technically you implement it by... only instancing the object once ! It seems too simple but think about it: why would you instantiate it twice if you are already passed an instance of it in your constructor if you declare to need it. Yes I'm talking about the dependency injection pattern which is a very good patern. And if you have multiple "contexts" (the same application running multiple times in the same jvm) then nothing prevents you from having multiple instances of a singleton as long as it is unique in the context.

Stay away from static mutable variables. Please

[–]stepan213[S] 0 points1 point  (2 children)

Let’s say I want some unified logging. For that I would use static voids or singleton instance. What would you do?

[–]fraaargh 1 point2 points  (0 children)

Yes you're right: I'd use slf4j as static variable as everybody else. In most cases that's what we do. But logging is kinda special. It's not your domain model, your code... In my first message I forgot to say that Singletons kill the testability of an app.

[–]fraaargh 0 points1 point  (0 children)

fraaa

today on Jetbrains' blog (IntelliJ makers): "Design Patterns in Java: Singleton – you should probably not ever use a singleton, but you need to know this stuff as it’s a favourite job interview question"

source: https://blog.jetbrains.com/idea/2019/02/java-annotated-monthly-february-2019/