So, let’s say you have this:
if ([foo respondsToSelector:@selector(bar:)]) {
[foo bar:abc];
} else {
[qux mux:abc];
}
And both bar: and mux: have side effects.
How would you port that to Java, where there is no such things as @optional members in an protocol (interface)?
I can think of three ways:
- The C way: add a method to the interface that returns a bitfield representing which methods are implemented and valid.
- The COM way: modify the interface so that all methods return a result code and check for E_NOTIMPL. Use out params for return values.
- The (what I imagine is the) Java way: mark each interface method as throwing UnsupportedOperationException and catch them to check for unimplemented methods.
Did I miss any other compelling choices? Assuming this code is not frequently called so we don’t need to optimize for performance, I’m thinking 3 is the best way as it is enforcable. Are there arguments for the alternatives?
No, the thing that you describe as “the Java way” is actually an anti-way: there should be no catching of
RuntimeExceptionsubclasses in the regular flow of your program, because they signal programming errors.A better way would be to split the protocol into smaller parts: all required methods would end up in a single interface, and the optional methods would be in their own tiny interfaces. Now you can test your objects with
instanceofto determine if an optional interface and its implied method are implemented.Here is an example:
Then you can write this check: