I ran into some Java code at work today :
public interface A {
public static interface AObj {
String toXml();
int getCode();
}
public static interface AMap {
String toXml();
int blah();
}
...
}
I have not encountered having static interfaces embedded in an existing interface before. It doesn’t make much sense. Can someone comment on :
a) Is this good design practice
b) What the benefits are to this vs Having individual interface files.
c) This was written by a c++ programmer, i get the impression they’re trying to re-create namespaces. If that’s the case, why not just use packages ?
Any comments most welcome.
Regards,
Alistair.
Good/bad practice can be debatable. Typically one would nest interfaces if the nested interface is an inseparable entity in relation to its parent.
Take a look at the source code of
java.util.Map. It merrily embedsMap.Entryinside it. The author is Josh Bloch, who is definitely not an inexperienced Java developer. So I’m sure there must be a valid reasoning behind designing the interface that way.Here is what it looks like (JavaDoc and comments stripped).