This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why do we need java interfaces for DAOs

 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic