This is a logical development of another question I asked.
Supposedly you have an interface, some methods of which might either be supported by a concrete implementation or not. The goal is to provide a reasonable way for the client to find out whether his particular implementation supports each particular method and to recover if it doesn't.
The solution I came up with utilizes the standard java.lang.UnsupportedOperationException
, which is thrown by the concrete implementation if the method is unsupported:
public interface CommonInterface {
void possiblyUnsupportedOperation () throws java.lang.UnsupportedOperationException;
}
However, this is claimed to be a bad solution, since the exception mechanism should not be used as a means of checking whether an operation is available. So the alternative suggestion is to use tester methods:
public interface CommonInterface {
void possiblyUnsupportedOperation ();
boolean isOperationSupported ();
}
But what if the interface has a whole multitude of optional operations? Should I use multiple tester functions? Should I create a separate Enum to map the optional methods onto and pass it as a method descriptor to a single tester function? Both variants look kinda clunky to me.
Is there an alternative way that is both an elegant code and a good design solution?