• 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

singleton object

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understand what is singleton object. I looked online and it says that singleton class is a class that can have only one object at a time. What does this mean ? I dont think it means we can only creatte one object of a class. Can anyone pls explain preferably with example.
Please also explain Factory class too.
Thanks
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example of a singleton is an application object. You might have more than one window, but you wouldn't have more than one application object.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Actually I have the same doubt in mind, but I am not convinced by the above post. Also another thing, I've never heard of something called a Factory Class do you mean to say factory method instead or rather a static factory method then this blog
has a good explanation I got recently.
Also I bear a doubt in my mind,
How to write a singleton class.

Thanks in advance.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There had been a discussion on same a few days ago in this same forum.
Please view this thread on topic to have more details.

PS. It is a good practice to check FAQ's of the forum and search the forum using the Search button on top to check if your question had been already answered. Please take time to the same before posting.
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look the following code. It's the canonical implementation of a Singleton. The private constructor avoids other classes to instantiate Singleton objects. The only way to get a Singleton instance is calling the method getInstance(), that will always return the same instance.
Of course, there are some issues regarding distributed systems or concurrence.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.
 
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
in brief... a singleton means you can only create the object one time within your program. say i had a car rental program. one of my classes would be a Car. clearly, i'd want to create many, many instances of cars, assuming i have more than one to rent.

I may also have a scheduler class. this is the thing that says "on this day, this car will be used by this person". if i had two instances of that, they might both fight for the same resource. instead, i design my code so that everybody gets a reference to the same instance. everyone is talking to the SAME scheduler.

a factory class is a class who's job is to create instances of other classes. to corrupt the HFJ example, i may have a PizzaFactory class. you pass its method a parameter, and it returns a PepperoniPizza, a SausagePizza, or a CheesePizza.
 
Rodrigo Lopes
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.



Sorry, my mistake

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.



That, plus the fact that the "canonical" example of a Singleton usually employs lazy instantiation to create the single instance, but that's splitting hairs really
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by R Lopes:
Sorry, my mistake


You're not the only one.

Log4J has the exact same problem with their NullAppender. There is a method called getInstance() with the following comment:

Whenever you can, use this method to retreive an instance instead of instantiating a new one with new.


Slight problem: that method is NOT static
So, in order to call a method to prevent creating a new object, you have to create an object first. Nice try guys
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you create a class with private constructor, how do you instantiate that class, if its not an inner class ?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From within the class itself, as in R Lopes' example.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jignesh soni:
If you create a class with private constructor, how do you instantiate that class, if its not an inner class ?



As Rob Prime said, you instantiate the object inside the class, at this point

So, outside your class, you use getInstance method to get a reference to this object, i.e,

So, just to remeber,
"Private constructors prevent a class from being explicitly instantiated by callers"
[ April 08, 2008: Message edited by: neilson ramalho ]
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic