Journey is as important as the Destination.<br /> <br />PMP, SCJP, SCJD, SCEA (Part 1), SCMAD, SCDJWS
Journey is as important as the Destination.<br /> <br />PMP, SCJP, SCJD, SCEA (Part 1), SCMAD, SCDJWS
Originally posted by Wilfried LAURENT:
It depends on what you want to achieve.
With the singleton implementation, you can hide the real implementation of your service. The client only knows the contract (API) but does not know about the implementation. This is easily configurable with a bridge or a property file.
You can not do that with a 'static method' implementation.
W.
Journey is as important as the Destination.<br /> <br />PMP, SCJP, SCJD, SCEA (Part 1), SCMAD, SCDJWS
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:
The point of the singleton pattern ist to hide the detail that there is only one instance.
In fact, getInstance() could give you a new instance each call, or alternate between two instances or...
Also, using the singleton pattern you can still use polymorphism, that is getInstance() could give you an instance of an other (sub-)class (depending on some configuration, for example).
Therefore, an all-static class is not a substitute for a singleton![/B]
Alan Shalloway.<BR>Look for Jim Trott and my book: <A HREF="http://www.amazon.com/exec/obidos/ASIN/0201715945/ref=ase_electricporkchop/103-0514572-3811868" TARGET=_blank rel="nofollow">Design Patterns Explained</A><BR>Visit our site <A HREF="http://www.netobjectives.com" TARGET=_blank rel="nofollow">Net Objectives</A>.<BR>Visit our <A HREF="http://www.netobjectives.com/dpexplained/index.html" TARGET=_blank rel="nofollow">Design Patterns Explained Community of Practice</A><BR>Check out our <A HREF="http://www.netobjectives.com/xml/xml_cdrom_info.htm" TARGET=_blank rel="nofollow">CDROM based audio training in XML</A>
Static = no instance at all which is even better when you think about it.
---------------
In fact, getInstance() could give you a new instance each call, or alternate between two instances or...
---------------
I would not call it a singleton anymore. this is an instance pool. The subject at hand is singleton.
My bottom line is that all static methods is a good substitute for singletons. less code, easier to understand.
Look at the JDK. How many singleton do you see compared to all static classes?
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 Alan Shalloway:
Actually, the essence of a singleton is controlling the construction of the object. protected will work just fine for that so inheritance will work.
To enforce the nature of a singleton class, you must code the class in a way that prevents other classes from directly creating instances of the class. The way to accomplish this is to declare all of the class' constructors private.
There is one other case I can think of, using a singleton can potentially control the order of when the object is instantiated better. With static methods, the static members will be instantiated the first time the class is referred to. This may be a reference to some const in the class. In the singleton pattern, it won't be instantiated until the getinstance is called.
And often harder to maintain, because of less encapsulation.
Originally posted by Ilja Preuss:
That seems to be the wrong question to me. The more interesting question is: When do you see all static classes, when singletons?
"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Journey is as important as the Destination.<br /> <br />PMP, SCJP, SCJD, SCEA (Part 1), SCMAD, SCDJWS
Originally posted by Matts Smith:
----
And often harder to maintain, because of less encapsulation.
----
I don't get it. Why would it be less encapsulated if it's static?
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
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |