I have an interface
interface XXXCommandHandler(){
void parse(String something);
String response();
String additionalResponse();
}
- Some of the classes that implement XXXCommandHandler do not implement additionalResponse().
- I am using ApplicationContextManager.getInstance().getBeansOfType(XXXCommandHandler.class) to get the classes that implement XXXCommandHandler
- Then call parse, response and additionalResponse
- Since some do not implement additionalResponse I am forced to return null.
I can think of the following
- Instead of returning null on classes that do not implement additionalResponse, declaire additionalResponse as default method and return null / or make it return Optional etc and override it on the classes that implement additionalResponse method.
- Ugly way :- return null in all the classes that do not implement additionalResponse
-
Create two different interfaces XXXCommandHandlerParser() with parse and response method and XXXCommandHandlerAddtionalresponse() with additionalResponse method extending XXXCommandHandlerParser i.e
interface XXXCommandHandlerParser(){
void parse(String something); String response(); }
interface XXXCommandHandlerAddtionalresponse() extends XXXCommandHandlerParser {
String additionalResponse(); }
-
But if I do #3 I had to change ApplicationContextManager.getInstance().getBeansOfType(XXXCommandHandlerAddtionalresponse.class).
- If I do #4 then classes that do not implement additionalResponse or that do not implement XXXCommandHandlerAddtionalresponse will not be picked up.
Can you think of any elegant way?
Aucun commentaire:
Enregistrer un commentaire