Specifically, I have a list of objects. Each subclass of that class of objects is associated with a specific piece of functionality.
Here is my naive solution to this problem, and it is ugly:
List<SuperClass> superClasses = ...
List<Subclass1> obj1 = Lists.newArrayList();
List<Subclass1> obj2 = Lists.newArrayList();
List<Subclass1> obj3 = Lists.newArrayList();
List<Subclass1> obj4 = Lists.newArrayList();
...
for (SuperClass obj : superClasses) {
if (obj.getClass().equals(Subclass1.class)) {
obj1.add((Subclass1) obj);
}
}
//repeat for each subclass
I also tried something like this:
public Map<Class, List<SuperClass>> constructMap(List<SuperClass> superClasses);
but this couldn't be used:
(List<Subclass1>) constructMap(superClasses).get(Subclass1.class)
cannot cast from List<SuperClass> to List<Subclass1>.
Am I doomed to the naive code here? Is there really no smart way to take in a list of super-class objects and handle them based on their actual class?
Here is the problem I am trying to solve:
public Driver {
List<Fruit> fruits = collectAllFruit();
StemHandler.pullStem(fruitsThatAreApples, fruitsThatArePears);
ColorHandler.getColor(fruitsThatAreApples, fruitsThatAreOranges, fruitsThatArePears);
public interface Fruit {
getColor();
}
public class Apple implements Fruit {
getColor();
pullStem();
}
public class Orange implements Fruit {
getColor();
peel();
}
public class Pear implements Fruit {
getColor();
pullStem();
}
public interface FruitHandler {
}
public class StemHandler extends FruitHandler {
pullStem(List<Apple>, List<Pear>)
}
public class ColorHandler extends FruitHandler {
getColor(List<Apple>, List<Orange>, List<Pear>)
}