The Artima Developer Community
Sponsored Link

Weblogs Forum
Singleton Considered Harmful?

34 replies on 3 pages. Most recent reply: Sep 17, 2007 3:20 PM by Marco Dorantes

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 34 replies on 3 pages [ « | 1 2 3 ]
Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Singleton Considered Harmful? Posted: Sep 2, 2007 11:28 PM
Reply to this message Reply
Advertisement
> But it doesn't seem to be very
> many steps to "Programming considered harmful" (which
> would be a very easy sell to many management teams).

Our managers solved this problem some years ago. Instead of trying to write everything ourselves, we (they) would go out and find existing 'standard' software that already did the sort of stuff we wanted. All we (not they) would have to do would be to adapt (or get the the vendors to adapt) the proprietary software to any special needs that we had. What could be simpler?

Strangely, we still seem to have just as big an IT department as before but with the added joy of having to maintain (and pay for the development of) bespoke versions of 'standard' products and, of course, the benefit of no longer owning our own code ans seeing it incorporated into the 'standard' and sold on to our rivals.

Sasa Milenkovic

Posts: 3
Nickname: littlesal
Registered: Nov, 2004

Re: Singleton Considered Harmful? Posted: Sep 3, 2007 9:47 AM
Reply to this message Reply
As I see it, even the screwdriver is harmful if you misuse it (one can stick it in own eye). The catch is that only skilled one use it properly.
I think that overuse of singleton is present because there is lots of less skilled programmers around, learning to think in patterns :) and the singleton is simpliest of all. All of them, over time, learn to use it properly. The problem is that they wouldn't honestly admit that they were only playing at a time, but now they know another of two patterns.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Singleton Considered Harmful? Posted: Sep 4, 2007 6:39 AM
Reply to this message Reply
> > The issue that most people have with singletons is that
> > they can create dependencies between classes that are
> not
> > apparent from anything other than by looking at the
> code.
> >
>
> As is true of any kind of indirection, right? I can
> certainly see a case for an "Indirection considered
> (sometimes) harmful" rant. But it doesn't seem to be very
> many steps to "Programming considered harmful" (which
> would be a very easy sell to many management teams).

I can't really address your question well because my statement was an attempt to reformulate an argument made by someone else in my own terms. I'm not sure I agree with it or not.

But keeping that in mind, I would say not all is equivalent in this regard. If you pass all dependencies in through methods or constructors (I am biased towards constructors) these dependencies become part of the API and are self-documenting at least to some degree.

If the code goes "around the back" to get it's dependencies, it's hidden from the outside.

From my personal experience, I can see how this might be helpful for understanding complex systems, it can also be frustrating for libraries. I've spent many hours trying to find out how to create the special objects that a class I need to use requires without really caring or needing to understand what these dependencies were for.

Personally I think the biggest problem with the Singleton pattern is not implementing it in a flexible way. When you do it the way the GoF describe it you essentially move code into configuration files, much like what Spring gives you. Java, by providing (awkward) reflection capabilities, allows for very flexible Singleton implementations.

Perhaps my feeling is that if you want to use Singleton, either go "all the way" or be prepared to. I often add a parameter to the Singleton getInstance() and I never call it "BlahSingleton" because I don't want to lock myself into that design.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Singleton Considered Harmful? Posted: Sep 4, 2007 12:12 PM
Reply to this message Reply
I think the big problem with singletons is when the class knows that it is a singleton. IMO, you should avoid

Foo.getInstance()

instead, using DIP or some other technique, that should decide if it a class is a singleton. Ultimately, there may be some "master singleton" like an ApplicationContext, so this isn't perfect, but at least it's only one class that knows it is a Singleton.

Nearly every time I have seen a decision like "Oh, never mind, we will only ever have one Foo at a time, we can use Foo.getInstance()" that decision has been wrong.

Even Spring has an issue that, at least how we have generally implemented it, it assumes there is one ApplicationContext. When sometimes you need an alternate context, for example, if you don't want to talk to the normal database URL. This would be a case for James' idea that your "master" getInstance() method should take an argument

Marco Dorantes

Posts: 11
Nickname: marcodoran
Registered: Feb, 2003

Re: Singleton Considered Harmful? Posted: Sep 17, 2007 3:20 PM
Reply to this message Reply
Why on earth people follow logic like the following?
Design patterns -like Singleton- are good.
My program (ab)use of design patterns.
Then, my program is good.

Flat View: This topic has 34 replies on 3 pages [ « | 1  2  3 ]
Topic: Singleton Considered Harmful? Previous Topic   Next Topic Topic: Quoted in CNN's Business 2.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use