Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton in Java

 
Ali Ekber
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone please briefly explain what singleton in Java is or point me some online resources? I am being asked this question during interviews constantly. Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Searching this board for "singleton" gives plenty of results. Here is a good recent one...

http://www.coderanch.com/t/376041/java/java/singleton
 
Hung Yee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some more resources on Singleton for you:
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/view?SingletonPattern
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Ekber:
Could someone please briefly explain what singleton in Java is or point me some online resources? I am being asked this question during interviews constantly. Thanks.


Note that Singleton is not a Java concept. It fits under the catgory of "Design Patterns" which are generic recipes, so to speak, to solve common problems that come up with many Object Oriented programming languages. I strongly suggest that you learn about Design Patterns in general as well as study how to implement them in Java (or any other language for that matter).

Layne
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to speak now against the doctrine.
Singleton is being portrayed as a design pattern, and has been for quite some time. Unfortunately, like a lot of things in Engineering/Science, the truth is in fact, counter-intuitive. Singleton is a design antipattern. That is to say that within the assumed context of achieving such things as software maintainability, clarity, ease of use, robustness, scalability, etc., the use of singleton implies a direct violation of requirements.

Granted, requirements analysis is not taught, or not taught very well, in education institutions (in my experience as a student and lecturer) and so this is a potential reason for common failure to acknowledge the obvious. It takes approximately 10 minutes of objective thought to arrive at the correct conclusion - the catch is abandoning the preconceptions and flawed reasoning that is portrayed in so many texts of today.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tony,

I'm not sure if i follow you correctly.

I agree that Singleton has its pitfalls, and therefore certain danger of being misused. Easily forgotten thread safety, to name the biggest issue.

But I do not agree with the consideration that Singleton is an anti-pattern. As a matter of fact and from my knowledge, a Singleton is the by far most elegant ways to handle certain situations like for example resource management and connection pools. Oh, and logging usually relies on Singletons, too.

Can you please bring up a few facts? What exactly is a correct implemented Singleton violating?

many greetings,
Jan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic