The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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).
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.
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?