GeeCON Prague 2014*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Advantage of using Singleton over a class with static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Advantage of using Singleton over a class with static methods" Watch "Advantage of using Singleton over a class with static methods" New topic
Author

Advantage of using Singleton over a class with static methods

Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
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.


Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375
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


Gabriel
Software Surgeon
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
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 ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Carl Wauters
Greenhorn

Joined: Aug 02, 2007
Posts: 19
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 ]

SCJP 5.0, SCWCD 1.4, SCBCD 5.0, SCJD, SCDJWS 5.0, SCEA/OCMJEA 5
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Advantage of using Singleton over a class with static methods