Interface with Generic method

EasyRider

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?

Vladimir Vagaytsev

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic method inside interface

From Dev

Generic method inside interface

From Dev

Interface with the method of generic type

From Dev

TypeScript Generic Method Signature in Interface

From Dev

Overload generic method from interface

From Dev

Generic interface with a method that return a list

From Dev

Override java generic method in interface

From Dev

Calling a generic method with interface instances

From Dev

How to write an interface for a generic method

From Dev

How implement an interface Generic Method

From Dev

Static Method in Interface with Generic signature

From Dev

How to implements a method of a generic interface?

From Dev

How implement an interface Generic Method

From Dev

Generic type in interface method Java

From Dev

Generic type constraints on an interface method

From Dev

Declare Generic method in non generic interface

From Dev

Specialize type in method that implements a generic interface method

From Dev

Difference between Generic method with interface constraint and method with interface parameters

From Dev

Generic method: Contraint with Interface, how to access properties

From Dev

Generic method where T implements Interface<T>

From Dev

Java - How to define interface method with generic

From Dev

Method parameter: Interface VS Generic type

From Dev

Multiple implementations of a generic interface and extension method discovery

From Dev

Using generic interface as typeparameter for a method or function

From Dev

interface as argument or generic method with where - what is the difference?

From Dev

Generic method with return type from interface

From Dev

Creating a method that returns an object implementing a generic interface

From Dev

Calling a Generic Interface Method does not work

From Dev

Mocking method with generic functional interface as an argument - Mockito

Related Related

  1. 1

    Generic method inside interface

  2. 2

    Generic method inside interface

  3. 3

    Interface with the method of generic type

  4. 4

    TypeScript Generic Method Signature in Interface

  5. 5

    Overload generic method from interface

  6. 6

    Generic interface with a method that return a list

  7. 7

    Override java generic method in interface

  8. 8

    Calling a generic method with interface instances

  9. 9

    How to write an interface for a generic method

  10. 10

    How implement an interface Generic Method

  11. 11

    Static Method in Interface with Generic signature

  12. 12

    How to implements a method of a generic interface?

  13. 13

    How implement an interface Generic Method

  14. 14

    Generic type in interface method Java

  15. 15

    Generic type constraints on an interface method

  16. 16

    Declare Generic method in non generic interface

  17. 17

    Specialize type in method that implements a generic interface method

  18. 18

    Difference between Generic method with interface constraint and method with interface parameters

  19. 19

    Generic method: Contraint with Interface, how to access properties

  20. 20

    Generic method where T implements Interface<T>

  21. 21

    Java - How to define interface method with generic

  22. 22

    Method parameter: Interface VS Generic type

  23. 23

    Multiple implementations of a generic interface and extension method discovery

  24. 24

    Using generic interface as typeparameter for a method or function

  25. 25

    interface as argument or generic method with where - what is the difference?

  26. 26

    Generic method with return type from interface

  27. 27

    Creating a method that returns an object implementing a generic interface

  28. 28

    Calling a Generic Interface Method does not work

  29. 29

    Mocking method with generic functional interface as an argument - Mockito

HotTag

Archive