File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes DAO without Factory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "DAO without Factory?" Watch "DAO without Factory?" New topic

DAO without Factory?

Tonny Tssagovic
Ranch Hand

Joined: Dec 30, 2003
Posts: 226
Hi guys,
I have a small question. I understand that DAOFactory is a nice thing, and it makes sure the right DAOImpl is returned according to your configuration. But do you really need to ALWAYS check your configuration/deployement file to return the right implementation? It is likely that you will be returning always the same thing, since these DATA sources don't change "everyday", and they could as well be HARd coded, or checked with something like a precompiler that will make sure your code is compiled with the right data code, and contains only that one.
Any thoughts? Would you make a DAO factory even if you don't anticipate to have dif. sources of the same data?
- BTW I have another question if you permit , would your factory return a new instance of the DAO each time it's getDAO is called, or rather have a thread safe singeleton?
Thank you for your time.
Tonny Tssagovic
Ranch Hand

Joined: Dec 30, 2003
Posts: 226
Just pushing this thread up
Ok, I have a DAO link for those new to the pattern, but still want your comments about the factory / singeleton and about using a DAO for each business object or one big do-it-all dao.
[ March 05, 2004: Message edited by: Tonny Tssagovic ]
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi Tonny,
IMO, DAOFactory could be an overkill for small projects. Though it does not require to be complex, in my experience its quite straightforward to implement. I would still advise, that even if you use a single class implementation, keep your data sources, etc in a config file (could be a properties file or xml, etc .. ) that can be changed without affecting the code.
I think the concept of DAO factory is only a clean way to implement data access tier that can change and be ported without affecting anything else in the application. This can just as well be done in a singleton/ any other class as long as you are sure that the 'contract' will not change between the data layer and other layers when the data layer changes (No ripple effects). As long as you are able to encapsulate the data layer, you can do without the factory.
As far as one big do it all DAO is concerned, I think I would not go for it as it defies all laws of OOP. Small manageable data objects are the way to go.
As far as using a singleton and new data objects each time is concerned, I think a singleton would be good if you were planning on implementing a data cache as well in your DAO. A DAO pool might be a good idea ;-)
Let me know what you think!
B Hayes
Ranch Hand

Joined: Feb 07, 2003
Posts: 61
I agree with the above poster. DAO Factory is not difficult to implement, so why not?
Clients are generally fickle anyways. They think they want data going one way, and then they want it going a different way. With a DAO Factory you only have to change a couple lines of code to accomodate this type of thing.
Tonny Tssagovic
Ranch Hand

Joined: Dec 30, 2003
Posts: 226
Thanks for the reply Aneesha Sing and R Hayes
Well I am quite sure, using a DAO factory is a good think since one can not predict the future, and as Hayes said it is few lines of code anyway.

But about the BIG do it all DAO, I don't know, although it will make it ugly it might have the benefit to share the connections to the specific source, whatever data on that source is used. I will be using a driver to an external source and it does not seem to be thread safe, so I don't know about the singleton. Although it will be too much overhead to create a DAO each time.
Could you please give comments about using a single DAO and init a new one for each data request?
Awaiting your comments.
I agree. Here's the link:
subject: DAO without Factory?
It's not a secret anymore!