• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Singleton Pattern in java

 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 151
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic