Reputation: 963
I'm writing some simple DAOs in Java using JDBC (no Spring, Hibernate or anything else).
Is it better to put the implementation DAOs in the same package as their interfaces or to put them in a sub-package?
Example:
com.mycompany.myproject.dao.MyDao
com.mycompany.myproject.dao.MyDaoImpl
OR
com.mycompany.myproject.dao.MyDao
com.mycompany.myproject.dao.impl.MyDaoImpl
If you suggest the sub-package structure, what would you suggest as a sub-package name? .impl? .sql? .jdbc?
Realistically, I'm not going to have multiple implementations. Am I over-engineering this?
Upvotes: 10
Views: 16925
Reputation: 868
One purpose of packages is to improve readability for the other programmers on your team. Generally, I put the implementation in the same package as the interface, because that is the obvious place to look, and in most cases, the DAO implementation is simple. If your implementation is complex, then you should find a framework that is appropriate for your application.
Other reasons to consider a separate package include: if you are writing a library that will be used by other groups, or if you want to support multiple implementations.
Upvotes: 0
Reputation: 7858
When designing an application there is no standard way of structuring in packages, experience is what usually helps every one to decide what are the appropriate names for our packages.
About packaging implementations of your interfaces in the same package or in a different one just think about how Java itself is structured: usually an implementation class is packaged in the same package that its interface, but is not all the times.
If you were about to have several implementations of the same DAO's then it would make sense having them structured in .jdbc
, .jpa
or .jdo
sub packages. If your are only going to have one implementation both of the options you enumerate make sense in some way (same package or a .impl
sub package).
Regarding over-engineering I would recommend you this article. Even though you are going to have just one implementation of your DAO's, it would make sense to have them defined as an interface and implementation as that will help you in a potential future to rewrite your DAOs for other frameworks whilst the code that makes use of them keeps unchanged.
At the end it's up to you (or you and your peers) to reach a consensus and make the decision that makes more sense in your specific case.
EDIT
An application usually has one implementation per DAO interface and that isn't over-engineering at all, it simply doesn't make sense to have the same DAO interface implemented for JPA and for JDO. Some of the purposes of using the interface/implementation pattern is to ease re-factoring, testing by means of mock objects, etc..
P.S.: I usually rely on JDepend to distribute my application classes in packages avoiding cycles as most as I can.
Upvotes: 6
Reputation: 47607
I would go with your second option (although none is really better), because you can see immediately in your imports if an impl is imported and refactoring would be simpler if you want to move your impl in another project.
This is not over-engineering. There are multiple advantages to use DAO:
Upvotes: 3
Reputation: 15363
Namespaces and packages only exist to prevent collisions. Neither is preferable as long as they are unique.
Upvotes: 0
Reputation: 49197
I don't think either is better, but in this case I prefer the first alternative. It would be in line with having ArrayList
, LinkedList
, etc. , in the same package as List
.
When using additional frameworks, such as hibernate
I prefer the second option with MyDao
and HibernateDao
as the implementor.
Upvotes: 3