The DAO pattern forms part of the core J2EE pattern set.
The DAOFactory is supposed to create a new Dao() rather than the Dao implementing the Singleton pattern.
This feels right to me. However, it does result in lots of objects being created during the running of the J2EE application, probably with no instance variables. I doubt that this takes up many resources and that they are garbage collected easily after the business method has finished with the DAO.
However, you could save any instantiation by using the Singleton pattern for the Dao.
Are there good reasons why not to use the singleton pattern in this way?
One reason that occurs to me is that it would cause confusion when subclassing a Dao. I'd be thinking to myself, "What does the author of the superclass want me to do? Should I make my subclass as singleton too...".
Personally, I think the 'confusion' issue would be a serious problem on its own. But while it might add to the instantiation and objects floating around, I think that you're right in saying it's worth the simplicity it lends in trying to determine what's intended.
Just my $0.02.
Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
Originally posted by Dave Boden: I doubt that this takes up many resources and that they are garbage collected easily after the business method has finished with the DAO.
With modern JVMs, short-lived objects really are very cheap. Modern garbage collectors are optimized for those kinds of objects, so they shouldn't be a problem at all.
Introducing a Singleton simply would in almost any case increase complexity and reduce flexibility without enough advantages to make it worth the costs.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus