More Singleton wisdom
Analyze the following article from Oracle: https://community.oracle.com/docs/DOC-918906
Improve Singleton examples and their documentation where necessary.
@iluwatar There is an issue #188 where concern regarding lazy loading of Singleton is raised. I was going through the singleton documentation yesterday and found that the disadvantages of approaches are not discussed. We should document that the lazy loading example with synchronized keyword is ok for small applications but actually is too slow for production and should not be preferred.
http://programmers.stackexchange.com/questions/179386/what-are-the-downsides-of-implementing-a-singleton-with-javas-enum covers downsides of Enum based singleton.
Misko Hevery has discussed Singleton vs single instance in this blog post http://misko.hevery.com/2008/08/25/root-cause-of-singletons/ In his blog Misko discusses that there is nothing wrong in having a single instance of something, like having a single application scope object maintained by IoC containers, they don't have private constructors or global instance variable. The singletonness is maintained by container. We can also include this in FAQ "Are Singletons bad?" or some better name. Further references which I found useful:
- http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
- http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/
- http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
I feel much can be said about Singletons so we need to figure out how we are going to address this. Your thoughts?
I haven't read those links yet but by my own experience I know that in some simple scenarios a singleton can be perfectly replaced by a uninstantiable class. I think this should be written as a footnote somewhere if we already have a place for these things.
There are interesting findings in the Oracle's article particularly about double-checked locking. However, I think our implementation ThreadSafeDoubleCheckLocking is correct.
Those are excellent articles @npathai :+1: Thanks for sharing.
@iluwatar Is there an implementation of Singleton with Enum in this repo ? I think Effective Java recommends that we use Enum as Singleton as it is the most bulletproof construction of singleton.
Edit:- There is one...saw it just now.
Yes but there are known downsides for that. EJ is old and much have been learned since then.
An issue is that it will have the following public static methods:
public static Foo[] values()
public static Foo valueOf(String name)
http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
@iluwatar I had linked this question in my previous comment. :+1: though.
Ah, didn't notice that. This thread is getting long...
@iluwatar is this task still open. Can you assign this to me? I am reading about singletons Task is: "Improve Singleton examples and their documentation where necessary." ?
Great, go ahead @sanghisha145
@iluwatar is this task still open. Can you assign this to me? I am reading about singletons Task is: "Improve Singleton examples and their documentation where necessary." ?
Hey @sanghisha145, Any updates?
hey @iluwatar , @ohbus is this issue still open. You can assign this to me. I will try to finish it within 1 month. and I will ask for help if necessary from you all.
After a long period of not receiving any updates from @sanghisha145
I am assigning this issue to @Vikashratn.
Good Luck!!
@Vikashratn are you working on this?
hey @Vikashratn are u still working on this issue
This issue is free for taking again.
public class Element {
public static final Element NONE = new Element();
public static final Element WATER = new Water();
public static final Element EARTH = new Earth();
public static final Element FIRE = new Fire();
public static final Element AIR = new Air();
public void printName() {
System.out.println("Element is: "+getName());
}
public String getName() {
return "None";
}
}
public class Water extends Element {
public String getName() {
return "Water";
}
}
public class Earth extends Element {
public String getName() {
return "Earth";
}
}
public class Fire extends Element {
public String getName() {
return "Fire";
}
}
public class Air extends Element {
public String getName() {
return "Air";
}
}
Why isn't something like this included in the singleton as a option? Yes you could create more instances from that class, but this unique instance is technically also a singleton. Which you can identify.
It provides a aspect that enums can not provide. If you have singleton instances that require expand ability, adding new elements (singletons) later, is not possible with enums without having resort to something like reflection/asm.
ofcourse you could just move functions from the enum into a interface too and let the enum extend them and have multiple enums.
Also this singleton pattern shown here is also really powerful with the factory pattern. Without the enum overhead that exists.
Hi, I'm new to this project. How would i go about contributing to it?Thanks.
@OtherHorizon see https://github.com/iluwatar/java-design-patterns/wiki/01.-How-to-contribute
Is this issue still free so I can be assigned? recently I was reading about the design pattern and I want to contribute. @ohbus @iluwatar
Ok @Omar-ahmed314, this is assigned for you
Thank you for considering me, @iluwatar , may I know the deadline for this issue? away from the deadline of hacktoberfest
I'd be happy for you if you completed it during the hacktober month 😀
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
We should document Bill Pugh's singleton solution
@iluwatar, I would like to contribute, can I be assigned to this issue?
That's excellent, please go ahead @jasokolowska