Reputation: 5433
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>)
}
Upvotes: 0
Views: 86
Reputation: 92334
The correct thing is to use polymorphism using your list of the base type (List<SuperClass>
). You shouldn't handle them differently in your loop that iterates through the different sub classes. What you should do is call a specific method that is defined on the super class (better yet, an interface) and implement the method differently in each sub class (implementation). See Returning an extended class
interface MyInteface{
public void doSomething();
}
class Sub1 implements MyInterface{
public void doSomething() {
System.out.println("Behavior # 1");
}
}
class Sub2 implements MyInterface{
public void doSomething() {
System.out.println("Different Behavior");
}
}
for (MyInteface obj : superClasses) {
// Will do different things for different implementations
obj.doSomething();
}
The secret is to define what are the common aspects of the different implementations and be able to squeeze them all into the same signature.
Upvotes: 2