Skip to content
Advertisement

Pattern for Java generics for “internal” type

I have a class. Its public interface (excluding the constructor) is not generic, but its internal logic is. A simplified example of this is:

JavaScript

Note that (after construction), the user of Summarizer has no interest in T‘s type. In my case, the type isn’t known at compile time (we use reflection to construct ComplicatedProducer and Processor), and I’d rather not have to have a factory function that returns Summarizer<?>. Is there any clean way to make Summarizer non-generic without resorting to the use of raw types?

Advertisement

Answer

You can always have a single-level wrapper:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement