Originally posted by Hung Tang:
okay.. I'm getting the impression that perhaps people are overusing Singletons, could this be the reason why people recommend "dont use it"
so we know a Singleton can be useful in various places. Would you want to create more than one of the same data source connection instances to your database in your application?
Or would you have disagreed with Sun's design decision with Boolean.valueOf().
This is why something explaing why singletons are generally a bad idea in the faq would be good, so people can be pointed in that direction.
Why not just create one instance ?
Boolean is not a singleton (it has a public constructor). Boolean.valueOf() is a factory method.
But why are we just targetting singleton here?
I don't know how using Singletons can reduce reusability of class
Effective Java by Josh Bloch explains it well
Well what if you decide that you want to use more than one instance of the class ?
I've read it, maybe you should have another look.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Hung Tang:
But why are we just targetting singleton here? All patterns are subject to scrutiny.
Yes, just creating one instance is sufficient. Singleton would be ideal for that.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Singleton does more than just creating one instance. First, it doesn't allow you at all to create a second one (which reduces its usefulness);
second it provides a global access point to the one instance (which increases coupling).
For me, a class is coupled very tightly to anywhere its name is mentioned in the code. I can't swap that class out for another implementation (one for testing, one for better performance, one for a different customer, whatever) without changing every occurrence of that name.
That same class might be a lot less coupled if it implements an interface or extends a well-known base class, and the code always just refers to that interface or base class and doesn't care which class implements it. In that case, changing the implementation is very easy and localized, and needs no recompilation of code which uses it.
It's even possible to create a new implementation long after the code which uses it is compiled and deployed, and dynamically load it into a running program. Servers, in particular, use this approach a lot.
The Singleton pattern ties all the code which uses it very closely to a particular name and implementation. Whatever you call them, Singletons and statics are "global", and "globals" are well known for making understanding, maintenance and improvement of software harder and more expensive.
Originally posted by Hung Tang:
You should clarify what you mean by "usefulness"
IMO, one strength of the singleton pattern is that it conveys a clear and concise message: only one instance will ever be allowed to exist.
designing your singleton class with a private constructor, and having a static factory method to return your instance is a design/implementation that does not increase coupling.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Hung Tang:
Case in point: many times we see factories created as singletons. We might have a DAOFactory that is implemented as a Singleton. Now where would be the most appropriate place, and we should try to limit our scope to just one place (for now), to obtain our DAOs? Well, it doesn't matter. The point here is made that we stick with a particular place that really makes use of it...could be in a service facade layer.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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
And that's not a usefull message in my eyes. Why shouldn't I be allowed to create a second instance
And if I really shouldn't be allowed, where does the need for a global access point come from?
The benefit of Singleton comes in when I subclass the Singleton.
Imagine I have a Singleton as defined by T, or as improved by M (I prefer and implement this way).
code:
--------------------------------------------------------------------------------
public class WidgetFactory { //.. Singleton implementation // only one method shown protected WidgetFactory thisInstance; public static void createInstance() { if (thisInstance == null) { thisInstance = new WidgetFactory(); } } public Widget createNewWidget() { return new Widget(); }}public class Widget{ // a Widget class}
--------------------------------------------------------------------------------
I have supplied a library of this class to several customers. And later, I found the time and the money (from the above mentioned customers) to develop extraordinary widgets, and would like to sell them to these customers again. But, since this is a premium enhancement, not all customers want it. Moreover the customers who want it will definitely not want to do compilation stuff to change the
code:
--------------------------------------------------------------------------------
return new Widget();
--------------------------------------------------------------------------------
.
So, I do this -
code:
--------------------------------------------------------------------------------
public class ExtraordinaryWidgetFactory extends WidgetFactory { //.. Singleton implementation, overriding // only createInstance() public static void createInstance() { if (thisInstance == null) { thisInstance = new ExtraordinaryWidgetFactory(); } } public Widget createNewWidget() { return new ExtraordinaryWidget(); }}public class ExtraordinaryWidget extends Widget { // an extraordinary Widget class}
--------------------------------------------------------------------------------
I supply the later code as part of a patch.
You only have to ensure that the createInstance of the child is called before that of the parent. How you call that that way is a another matter, and would need another post.
-GB.
Originally posted by Gopi Balaji:
Interesting discussion..
A point of view in favour of Singleton. [See complete discussion here - ]https://coderanch.com/t/324453/java/java/difference-between-class-static-method]
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Hung Tang:
By not making it a singleton and allowing multiple instances to exist, I will have to trust my users. That's a risk, do I want to take that risk?
I think more along the lines of readability.
It's not difficult to find singletons in APIs, they are really easy to spot.
If what you are encouraging is the info. expert pattern (Larmen) applied in OO design,
then lets move to another discussion: don't use "factories".
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
That link doesn't seem to work![]()
And I don't understand why you'd need Singleton here - it seems to me as if AbstractFactory would totally suffice (and be more flexible).
She'll be back. I'm just gonna wait here. With this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|