Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton object

 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • 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
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 119
  • Mark post as helpful
  • send pies
  • 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

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 150
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From within the class itself, as in R Lopes' example.
 
neilson ramalho
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic