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 | » ]
disney

Posts: 35
Nickname: juggler
Registered: Jan, 2003

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 3:55 AM
Reply to this message Reply
Advertisement
The example I remember is of a computer simulation of the Earth. It was suggested that the Sun would be an ideal candidate for a Singleton. There can and should be only one Sun (instance) within the model, and any class that needs access to it ought to be able to get it. This makes sense to me.

The Singleton concept was developed before distributed computing was common. If you can't guarantee a single instance in your operating environment, the concept of Singleton becomes more hazardous and less useful, IMO.

The Monostate class is my usual choice of Singleton implementation. It's very simple, and can be handy. I do not generally write distributed applications, but embedded ones.

David Linsin

Posts: 9
Nickname: dlinsin
Registered: Aug, 2007

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 5:23 AM
Reply to this message Reply
Great example Steve, thank you!

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 6:09 AM
Reply to this message Reply
> Obviously a singleton that is initialized and then remains
> invariant during it's life-cycle of use is fine as there
> is then no thread blocking contention for accessing it.
> (It's read-only so no thread need wait on a lock in order
> to access it.)
>
> The serious complaints about singletons have centered
> around the drawback of thread contention.

I don't think this is the case. For example the suggestions above about using DI are equivalent in terms of thread contention issues. I consider this to be a serious complaint. Much more serious than most of the thread contention issues I've seen argued.

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.

I'm really starting to think that maybe what is wrong about Singletons is how you get access to them. Static methods in Java are, for most intents and purposes, global methods.

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 6:51 AM
Reply to this message Reply
> Singletons aren't really about uniqueness; they're about
> enforcing a one-to-one mapping between a classloader and
> an instance, or in the simple case where there's only one
> classloader, it's one-to-one between the instance and the
> JVM. The situations in which you need that particular
> one-to-one-mapping are a lot rarer than people think.
>

This paragraph shows some confusion between a concept (singleton) and its implementation. Classloaders and JVM are valid only in the Java world. Singletons may be used (correctly or incorrectly) in applications using any type of technology: Java, C++, C#, Ruby, and so on. In every case except Java, the discussion of classloaders becomes meaningless. (Is there a message there for the Java designers? Why do classloaders even exist? Why should I, as a developer, even know of the existence of classloaders? I am not satisfied with the common explanations provided.)

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 7:21 AM
Reply to this message Reply
> (Is there a message there for the
> Java designers? Why do classloaders even exist? Why should
> I, as a developer, even know of the existence of
> classloaders? I am not satisfied with the common
> explanations provided.)

I can't address it from a theoretical perspective but from a pragmatic one, it's allowed me, in the past, to have two applications running in the same app server using different versions of the same libraries.

You might want to take a look at http://www.projectdarkstar.com/. I think they have some discussion about why they chose Java for this project even though C/C++ is the more natural choice for most game developers on their FAQ. I think the reason has something to do with classloaders and how it gives them something that they could not get with C/C++ (at least not easily.)

Ian Robertson

Posts: 68
Nickname: ianr
Registered: Apr, 2007

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 8:12 AM
Reply to this message Reply
The suggestion made by many to use Dependency Injection instead of singletons is interesting; don't most DI frameworks use singletons behind the scene? JNDI is another example of a singleton in disguise. It seems that the real goal is not to eliminate singletons, but rather contain their use to a small, predictable set of usage points.

Michael Barker

Posts: 5
Nickname: mikeb01
Registered: Oct, 2005

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 8:21 AM
Reply to this message Reply
> This paragraph shows some confusion between a concept
> pt (singleton) and its implementation. Classloaders and
> JVM are valid only in the Java world. Singletons may be
> used (correctly or incorrectly) in applications using any
> type of technology: Java, C++, C#, Ruby, and so on. In
> every case except Java, the discussion of classloaders
> becomes meaningless. (Is there a message there for the
> Java designers? Why do classloaders even exist? Why should
> I, as a developer, even know of the existence of
> classloaders? I am not satisfied with the common
> explanations provided.)

You are correct that classloaders only apply to Java, but other technologies have their own gotchas in this area. In .NET if your application spans multiple AppDomains you will have the same problem (I think, some .NET person might tell me I am wrong). If you are using C++ and an application framework like Tuxedo, business components can be distributed across a number of separate processes, which will also give you multiple instances of a Singleton. Therefore the problem of understanding the impact that deployment has on your design does not go away if you move away from Java.

Mike.

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Singleton example Posted: Aug 28, 2007 9:04 AM
Reply to this message Reply
The heirarchy of Logger's is a singleton (only one root logger). I think this is correct as we want to be able to attach a handler to the root and get all the log events. As this particular singleton is in the system classloader it will be a genuine unique instance.

As for the example of the Sun being unique, what if you wanted to test hypotheses involving changes in the Sun's characteristics. You would be compelled to run these simulations sequentially instead of in parallel.

acton wang

Posts: 6
Nickname: actonwang
Registered: Mar, 2006

Re: Singleton example Posted: Aug 28, 2007 6:13 PM
Reply to this message Reply
Spring container or even each app server is a "Singleton". Too many singleton which buries true relationships between objects are harmful.

Also singleton will make naive developers puzzled when multiple classloaders invovled say in the container architecture.

I used singleton a lot for configuration before spring.

For logging, Hiearchy might be not be singleton and logManager and RepositorySelector are true singletons. That is why sometimes log4j makes people puzzled :)

mike bayer

Posts: 22
Nickname: zzzeek
Registered: Jan, 2005

Re: Singleton Considered Harmful? Posted: Aug 28, 2007 7:47 PM
Reply to this message Reply
> The suggestion made by many to use Dependency Injection
> instead of singletons is interesting; don't most DI
> frameworks use singletons behind the scene? JNDI is
> another example of a singleton in disguise. It seems that
> the real goal is not to eliminate singletons, but rather
> contain their use to a small, predictable set of usage
> points.

+1

when you use a framework like spring, the beans you configure are singletons by default, and very often thats what you actually want them to be. not for any reason of "uniqueness", but just because they are often stateless "worker beans" like DAOs which would be wasteful to instantiate on each usage.

that said, a large app, even if it uses spring, is probably going to have one MyBigRegistryThing.get() in there someplace. But if you think you need MySecondRegistry.getThatToo(), then you've gone overboard.

John Cowan

Posts: 36
Nickname: johnwcowan
Registered: Jul, 2006

Re: Singleton Considered Harmful? Posted: Aug 30, 2007 5:11 AM
Reply to this message Reply
> You say singleton Registry, I say put it in the
> e Application object or create an Application class if one
> doesn't exist. If you consistently apply this routine all
> the way out to main() (where you'd create the single
> needed Application object in this case) you don't ever
> need singletons.

Why, you contradict yourself in a single breath. "Single needed Application object"? What is that if not a singleton?

All programs start with a single thread of control executing in a single object with a singular set of environmental information provided to it.

Steve Love

Posts: 20
Nickname: essennell
Registered: Sep, 2004

Re: Singleton Considered Harmful? Posted: Aug 31, 2007 1:27 AM
Reply to this message Reply
Having only a single instance of an object is subtly different to an object which can have only a single instance.

Florin Jurcovici

Posts: 66
Nickname: a0flj0
Registered: Feb, 2005

Re: Singleton Considered Harmful? Posted: Aug 31, 2007 5:21 AM
Reply to this message Reply
> Agreed. A Singleton typically is a global variable,
> though.

Not necessarily. It can also be a global function. However, IMO it sometimes is at least useful to have such singletons.

> I'm not a big fan of DI using external XML. Instead, I
> prefer to pass in the required collaborators as
> constructor parameters to each class, using factory
> functions where necessary. This puts the key information
> about which classes are actually used in the application
> in the code itself --- at the top level, the "main"
> function.

I wonder how you change your app's structure without recompiling it and doing a completely new redeploy. I _love_ XML in this regard, although it sometimes can get quite annoying.

Florin Jurcovici

Posts: 66
Nickname: a0flj0
Registered: Feb, 2005

Re: Singleton Considered Harmful? Posted: Aug 31, 2007 5:31 AM
Reply to this message Reply
Class loaders are not a language quirk, you might consider them a paltform quirk at best. However, any other system has its own similar quirks - Javascript has prototypes, for instance.

Nobody forces you to know about class loaders, as long as your code doesn't get that deep into the platform specific functionality that you _need_ to know about them.

Jaime Metcher

Posts: 3
Nickname: jmetcher
Registered: Sep, 2007

Re: Singleton Considered Harmful? Posted: Sep 2, 2007 3:27 PM
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'm really starting to think that maybe what is wrong
> about Singletons is how you get access to them. Static
> methods in Java are, for most intents and purposes, global
> methods.

Indeed. Something that never gets mentioned is that the static part of every class on the classpath is in effect a singleton. The namespace of packages is a global namespace (and a very heavily populated one in many apps), but for some reason this doesn't seem to bother the "no global variables" camp.

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