Re: hierarchy of interface/implementations.
On Mar 24, 5:29 pm, horo...@gmail.com wrote:
I had a quick question about implementing interfaces and storing those
implementations..
suppose I have a implementation that I defined:
package mycom;
public interface MyInterface
{
public void mymethod();
}
and I put it in file:
mycom/MyInterface.java
and I want to have several different implementations defined for it,
that I want to stick under:
mycom/MyInterface/Implentation1.java
mycom/MyInterface/Implentation2.java
mycom/MyInterface/Implentation3.java
Can you do this? when I go to make my Implementation1, I say:
package mycom.MyInterface;
I get a conflict, apparently with the interface. But this seems the
logical way to structure things. My workaround is to make a directory:
mycom/MyInterfaceImpl
and stuff all implementations there, but this IMO is a hack and I'm
hoping that there are other ways around this (that make sense, camel
case promotes spelling errors, isn't compiler checkable, etc.)
Drop the upper-case letters to lower-case in the package name. That's
more conventional, and will avoid a conflict with type names, which by
convention must have upper-case letters in them.
Forget directories - think in terms of packages and types, then work
out the directories after the fact.
You have more flexibility with names if you have a two-level domain
followed by functional package names.
One-level domain (per your example):
mycom.MyInterface
mycom.impl.Implentation1
mycom.impl.Implentation2
etc.
"MyInterface", "impl" and "ImplementationN" are far too general and
semantically useless names to really show the power, though.
Two-level domain, e.g., lewscanon.com, for a product known as "Killer
App":
com.lewscanon.killerapp.Potable;
com.lewscanon.killerapp.potable.Water;
com.lewscanon.killerapp.potable.Juice;
com.lewscanon.killerapp.potable.Whiskey;
Another way, more structurally bound to the code:
com.lewscanon.killerapp.potable.Potable;
com.lewscanon.killerapp.potable.impl.Water;
com.lewscanon.killerapp.potable.impl.Juice;
com.lewscanon.killerapp.potable.impl.Whiskey;
And the way I usually prefer:
com.lewscanon.killerapp.potable.Potable;
com.lewscanon.killerapp.potable.Water;
com.lewscanon.killerapp.potable.Juice;
com.lewscanon.killerapp.potable.Whiskey;
--
Lew