The Artima Developer Community
Sponsored Link

Java Buzz Forum
Factory vs Singleton

0 replies on 1 page.

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 0 replies on 1 page
Norman Richards

Posts: 396
Nickname: orb
Registered: Jun, 2003

Norman Richards is co-author of XDoclet in Action
Factory vs Singleton Posted: Aug 20, 2007 10:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Norman Richards.
Original Post: Factory vs Singleton
Feed Title: Orb [norman richards]
Feed URL: http://members.capmac.org/~orb/blog.cgi/tech/java?flav=rss
Feed Description: Monkey number 312,978,199
Latest Java Buzz Posts
Latest Java Buzz Posts by Norman Richards
Latest Posts From Orb [norman richards]

Advertisement

It's an interesting question. When I first wrote against singletons, back in 2003, design pattern mania was still in full swing, and few people seemed ready to accept that the holy book of design patterns might be teaching techniques which are really more anti-patterns than patterns. But, in the last year or so, popular opinion has finally swung against the singleton to the point that we can start questioning other object management techniques.

The first thing I'll say is that there is absolutely nothing wrong with using only a single instance of an object in an application. That's fine. The problem with the singleton is in the (attempted) enforcement of the single instance. Thou shalt only use one instance. The problem comes when the object designer thinks he knows what is best. It's the same reason that declaring a class or method final is (almost) always wrong. When you artificially restrict how an object can be used,even if you are lucky enough not to feel the pain of the bad design, you will always be going down the wrong path.

Getting back to a factory, when is a factory bad? A factory is bad when it is the ONLY way to get an create an instance of an object in the system. I guess a thorough answer should be a bit more nuanced than that, but I think I like that as a general rule of thumb. A good factory should be flexible and customizable, and the best factory is the one you don't see. Ideally, objects won't need to be looked up, but can be provided by the runtime when needed. When a configuration for one use case can be easily assembled differently than a configuration for another, with no code-level change to the configured objects, then you know you've got it right. If that's not how your system works, then you should consider heading back to the white board.

In summary: singletons are bad, but there's nothing wrong with only using a single instances. Factories are better than singletons, but when using them you should take care that you aren't enforcing singleton-like rigidity in your system. For maximum usability, consider a managed environment with flexible IoC-like behavior.

Read: Factory vs Singleton

Topic: Regression Therapy - Contentful Testing Previous Topic   Next Topic Topic: CruiseControl.NET Demo

Sponsored Links



Google
  Web Artima.com   

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