• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the benefits of singleton ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does singleton provide us means what benefits it gives
i know if i want to make only one instance of the class than that with singleton we can do that
but what else we can do with singleton means its benefits ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that you can only make one is the whole POINT (and curse) of a singleton. As far as I know, the ONLY reason you would ever make a singleton is because you want to only allow one.

Why do you think there are/should be other benefits?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is not that very clear Navid.

I guess fred had clearly answered you. If you read your question again, you can find the answer in itself.

The very main benefit of using a singleton is to have one AND only instance of a class. This it to avoid too many instances of the same class, wherein the individual instances may NOT make a significant difference in the operations being carried out. This is accomplished by controlling the number of objects/instances being created of that class. A mechanism for achieving this is called Singleton, which is a Design Pattern.

Hope this helps.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a static instance variable such as a counter with a Singleton pattern you can ensure that only one object can modify the counter.
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i use only static variables and methods in a class, wouldnt that be very similar to a singleton "object"?
 
Cory Hartford
Ranch Hand
Posts: 85
Eclipse IDE Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know that there is any benefit from what I have seen. I had a digital display that I was populating and I thought a singleton class might be the answer, but what I realized with help from folks here is that the only reason I would use the singleton at all was because I had designed my program poorly. I didn't want to have to create a new object and manage it with a reference because my program was a mess. Once I cleaned up my program and and made objects less reliant upon one another it was a piece of cake to pass the object reference around where I wanted to use it. It turned out, for me anyway, that it was more of a question of good oo practice than it was about the singleton in particular.
 
H Paul
Ranch Hand
Posts: 471
4
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I read correctly. (Just for a laugh and nothing more or less.)

The very main benefit of using a singleton is to have one AND only instance of a class.


1 President.

This it to avoid too many instances of the same class, wherein the individual instances may NOT make a significant difference in the operations being carried out.


Acts as a Command in Chief. Only the President can declare the war.

This is accomplished by controlling the number of objects/instances being created of that class.


Voting

A mechanism for achieving this is called Singleton, which is a Design Pattern.


American Democracy Pattern.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Avadis wrote:If i use only static variables and methods in a class, wouldnt that be very similar to a singleton "object"?

You shall have instance methods... but access them with the ONLY object created...
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods soln = acceptable logically, but not a good design paradigm. What you want is not reflected properly in the code (even though it works). Hence the signleton
 
Winston Gutkowski
Bartender
Pie
Posts: 10091
56
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i use only static variables and methods in a class, wouldnt that be very similar to a singleton "object"?

No, because
1. You couldn't pass that object to a method.
2. You couldn't decide later to allow more than one object without a lot of refactoring.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic