Suppose I have an interface that supports several potential operations:
interface Frobnicator {
int doFoo(double v);
int doBar();
}
Now, some instances will support only one or the other of these operations. They can support both. The client code will not necessarily know until it actually receives one of the corresponding factories, through dependency injection or wherever it receives instances.
I see several ways to handle this. One, which seems to be a common tactic accepted in the Java API, is to simply have an interface as shown above and use unsupported methods UnsupportedOperationException. However, this has the disadvantage that it does not work with an error - the client code cannot determine whether it will doFoowork until it tries to call doFoo.
This can be supplemented with the methods supportsFoo()and supportsBar(), defined to return true, if the corresponding method works do.
, doFoo doBar FooFrobnicator BarFrobnicator . null, . instanceof, Frobnicator :
interface Frobnicator {
FooFrobnicator getFooFrobnicator();
BarFrobnicator getBarFrobnicator();
}
interface FooFrobnicator {
int doFoo(double v);
}
interface BarFrobnicator {
int doBar();
}
FooFrobnicator BarFrobnicator Frobnicator, get* as*.
: Frobnicator frobnicator, frobnicators ( as*). . , Frobnicator FrobnicatorEngine.
- , ? ? , ( , ), - , . , , : Frobnicator, (, doFoo , Foo), ..
: - . FooFrobnicator BarFrobnicator, , Guice-modules-as-configuration, (, factory/builder, ). , factory, frobnicators, ( , Guice), , : " frobnicator ". , , , .