SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What's so evil about Singleton?

    I keep running into people who caution against using Singleton and recommend to avoid it like a plague.

    1. What is so scary about it?
    2. When to use it?
    3. When to avoid it?

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    First, I moved this over to the General Application Design & Theory forum as this topic applies to just about everything.

    As for the question at hand:

    1) The fundamental issue is that, as much as possible, objects should stand on their heads. External dependencies will generally bite you in the end. What if you want to use that class in a different application that does not expose the singletons that class expects? Finally, 99% of the time people use signletons, one could just pass a reference to the object depending on that singleton to make things work well.

    Singletons also present some other problems in web applications, such as race conditions and other threading issues.

    2/3) Almost never for the underlying objects. I think you can get away with having application-level singletons to glue things to external resources so long as your objects are being supplied with a reference as opposed to going and grabbing things on their own.

    Discuss.

  3. #3
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kyberfabrikken explained it rather nicely:

    http://www.sitepoint.com/blogs/2008/...-dependencies/

    (oh, and also read the comments)

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Singletons frequently are used to provide a global access point for some service.
    True, they do this, but at what cost? They provide a well-known point of access to some service in your application so that you don't have to pass around a reference to that service. How is that different from a global variable? (remember, globals are bad, right???) What ends up happening is that the dependencies in your design are hidden inside the code, and not visible by examining the interfaces of your classes and methods. You have to inspect the code to understand exactly what other objects your class uses. This is less clear than it could be. The urge to create something as a global to avoid passing it around is a smell in your design; it is not a feature of globals/singletons. If you examine your design more closely, you can almost always come up with a design that it is better and does not have to pass around tramp data to every object and method.

    2) Singletons allow you to limit creation of your objects.
    This is true, but now you are mixing two different responsibilities into the same class, which is a violation of the Single Responsibility Principle. A class should not care whether or not it is a singleton. It should be concerned with its business responsibilities only. If you want to limit the ability to instantiate some class, create a factory or builder object that encapsulates creation, and in there, limit creation as you wish. Now the responsibilities of creation are partitioned away from the responsibilities of the business entity.

    3) Singletons promote tight coupling between classes
    One of the underlying properties that makes code testable is that it is loosely coupled to its surroundings. This property allows you to substitute alternate implementations for collaborators during testing to achieve specific testing goals (think mock objects). Singletons tightly couple you to the exact type of the singleton object, removing the opportunity to use polymorphism to substitute an alternative. A better alternative, as discussed in the first point above, is to alter your design to allow you to pass references to objects to your classes and methods, which will reduce the coupling issues described above.

    4) Singletons carry state with them that last as long as the program lasts
    Persistent state is the enemy of unit testing. One of the things that makes unit testing effective is that each test has to be independent of all the others. If this is not true, then the order in which the tests run affects the outcome of the tests. This can lead to cases where tests fail when they shouldn't, and even worse, it can lead to tests that pass just because of the order in which they were run. This can hide bugs and is evil. Avoiding static variables is a good way to prevent state from being carried from test to test. Singletons, by their very nature, depend on an instance that is held in a static variable. This is an invitation for test-dependence. Avoid this by passing references to objects to your classes and methods.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webaddictz View Post
    Kyberfabrikken explained it rather nicely:

    http://www.sitepoint.com/blogs/2008/...-dependencies/

    (oh, and also read the comments)
    Actually, I tried to avoid that question. I explained how to write code, which doesn't make use of a singleton, but I only briefly mentioned the motivation for doing so. This is because the answer to that question is actually quite complicated. Which is also why I don't have the time to elaborate further on it right now (Though I think blueyon sums it up pretty good).

  6. #6
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webaddictz View Post
    Kyberfabrikken explained it rather nicely:

    http://www.sitepoint.com/blogs/2008/...-dependencies/

    (oh, and also read the comments)
    I just read the article and followed the ensuing comments. It was a very interesting affair. I liked how Tony Marston argued for what he believed was right. The comments were at times heated but made an interesting read on the whole.
    ------------------

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rageh View Post
    I just read the article and followed the ensuing comments. It was a very interesting affair. I liked how Tony Marston argued for what he believed was right. The comments were at times heated but made an interesting read on the whole.
    I closed the comments, because I felt it was getting out of hand. I think that Tony, somewhere deep down, has a point to make. I also think it's unfortunate, that his rhetoric makes it very hard to take any real argument with him.

  8. #8
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    I closed the comments, because I felt it was getting out of hand. I think that Tony, somewhere deep down, has a point to make. I also think it's unfortunate, that his rhetoric makes it very hard to take any real argument with him.
    The discussion in the end was getting inflammatory. But Tony was still making a good point that nobody wanted to accept. For example the arguments in the end regarding Singletons and Polymorphisms. By then though the discussion was getting far too off-topic.

    Anyway, it was a good discussion although rather heated at times.
    ------------------

  9. #9
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reply got a bit long-winded.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •