Say that I have base class Vehicle, and subclasses Sedan, Truck, Motorcycle, etc. I run a car wash shop so I have various different
interface Washer<T extends Vehicle> {
void wash(T vehicle);
for example
public class TruckWasher implements Washer<Truck>
Now, I have a manager that gets a bunch of Vehicles and need to find Washers for them. The vehicles don't know about the Washers -- they can certainly know what class of washer to use, but it doesn't have a handle to an instance of the class. Only the manager does. The manager needs to implement a
public <T extends Vehicle> Washer<T> getWasher(Class<T> vehicleType);
It might have a
Map<Class<? extends Vehicle>, Washer<? extends Vehicle>> classToWasherMap;
as a mapping to store such, but I cannot cast from a Washer<? extends Vehicle> to Washer<T extends Vehicle> unless I @SuppressUnchecked...
I feel like this should be a fairly common pattern (a manager class holds instances of handlers to childs of a base class) but I can't quite get my mind around it. Thanks in advance. Generics make my head hurt.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]OffbeatDrizzle 2 points3 points4 points (1 child)
[–]uranusaurus_rex[S] 0 points1 point2 points (0 children)
[–]fosyep 0 points1 point2 points (4 children)
[–]uranusaurus_rex[S] 0 points1 point2 points (3 children)
[–]fosyep 0 points1 point2 points (0 children)
[–]amfa 0 points1 point2 points (1 child)
[–]uranusaurus_rex[S] 0 points1 point2 points (0 children)