logo
  • Jobs
  • About Me
  • Contact
  • Home
« VS.NET 2005 May CTP now on MSDN
Refactoring to Patterns »

Singletons are not evil

Posted May 27th, 2004 by Matt Berther

Scott Densmore has an interesting post regarding singletons and why he thinks they are evil. I’d like to go through his points one by one and add my thoughts:

Singletons frequently are used to provide a global access point for some service.

Unfortunately, I tend to agree with this. However, this does not make the Singleton pattern evil. Using a singleton in this way could easily be compared to using global variables. Using a singleton for this is no different than having a class with a bunch of static members on it.

Singletons should be used to control creation, not access. A singleton should *not* be used as a global object just because you can. I find it hard to dismiss a pattern simply because some people dont use it how it was intended to be used.

Singletons allow you to limit creation of your objects.

Scott states that this is true (as it is), but thinks that this is a violation of the Single Responsibility Principle, since a class should only be concerned with its business responsibilities.

Most of the issues around singletons revolve around the terminology. After all, isnt the Singleton property/method *really* just a Factory Method.

I agree that you could use a factory or builder object to do this for you, however, this method doesnt allow you to create one and only one instance.

Singletons promote tight coupling between classes.

This one I have to agree with. The way around this is to use a factory method, however, then you dont get the private constructor that the singleton really should have.

Singletons carry state with them that last as long as the program lasts.

As they should. The idea behind a singleton is that you need one and only one instance of an object.

As with any design pattern, the singleton is good when it is used wisely. Unfortunately, what happens a lot of the time is that a pattern is thrown into something without looking at the other options. Before implementing a singleton is not a bad idea to ask yourself, would it be wrong to have another instance of this class in the system?

Let me leave you with this to chew on… If singletons are evil and should be avoided at all costs, how would you suggest to handle things in the framework such as HttpContext? I personally find HttpContext.Current to be an absolute godsend. Without it, I would probably end up having to corrupt my class interfaces with properties or method parameters that will allow me to access HttpContext.

This entry was posted on Thursday, May 27th, 2004 at 12:37 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Gishu
November 22nd, 2005

I’m quite confused with Scott’s and this post.
I think the primary motive for Singleton pattern is to limit creation of the instance. I think point #2 is weak.
Do I really care if it violates the Single responsibility rule? I’d rather allow this than write another factory class just for a single method (at the cost of being labelled an immature designer. I’ve been called a lot of other things). + I too don’t get how I would get the private ctor bit done with Scott’s/Brian’s approach.
If I keep the ctor public - it ain’t unique
if I keep the ctor private - the factory can’t create it
maybe use something like internal in .NET, but then any other class in that assembly can do a new which violates the req. This puts more emphasis on ‘Future coders playing by the rules’

Brian
April 19th, 2008

I have been reading everyone’s input on pattern design and I am soaking it all in. Can someone point me to a good resource that will define these terms (singleton, factory, facade, proxies, etc.) and help me determine the best approach for my project’s design?

Mark
May 26th, 2008

It seems to me that people want the Singleton pattern to be or work in a way that is not intended to. This spawns the idea that people use it excessively and in contexts that it should not be used in. Like I tell people, its a tool that has only one purpose. Once you cross the line of that purpose, you are using the pattern in a way it was not intended. For example, if you need an order in which resources are created, don’t use a pattern like this. If you want to have many instances of a singleton, don’t use this pattern. etc.

Once thing that you did not mention here is that This pattern is not thread safe, and this is a fact. But it all depends on what the singleton is used for. If it does not have a state, then there is no state to manage between threads, making that class essentially thread safe. Singletons work best in non-state classes.

Another problem I have run into in my programming career is that not only are they not safe, they are also horrible when compiled into DLLs. A DLL has its own memory space and will create a singleton in its space ONLY. Therefore, any program that uses that DLL and tries to access the singleton will soon find out that the singleton creates another instance of itself in a different section of memory. This is how DLLs were designed.

Those are the only two scenarios where singletons fail. Everything else is a property of this pattern and most likely is the fault of the programmer not using it as intended.

Code
August 15th, 2008

I found this post while googling “httpcontext.current evil” because I think it is. I was looking for tools to hook into my build process that would raise warnings when we use it. I’ll give a few reasons.

1. HttpContext.Current must have thread affinity because multiple IHttpHandlers may be executing concurrently on different threads. Therefore, HttpContext.Current is probably implemented using either a [ThreadStatic] attribute or some other mechanism to limit its scope to the executing thread. There is a considerable performance cost to using these constructs.

2. HttpContext encourages poor separation of concern. In a layered architecture, a Page might call a method on a service class, which would delegate to a data access object, then do some business logic. None of these layers (except the Page) need to know they are executing in a web context. However, it’s all too easy to write HttpContext.Current from anywhere in your code, in any layer, to check for a query parameter or write a cookie, or do any other number of things that you shouldn’t be doing at that point.

Furthermore, you seem to be confused about the nature of HttpContext. HttpContext.Current will return an instance of HttpContext that will be different on each thread. Therefore, it isn’t a singleton at all. Neither is HttpApplication for that matter (though HttpApplicationState is). Head spinning yet?

In the JEE / JSP world, these static/ThreadStatic constructs don’t exist, and I don’t think anyone really misses them.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

Social
  • mattberther on twitter
Syndication
Archives
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2008