• 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

Singelton Class

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



By seeing the above code my understanding is like this. If I am wrong please Correct me

a) By providing the static reference, means the instance created can be accessed globally

My question is the purpose of Singelton Class is to create only one instance.

I hope if I am right For this we need to provide our Class Constructor as Private. So that Other Classes Can't create an instance. By looking the above code means

If the constructor is protected then we can create an instance for the class which extends ClassicSingleton Class from the same package

Please make me Clear on the above topic

Thanks in advance
Ram
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example you show I think it is indeed wrong to make the constructor protected instead of private.

By making it protected, the class can be instantiated in a subclass or in any other class in the same package (also classes in the same package that are not subclasses!). The class itself does not have complete control over instantiation this way, so this is not a good way to implement a Singleton.
[ February 26, 2007: Message edited by: Jesper Young ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you got the ideas correct. As described above, you probably need a private constructor to keep things safe.

The null check you showed is a risk in multi-threaded apps. You can synchronize the method to make it safer, or just create the instance when you declare it. With both of these changes ...

This makes the class a factory for its single instance. There are other ways to implement Singleton but this is the most common.

I'll attach the usual warning that Singleton invites a variety of problems into your code. It's global which is usually bad, impossible to extend because of the private constructor, difficult to replace because other classes are tightly bound to the class name, it really assures one instance per class loader instead of one instance per JVM, and more. Any time you're tempted to use one, look for alternatives. Sometimes Singleton is the right answer, but it is overused.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan, please could you explain

assures one instance per class loader instead of one instance per JVM


Thanks
 
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
My understanding is that a single JVM can have more than one class loader. This pattern only guarantees one instance per class loader, so it's still possible to have more than one instance in the JVM of the singleton class. which is exactly what you don't want.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Fred, I guess I need to do some independent research as well.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this help
 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stan

This makes the class a factory for its single instance

If Possible can you explain more on this sentence.

Thanks for your response guys

Ram
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I call it a factory because it creates and provides access to the single instance. I usually add it's a "lower case factory" not one of the formally defined Something Factory patterns. I grew up calling generic creational utilities like that factories well before I saw GoF. Others may choose to call it something else.
 
reply
    Bookmark Topic Watch Topic
  • New Topic