• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singelton Class

 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan, please could you explain
assures one instance per class loader instead of one instance per JVM

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks Fred, I guess I need to do some independent research as well.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this help
 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic