using java generics to extend interfaces or just bad design?
I'm still new to the Java world (former C++ programmer). I designed an Interface hierarchy to force my implementation to @Override the methods in the interfaces. Now I'm in some kind of "how to do this in java". All bellow are interfaces.
XMLDecoder HTTPDecoder
| |
------------------
|
SSODecoder UserStuff
| |
开发者_StackOverflow -----------------------------
|
|
SSOBinding
|
|
Assertion
Now the fun part is I have an object implementing SSOBinding and another implementing Assertion. My problem is both HAVE to implement every method in XMLDecoder AND HTTPDecoder. I believe I have some pretty design but for that :(
Can Java Generics help me solve this problem? Something like:
Assertion<XMLDecoder> anAssertion = new Assertion<XMLDecoder>();
anAssertion.OnlyMethodsInXMLDecoder();
At least that's what I can think of for now but not having any success in implementing this.
Thanks a lot!
EDIT 1
Sorry for my poor explanation.
The hierarchy is not upside down (on top are the bases).
SSODecoder is my problem. I know it can have either require XMLDecoder OR an HTTPDecoder. Forget about SSOBinding. So I could have an implementation:
class AssertionImpl1 implements Assertion {
// Only implementing XMLDecoder
}
class AssertionImpl2 implements Assertion {
// Only implementing HTTPDecoder
}
The only difference for these to would be the method they need to implement for the decoder.
Short answer: No.
I'm guessing that you got your inheritance tree wrong: If not all instances of SSODecoder need to implement the methods in HTTPDecoder and XMLDecoder, then SSODecoder is not a subinterface of both.
You should look into an alternative design, where HTTPDecoder and XMLDecoder are strategies that are used by the SSODecoder. Force them to have the same interface, and make SSODecoder delegate to them... whatever responsibility they have.
That way, your call will be something like:
Assertion anAssertion=new Assertion(new XMLDecoder());
I'm not sure in which direction your "extends" relation goes, but if Assertion
extends SSOBinding
, SSOBinding
extends SSODecoder
and SSODecoder
extends XMLDecoder
and HTTPDecoder
then if two objects implements SSOBinding
and Assertion
respectively, *both will implement every method in XMLDecoder
and HTTPDecoder
.
精彩评论