I have got a lot of classes, which are all derived from the class AbstractVariable. I need to collect them into corresponding lists because I'll need to work with them. It may be simpler to collect them into one list and then filter them by instanceof by the getters, I am not sure however about the performance.
public class VariableRecorder {
private static List<Beacon> beacons = new LinkedList<>();
private static List<Engineer> engineers = new LinkedList<>();
private static List<Heli> helis = new LinkedList<>();
private static List<Locatable> locatables = new LinkedList<>();
private static List<Location> locations = new LinkedList<>();
public static void record(AbstractVariable av) {
if(av instanceof Engineer)
engineers.add((Engineer) av);
if(av instanceof Heli)
helis.add((Heli) av);
if(av instanceof Locatable)
locatables.add((Locatable) av);
if(av instanceof Location)
locations.add((Location) av);
if(av instanceof Beacon)
beacons.add((Beacon) av);
}
}
Some of those are classes, some of those are decorator interfaces. Visitor seems like an overkill as it would have to be declared in all classes including subclasses and considering that one class may have multiple interfaces, the maintenance would be horrific.
This is not the final list of classes nor interfaces. It will get much bigger.
Right now I am really considering the getter filtering, as this selection is only at the very beginning of the algorithm.
The question is - what is the best design choice in this matter? Visitor? Getter filtering? Maintaining different lists? Or some other design pattern?