Should interfaces in Java reside in their own directory? O开发者_JS百科r should both the interface and its implementation be placed in the same directory (package)? Thanks.
Interfaces don't specifically need their own directory. They should be placed where it makes sense, just as classes should be placed where they make sense. In many cases, it may make sense to put them in the same place.
One pattern I've seen is to put the interfaces in a base directory, then put the implementations in a subdirectory from there.
For example, interfaces might go here:
com.myproject.data.dao.CustomerDao (some people do ICustomerDao for interfaces, but some don't like that.)
com.myproject.data.dao.ProductDao
And the implementations might go here:
com.myproject.data.dao.hibernate.HibernateCustomerDao
com.myproject.data.dao.hibernate.HibernateProductDao
com.myproject.data.dao.someotherorm.SomeOtherOrmCustomerDao
etc.
This might work in some situations, and might not in others, but just something to think about.
As there are already some good points, I just wanna add one thing:
In some projects we have even gone so far that we placed all interfaces into one sub-project (maven module) and implementations into another one. This way it was possible to FULLY seperate the interfaces from the implementations and finalize the interface project very early in the project and deliver it to other teams working agains those interfaces. Within each of the projects we used the same packages.
In general I would say, you should seperate interfaces and their implemetations, the way doesnt really matter, as long as you are consistent with it.
Same package. The user should not know or care that they are using an interface
Wherever you want, but it is absolutely fine to keep interfaces in the same package and directory structure. Just look at the java api. If you select any of the packages you will notice that many contain both classes and interfaces. Some of the interfaces are implemented by classes in the same package, and some are not.
I think the worst practice is to insist that you must have a different directory for interfaces. I have seen directories like /services and /impl and others, which only muck up the directory structure. At my current workplace we employ a lot of contractors that come and go, and some of our projects have multiple types of interface directories. The only time I think it makes sense to use a separate directory is if you plan to copy the interfaces into other projects, say for EJBs, but even then they can have the same package if you use a shared project for the interfaces.
So short answer is, anywhere you want, but don't think you need to separate your classes and your interfaces. In many cases it is preferable to keep them in the same package/directory.
Its Not at all necessary to place the interface in the same directory(package). If your interface has a public access, then you can import it anywhere, in any package.
The question as I read it (but then it's strangely formulated) is not if interface should be in their own directory or not. The question is if you should recreate your complete directory structure (bold to emphasis what is in the question's title) where one branch would contain only interfaces, like this:
pureooabstraction/
|
|_com/
|
|_example/
|
|__SomeInterface.java
|__SomeOtherInterface.java
src/
|
|_com/
|
|_example/
|
|__SomeClass.java
|__...
Where the pureooabstraction/ directory structure would contain only "pure abstract classes" (from an OO point of view, not the Java 'abstract' definition), aka interfaces in Java.
And the petty implementation details (which don't exist at the OOA/OOD level) where "code" lies would go in the src/ directory.
It certainly makes sense if your development process goes from OOA to OOD to OOP.
精彩评论