aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes why do we need java interfaces for DAOs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "why do we need java interfaces for DAOs" Watch "why do we need java interfaces for DAOs" New topic
Author

why do we need java interfaces for DAOs

Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
I wanted to ask,

why do we need java interfaces for DAOs? I mean instead of coming through CustomerDAOInterface, if I access CustomerDAO directly from sessionbean/service-layer, what's wrong with it? If I need to add a new method in future, I can still add it to CustomerDAO, if I need to change a method in CustomerDAO I can do that. If I need to use a different persistence package I still will be working with CustomerDAO class only in both the cases.

So why should I use interface for DAO classes?


regards
VC


-Varun -
(My Blog) - Online Certifications - Webner Solutions
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The idea is that this way you could choose at runtime which DAO implementation to use.

A common use is to use a different DAO implementation for unit testing (for example by using DAO mocks). It would also make it possible to have, say an OracleCustomerDao, MsSqlCustomerDao, XmlCustomerDao, etc. pp.


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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why do we need java interfaces for DAOs