1

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?

Eramol
  • 33
  • 7
  • What is your question? – JB Nizet Mar 30 '17 at 22:05
  • edited so it would brighten up the situation a little bit and added the question – Eramol Mar 30 '17 at 22:07
  • 1
    This code looks like it's starting to smell. You've got a bunch of mutable global variables, and you've got several classes which inherit from a common interface, but that interface is apparently useless for describing what the object does. – 4castle Mar 30 '17 at 22:10
  • 1
    Start with a `Map, List extends AbstractVariable>>`. – Louis Wasserman Mar 30 '17 at 22:10
  • This sounds very similar to Joshua Bloch's "Typesafe Heterogeneous Containers", although type erasure of the lists means you'd have to keep a `Map` of the generic type of each `List` to the `List` itself. Neal Gafter's "[Super Type Tokens](http://gafter.blogspot.com.uy/2006/12/super-type-tokens.html)" might solve that for you. (Note: Gafter's link to Joshua Bloch's THC pattern is long dead. Bloch covered them in his "[Still Effective After All These Years](https://www.youtube.com/watch?v=V1vQf4qyMXg)" talk, around 36mins in at the end of "Generics" (Item 29), before "Enums".) – Kevin J. Chase Mar 30 '17 at 22:12
  • it describes the property of the object. The final goal is constraint programming approach to planning. And as there are some actions, which affect let's say Locations, I need to filter the IDs of all the variables that implement the Location interface. It's not that much of an OOP, but so isn't the description. – Eramol Mar 30 '17 at 22:13
  • I feel this belongs on codereview.stackexchange.com, not here. I also feel that @LouisWasserman's comment is the correct answer. – Dawood ibn Kareem Mar 30 '17 at 22:23
  • Would collections.grouping by in Java 8 help you in this regard..am not dead certain..please have a look. – acearch Mar 31 '17 at 09:56

1 Answers1

0

Check on Google for the Visitor Pattern Design, as a little introductory explanation, you need to create an accept(Visitor) method in the AbstractVariable class, that recieves a Visitor instance and do visitor.visit(this). Then in the Visitor class you create as many methods as subclasses of AbstarctVariable exists, every method should be called visit(SubclassX) and SubclassX is going to be each one of the AbstractVariable subclasses. Finally the visit(SubclassX) method needs to call a addSublassX(SubclassX) on AbstractVariable, this method should be in charge of adding the instance of SubclassX to the corresponding List.

Richi
  • 1