aspose file tools*
The moose likes Java in General and the fly likes what is the benefits of singleton ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is the benefits of singleton ?" Watch "what is the benefits of singleton ?" New topic
Author

what is the benefits of singleton ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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 ?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Jul 20, 2011
Posts: 49

If i use only static variables and methods in a class, wouldnt that be very similar to a singleton "object"?


Knowledge Reigns Supreme
Cory Hartford
Ranch Hand

Joined: May 16, 2011
Posts: 82

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.


‎"The greatest of all weaknesses is the fear of appearing weak." - JB Bossuet
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 461
    
    4
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Jul 23, 2010
Posts: 136
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

Joined: Mar 17, 2011
Posts: 7801
    
  21

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the benefits of singleton ?