Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it at all acceptable to have nested public interfaces?

Tags:

java

I have several interfaces all with only a single method and they are all very similar. I know it works, but should I or should I not group them like this:

public class InterfaceGroup {
    public interface Type1 {
        public void method(int a);
    }
    public interface Type2 {
        public void method(String s);
    }
    public interface Type3 {
        public void method();
    }
}

And then reference them externally as InterfaceGroup.Type1.

like image 649
rtheunissen Avatar asked Dec 05 '25 17:12

rtheunissen


1 Answers

Yes, it is sometimes helpful to have such a design. For example, it is the choice for Map.Entry in Java standard library.

The blemish on such a design, though, is that you define a type that doesn't play the role of a type, and it is actually never appropriate to implement it. If I found out that in a library I use such a type exists for no other reason than to reduce the number of source code files, I'd be at least slightly annoyed :-) On the other hand, when I'm writing code whose scope of visibility is just within the implementation, I use such tricks remorselessly.

like image 186
2 revsMarko Topolnik Avatar answered Dec 08 '25 07:12

2 revsMarko Topolnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!