• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No Singletons in TDD?

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
An advice in the TDD isn't to write code that drive us to employ the Singleton pattern as it imposes itself as a problem in testability.
But Singleton pattern is very important and important (Spring DAOs for example are singletons).
Why not starting developing the singleton class with fake implementation and start refactoring it to the real implementation, you know, this is the TDD after all?
Does this mean that I don't use singleton in my TDD application?
Thanks.
[ August 06, 2008: Message edited by: John Todd ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34853
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
Aren't Spring DAO singletons dependency injected? The singleton itself isn't the problem per se. The problem is how to get a reference/interface that isn't a singleton. For example, when you really need a singleton, putting it behind a factory often helps.

TDD also points out that many things that are singletons don't need to be. Some people have "object creation phobia" and make classes singletons when they could easily be normal objects.

I don't always TDD, but I did try TDD'ng a singleton once. It wound up being something that didn't need to be a singleton and I learned not to presume the implementation. Of course, this doesn't apply to all cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic