Im looking to implement a modular generic object factory in Java.
My code will comprise of three basic elements:
- A DataLoader, which will provide a generic Collection
- A factory, which, given an element T, returns an object of type U
- A class that ties the two together, and provides a Collection
The bit Im struggling with is that class 3 doesnt care about the type T. What does matter is that the factory and DataLoader use the same T – ie the raw data provided by the DataLoader should be usable by the builder to build the objects provided by the third class – but the third class doesnt care what that intermediate format is.
So how do I mandate that the generic type T is the same for 1 and 2 without that type being part of the specification of 3?
One option is to use a helper class with two type parameters to bind the data loader and factory together, and then wrap that helper in another class that doesn’t care about the original data loader’s type:
You could even hide the existence of
Binderfrom outside code by givingClassNumber3a generic constructor: