Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern in java

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Could you tell me examples where singleton pattern is used in java programming apart from logger?

Thanks
Padma priya N.G.
 
Vijay Vishwa
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton class are those whose only instance exists in the application. Multiple instance would make no sense.
In Singleton class the constructor are not exposed, that means the default contructor is made private.
The only way to ge the instance is by calling the exposed stativ method which will return the same instance every time.

In JDK there are many example of Singleton class
Take example of java.lang.Runtime class

You can get the reference of Runtime class by calling



What Runtime class provides you can explore through Java API
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think singletons are good practice. In many if not all of the cases i implemented them i had to change them back because i needed more than one instance.
Even if you realy think you don't need more than one you should implement it in a non static way so you can easily change it later.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Petermann wrote:I dont think singletons are good practice.


Wrong. Singletons are extremely good practice if you know how to use them.
In one of my applications, I use a singleton to store the application's settings - why should I have many copies or read them every single time a user loggs on, if I can just store them in a singleton?

Singletons are used whenever you need one, and exactly one, object of something.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Wrong. Singletons are extremely good practice if you know how to use them.
In one of my applications, I use a singleton to store the application's settings - why should I have many copies or read them every single time a user loggs on, if I can just store them in a singleton?

Singletons are used whenever you need one, and exactly one, object of something.

I disagree. Singletons are almost always not good practice. One of the problems with singletons are that they make your code hard to test. Singletons, implemented with the classic static instance pattern, are hard to replace by mock or stub implementations, which is what you very often want to do in unit tests. You can use a dependency injection framework to get around this (when testing, you let the framework inject mocks instead of the "real" objects).

You can find a lot of discussion about why many people regard singletons as bad by searching for "why singletons are bad" or similar search terms.

Especially singletons which have (mutable) state (member variables) are bad, because those variables are essentially global variables, which can cause all sorts of issues, for example with thread safety.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
I disagree. Singletons are almost always not good practice. One of the problems with singletons are that they make your code hard to test. Singletons, implemented with the classic static instance pattern, are hard to replace by mock or stub implementations, which is what you very often want to do in unit tests. You can use a dependency injection framework to get around this (when testing, you let the framework inject mocks instead of the "real" objects).

You can find a lot of discussion about why many people regard singletons as bad by searching for "why singletons are bad" or similar search terms.

Especially singletons which have (mutable) state (member variables) are bad, because those variables are essentially global variables, which can cause all sorts of issues, for example with thread safety.


Oh, okay. I hadn't thought about testing (mainly because we use dependency injection at work), but you're right.
But that doesn't mean that there are no cases where you should use a singleton, does it?

(And I should remember to use "I disagree", like you did, instead of "Wrong" - my opinion is not the only valid, as other opinions are not automatically wrong if they differ - I sincerely apologize to Manuel.)
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. I suggest reading this.
In addition i would say that if you are designing software you might think its a good idea to use singletons.
You know of their dangers. You know it but others might not so they might assume its safe do do things with your singleton you did not intent which interfere with the controlflow of the rest of the program.
You say you are using them for configurations? I really hope you made them immutuable because you might end up in errors which are really bad to debug.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic