|
Posted by Jerry Stuckle on 07/26/07 13:48
Toby A Inkster wrote:
> Jerry Stuckle wrote:
>
>> Yes, it's a philosophical problem, but I don't see anything in OOP
>> theory which rules out singletons.
>
> http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial
>
Hi, Toby,
Yes, I've seen such arguments. But none of them indicate a violation of
OOP theory. Rather they are geared more towards poor programming practices.
And I could tear a lot of their arguments apart - i.e.
"When one class uses a singleton (and I'm talking about a classic
singleton, one that enforces it own singularity thorough a static
getInstance() method), the singleton user and the singleton become
inextricably coupled together. It is no longer possible to test the user
without also testing the singleton."
Well, that's true whether it's a singleton class or not. If you have a
class MyData which depends on a MyDatabase class, for instance, you will
not be able to test MyData without having MyDatabase available. And you
will be testing MyDatabase also, whether it is a singleton or not.
You can pass an object in the constructor. It does have some
advantages, as noted by others. But it also has the disadvantage that
the code which creates your class must know more about your class's
implementation, i.e.
$p = new MyData($instanceOfMyDatabase);
So while arguably you have loosened the coupling between MyData and
MyDatabase, there is tighter coupling between MyData and the creator.
There are a lot of other things which could be argued both ways -
because this (and the attached articles) is an opinion on a
philosophical issue.
Now - don't get me wrong. I'm not saying whether I agree or disagree
with what they say - just that this is a philosophical discussion. It's
not that much different than global variables in structured programming.
They are frowned upon - but there's nothing in structured programming
theory which rules them out. And there's nothing in OO theory which
rules out singletons.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|