• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the benefits of singleton ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Ranch Hand
Posts: 85
Mac Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 491
5
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic