Re: Can use of singletons denote poor project design?



Daniel Parker wrote:

I don't really see how there are any special issues for writing unit
tests for singletons.  For example, in Java, DateFormat.getInstance()
returns a default date format, which is a singleton object, there is
one instance of a default date format.  Are you suggesting that you
would have difficulty testing whether this object satisfied its
specified properties?  Why?

Could you outline a test that changes the format, calls a method that relies
on formatted dates, and test its output is correct?

Could we then make the test just a tiiiiny bit simpler by taking out the
singleton and passing the date format directly?

To make the test case harder, suppose in System Q the CurrentTime is a
singleton (because, gee, time is a universal constant, so there's only one
of them). Now write a test that depends on a specific date to pass its
assertions.

I didn't say my method can detect any singleton that sucks. (I think I
didn't.) I implied my method can illustrate why some of them suck. There
might be other reasons that others of them suck too.

Those of us who recover legacy code can tell many stories of Singleton
abuse. If there was ever a candidate for time-travel to remove a chapter
from the Design Patterns book, Singleton would be it...

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
.



Relevant Pages

  • Re: Can use of singletons denote poor project design?
    ... Changes the format to something other than what is required for the ... To see if the target function respects the date format! ... singleton (because, gee, time is a universal constant, so there's only ... Who said anything about the DateFormat being a singleton? ...
    (comp.object)
  • Re: Can use of singletons denote poor project design?
    ... relies on formatted dates, and test its output is correct? ... Changes the format to something other than what is required for the ... To see if the target function respects the date format! ... singleton (because, gee, time is a universal constant, so there's only ...
    (comp.object)
  • Re: Can use of singletons denote poor project design?
    ... returns a default date format, which is a singleton object, there is ... one instance of a default date format. ... Presumably the default format depends on the default locale, ...
    (comp.object)
  • Re: When is it appropriate to use static methods?
    ... I am using the following format to create my singleton: ... > namespace Company.TWS.BusinessObject ... The normal singleton pattern would have the _twsDS and twsDS ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: little more help on remoting?
    ... seems to be able to supply either a singleton or mutliple objects depending ... > from one client, so thread-safety isn't as much as concern. ... I can access the same singleton object from my ... >> public static void Main ...
    (microsoft.public.dotnet.languages.csharp)