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
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 ]
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abdulla Mamuwala
Ranch Hand
Joined: Jan 09, 2004
Posts: 225
posted
0
Stan, please could you explain
assures one instance per class loader instead of one instance per JVM
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.
Never ascribe to malice that which can be adequately explained by stupidity.
Abdulla Mamuwala
Ranch Hand
Joined: Jan 09, 2004
Posts: 225
posted
0
thanks Fred, I guess I need to do some independent research as well.
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
Joined: Jan 29, 2003
Posts: 8791
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.