Question in brief: Is it possible to automatically acquire a list of all instantiated objects of a certain type?
Question in full:I have a BaseController and BaseDragger class that each have to know about instances of the other: a single Controller object must be instantiated and this needs to contain an ArrayList of all Dragger objects. Each Dragger needs to know about the Controller.
My first attempt was to instantiate the Controller, create the Draggers (passing a reference to the controller as a constructor argument) and then using a method to add an ArrayList of all the Draggers to the controller at the end of setup.
That seemed clunky. Since the controller is being passed an array of all Draggers I realised I could simply iterate through this when the Controller is created and add a reference to itself to all draggers. My Controller constructor now looks like this:
Code: BaseDragController(ArrayList allTargets, ArrayList allDraggers) {
this.allTargets = allTargets;
this.allDraggers = allDraggers;
for(int i=0; i<allDraggers.size(); i++) {
BaseDragger currentDragger = (BaseDragger) allDraggers.get(i);
currentDragger.controller = this;
}
}
The problem I have with this is that if a Dragger isn't added to the ArrayList passed to the Controller in setup you won't know about it until you try and use that Dragger in the sketch... So I was wondering whether it's possible to avoid the need for manually constructing an ArrayList of all draggers and having to pass this as an argument to the Controller: i.e. Is it possible to automatically get a list of all instantiated objects of a certain type?
If not how might I catch this error when the Controller is instantiated?