Is it a bad practice to "mix class and interfaces in one package"?

I just found something that I had never heard of before, and I disagree (by now). In the answer I read "why mix class and interfaces in one package"

So itโ€™s interesting if there are reasons to separate interfaces and implementations in Java.

I know that we are not required to have all implementations in an interface package, but is it (sometimes) reasonable not to have it?

Yours faithfully
Mike
[; -)

+7
source share
4 answers

Reasons to store interfaces and implementations in separate packages:

clear code base . It looks โ€œbetterโ€, more accurate if we have one package with interfaces and another with implementations (usually something.impl namespace). And the code structure shows / reflects what you code against interfaces.

access modifiers . We can use package private access modifiers for the package private API to implement related interfaces.

The structure of the library . Perhaps one day you may decide to create different libraries for the API (s) and implementation (s). Then itโ€™s nice to have interfaces and implementations in different packages. Thus, you can change the assembly without refactoring your code base.

+8
source

I agree with org.life.java - I will have the service and basic packages service.impl, but always in this form.

I do not agree with the phrase "bad practice." This is too strong.

The java.util Collections API API contradicts this advice. I would not want to be the one who told Joshua Bloch that he did a "bad job."

+10
source

For OSGi, it is almost necessary to use separate AFAIK packages so that you can export / import the API without exporting / importing the implementation.

For interfaces that are only internally, however, it is not a problem to save everything in one package.

+7
source

This is not bad, but, of course, itโ€™s good practice to distinguish between interface and implementation in different packages.

eg

 com.mycompany.domain.service com.mycompany.domain.service.impl 

<strong> Benefits:

  • Unified packaging structure
  • For a while, when you want to process only certain classes, you can split it into a package
+3
source

All Articles