Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton creattion!

 
Mateen Dar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, how can i create a class to be a singleton. a singleton is a class that can only be used a sinlge time during the execution of a program. am i right. thanks.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite, an instance of a singleton class is unique. There is only one of them in your program. The problems are 1) how to make sure the instance is unique, and 2) how do the various objects in your program know where to find the single unique instance of the singleton class.

You can use the singleton instance as many time as you like, even from multiple threads running simultaneously in your program. (This is perhaps important in solving problem 1.)

So how would you start making your very own singleton?
 
Mateen Dar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt understand .. how do u
 
Grant Gainey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some more things to think about:

If you want something to be a singleton, you have to insure that not just anybody can call "new MySingletonClass()" - that could result in multiple copies of my singleton floating around. How can you "hide" the constructor(s) of your Singleton?

Now that you've hidden the constructor - you need to provide a "gatekeeper" method. That gatekeeper needs to be able to construct the singleton, and then return the same reference each time it's invoked. Try google'ing on "Factory Pattern".

Finally, since you want/must be able to invoke your factory without access to a constructor, the Factory method (and the storage for the singleton) need to be Class-level accessible rathe rthan instance-level. Take a look at what declaring something "static" does for you.

Advanced topics (such as, "Mainting Singleton-ness in the face of multiple ClassLoaders" and "Is Singleton really an Anti-Pattern?") left for future discussion.

Grant

(Who's learning how to answer "the JavaRanch way" by asking more questions or pointing out areas for investigation. Bear with me while I learn...)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an exercise, see if you could hide the whole class instead of hiding the constructor. There are a couple ways with different interpretations of just who we want to hide a class from.

Then the question arises, how do you use a class that is completely hidden? Hint: Have you ever used an Iterator for a List? Did you have to know what class it is or how it was created?
 
Naveen Vooka
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://forums.java.sun.com/thread.jspa?threadID=575682&tstart=0

--------------
Naveen Vooka
www.devsquare.com
DevSquare - Online Application Development
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic