How to use a java interface with multiple implementation classes
public interface Foo {
public interface Foo { } public class SpecificFoo implements Foo { } public interface SomeInterface { void thisMethod(Foo someKindOfFoo); } public class SomeClass implements SomeInterface { public void thisMethod(Foo someKindOfFoo) { // calling code goes into this function System.out.println("Dont go here please"); } public void thisMethod(SpecificFoo specificFoo) { // not into this function System.out.println("Go here please"); } } public class SomeOlderClass { public SomeOlderClass( SomeInterface inInterface ) { SpecificFoo myFoo = new SpecificFoo(); inInterface.thisMethod(myFoo); } }
Calling code:
SomeClass myClass = new SomeClass(); SomeOlderClass olderClass = new SomeOlderClass(myClass);
I have a someinterface and several class calls (such as someolderclass) I have a class that implements interfaces, but I want to do type - safe operations on specific implementations passed to generic interfaces
As shown in the above code, I really want to be able to create another method that matches the specific type of the incoming interface It doesn't work I assume this is because the calling code only knows the interface, not the implementation with more specific methods (even if specificfoo implements foo)
So how can I do this in the most elegant way? I can make the code work by adding an IF statement to the class that implements the interface (someclass):
public void thisMethod(Foo someKindOfFoo) { // calling code goes into this function if ( someKindOfFoo.getClass().equals(SpecificFoo.class) ) thisMethod(SpecificFoo.class.cast(someKindOfFoo)); else System.out.println("Dont go here please"); }
However, this is not elegant because I add an IF statement every time I add a new foo I may forget to do so
Another option is to add specificfoo to someinterface so that the compiler can remind me that it needs to be implemented in someclass This problem is that I finally added quite a lot of boiler board code (if others implement the interface, they must implement the new method, as well as any tests)
Considering that Foo and specific foo are related, it seems that there should be another option I lack Ideas?
More information:
Well, I actually worked for a while trying to simplify the problem As I added more details, the complexity increased a lot But anyway... I think I can explain
Basically, I'm writing a GWT web application RPC servlet using command mode, as ray Ryan explained in his talk
There are several implementations of Google Code, but many of them have this inheritance problem I think this is an error in the bugreport of gwt-rpc code. Because I further implemented it, I noticed that a similar problem occurred purely on the client and in managed mode (that is, all Java, no GWT JavaScript madness)
Therefore, I abstracted the basic idea into the original Java command line case and saw the same problem, as described above
If you follow ray Ryan's discussion, foo is an action and specificfoo is the specific action I want to call Someinterface is a client RPC service and someclass is a server RPC class Someolderclass is an RPC service that can understand caching and things like that
Obviously, right? As I said, I think all the GWT RPC nonsense will only spread on the basic issues, which is why I try to simplify it
Solution
If you need to find out the actual type of object at run time, the design is likely to be wrong This at least violates the open closed principle and dependency inversion principle
(Java does not have multiple dispatch, so this method (foo) will be called instead of this method (specificfoo) You can use double dispatch to bypass language constraints, but there may still be some potential design problems...)
Please provide more information about what you want to do At present, the problem does not provide enough information to propose the correct design
The general solution is that since the action depends on the runtime type of foo, the method should be part of foo so that its implementation can vary according to the type of foo Therefore, your example will be changed as follows (someinterface or other parameters may be added to thismethod())
public interface Foo { void thisMethod(); } public class SpecificFoo implements Foo { public void thisMethod() { System.out.println("Go here please"); } }