Search code examples
javainterfaceportingobjective-c-protocol

Porting @optional selectors in protocols to Java interfaces


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:

  1. The C way: add a method to the interface that returns a bitfield representing which methods are implemented and valid.
  2. 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.
  3. 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?


Solution

  • No, the thing that you describe as "the Java way" is actually an anti-way: there should be no catching of RuntimeException subclasses 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 instanceof to determine if an optional interface and its implied method are implemented.

    Here is an example:

    @protocol MyProtocol
    @optional
        -(void)method1;
        -(void)method2;
    @required
        -(void)method3;
        -(void)method4;
        -(void)method5;
    @end
    
    interface IMyProtocol {
        void method3();
        void method4();
        void method5();
    }
    
    interface IMyProtocolMethod1 {
        void method1();
    }
    
    interface IMyProtocolMethod2 {
        void method2();
    }
    
    class MyProtocolImplWithMethod2 implements IMyProtocol, IMyProtocolMethod2 {
        public void method2() {
        }
        public void method3() {
        }
        public void method4() {
        }
        public void method5() {
        }
    }
    

    Then you can write this check:

    if (obj instanceof IMyProtocolMethod2) {
        ((IMyProtocolMethod2)obj).method2();
    }