Let’s say I have an interface:
package org.my.dicom.CEchoSCU;
public interface CEchoSCU {
// stuff here...
}
I will also have an implementation of the interface:
public class SimpleCEchoSCU implements CEchoSCU {
// stuff here...
}
My question is, which package should house the implementation? I have seen other developers place the implementation is in the same package as the interface (org.my.dicom, in this case), and also cases where a separate package is used (typically something like org.my.dicom.impl). Other than personal preference, is there any reason to prefer one over the other?
A class implements, among other things, an interface. I would stick the class in a package that is related to what the rest of the class does/contains, not a package that reflects one interface it implements. For instance, if I had 200 classes that implement Printable, I wouldn’t stick them all in a package com.mycompany.impl.printable. The Airplane class that implements printable would go in a package with other airplane stuff. The Boat class would go in a package with other boat stuff. The Car class that implements Printable would go in a package with other car stuff.
If all SimpleCEchoSCU (and any other future classes) does is implement CEchoSCU, then you should consider making CEchoSCU an abstract base class and deriving different implementations. And either way, (interface or abstract base class) things would be completely related and I would put them in the same package.