I have an abstract class Vehicle
with 2 implemented subclasses RedVehicle
and YellowVehicle
.
In another class I have a List<Vehicle>
containing instances of both subclasses.
I want to be able to pass into a method a class type and then use that type to decide which set of objects I want to do something to in the List
.
Since Class
is generic I should parameterise it with something, however putting the parameter as the parent class Vehicle
stops the calling code working since exampleMethod
is now expecting a type of Vehicle, not a subclass of RedVehicle
or YellowVehicle
.
I feel there should be a clean way to do this so what would be the correct way to implement the functionality?
n.b. I don't necessarily have to pass in the Class
type, if there are better suggestions I'd be happy to try those.
Calling code:
service.exampleMethod(RedVehicle.class);
service.exampleMethod(YellowVehicle.class);
Fields/Method:
//List of vehicles
//Vehicle has 2 subclasses, RedVehicle and YellowVehicle
private List<Vehicle> vehicles;
//Having <Vehicle> as the Class parameter stops the calling code working
public void exampleMethod(Class<Vehicle> type)
{
for(Vehicle v : vehicles)
{
if(v.getClass().equals(type))
{
//do something
}
}
}
Do this instead:
Why don't you use the visitor pattern?
That way you
if(v.getClass().equals(type))
)In detail:
your abstract class
Vehicle
gets a methodaccept(Visitor v)
, with the subclasses implementing it by calling the appropriate method onv
.Using a visitor: