This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Can some please help highlighting the advantage of using a Singleton class over a class with just few public static methods.
Scenario: 1. I have a singleton class. So the constructor is private, I expose a public static getInstance() which is synchronized and overide the clone method. I have a method getConnection(), which will return me a connection object. Logic is to check if the connection is not null, create and return the connection or return the already available connection.
2. I can do all the above using a plain class with a public static getConnection() method and having a private constructor. Of course this may look like a utility class.
But doesn't approach 2 serve the purpose.
Question is how does a singleton differ from a class with static methods.
I don't have the GoF book at hand, but from Wikipedia: Singleton pattern "although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed" If you use a singleton instead of a class instance, you can use lazy initialization. Also you can have singletons with a limited number of instances (instead of only one), not possible with a class
Originally posted by Ben Souther: If you have a private constructor, how will you instanciate and initialize your class?
Assuming that you're talking about a connection pool, where are you storing the references to your connections?
In case of Singleton, the getInstance() will create the instance of the class and return the object.
In case of the second scenario, I don't want anyone to create an instance of my class. I have exposed a public static method.
The references to the connection object (this is not a pool, but a single object. I used "connection" just as a hypothetical example) will be a private variable in case of singleton class, for which I will have a getter method exposed.
In case of second scenario, I will have a private static object to store connection, and expose a public static getConnection method.
Well, basically what I am trying to understand is why use Singleton, when I may be able to achieve similar results with a class with static methods.
Thanks Gabriel... Well yes, the points you mentiioned seems to make sense... [ July 12, 2007: Message edited by: Sunil Vasudevan ]
Singletons have a lot of issues, but as GoF points out, you can subclass them and plug in different implementations as needed. The typical Java implementation with a private constructor makes that difficult, but the private constructor is by no means required by the pattern.
BTW: "utility" classes with all static methods have a lot of issues, too, including the one that you can't plug in a subclass without changing all references to the class.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Reiterating with other words: Using the Singleton pattern, you can still make use of polymorphism, the most important feature of an OO language.
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
I was wondering the same. I know that the singleton pattern has some advantages, but suppose that you've got an application where both ways can be used without any problem. Wouldn't it be better then to use a class with static methods instead of the singleton pattern? Using a singleton normally means you create 1 object (of the same type as the singleton class) while using a class with static methods doesn't create any object at all. (when I talk about a class with static methods, I mean a class with a private constructor) [ August 08, 2007: Message edited by: Carl Wauters ]
Wouldn't it be better then to use a class with static methods instead of the singleton pattern? Using a singleton normally means you create 1 object (of the same type as the singleton class) while using a class with static methods doesn't create any object at all. (when I talk about a class with static methods, I mean a class with a private constructor)
In the scheme of things, why would you be concerned about creating one object. There's a negligible amount of overhead and if you ever do need polymorphic behavior, you can add it in without changing client code. Java is an object-oriented language, feel free to use objects liberally.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter