This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Advantage of using Singleton over a class with static methods

 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gabriel Claramunt
Ranch Hand
Posts: 375
Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reiterating with other words: Using the Singleton pattern, you can still make use of polymorphism, the most important feature of an OO language.
 
Carl Wauters
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic