I have a bunch of different processors and every processor has its own ProcessorRequest
and ProcessorResponse
classes.
public class Processor1 {
ProcessorResponse1 process(ProcessorRequest1 request) {
// do something
}
}
public class Processor2 {
ProcessorResponse2 process(ProcessorRequest2 request) {
// do something
}
}
public class Processor3 {
ProcessorResponse3 process(ProcessorRequest3 request) {
// do something
}
}
All ProcessorRequest
classes extend the BaseRequest
class, and all ProcessorResponse
classes extend the BaseResponse
class.
Now, I have requirement to make all Processor
classes implement a common interface BaseProcessor
so, that if possible do not touch existing code:
public interface BaseProcessor {
public RRR process(TTT request) throws Exception;
}
Where RRR
and TTT
are corresponding ProcessorResponse
and ProcessorRequest
.
I tried to do it with Generics.
public interface Baserocessor<T extends BaseRequest, R extends BaseResponse> {
public R process(T request) throws Exception;
}
But in this case, I need to change the signature of every process method and cast BaseRequest
to a ProcessorRequest
:
public class Processor1 {
BaseResponse process(BaserRequest baseRequest) {
ProcessorRequest1 request = (ProcessorRequest1) baseRequest;
// do something
}
}
Is there another way to do it without changes in every single processor?
You processor classes should implement the generic interface as follows:
public class Processor1 implements BaseProcessor<ProcessorRequest1, ProcessorResponse1> {
ProcessorResponse1 process(ProcessorRequest1 request) {
// do something
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments