We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › getting a list of all objects of a certain class
Page Index Toggle Pages: 1
getting a list of all objects of a certain class (Read 686 times)
getting a list of all objects of a certain class
Nov 10th, 2009, 1:27am
 
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?
Re: getting a list of all objects of a certain class
Reply #1 - Nov 10th, 2009, 4:49am
 
blindfish wrote on Nov 10th, 2009, 1:27am:
Question in brief: Is it possible to automatically acquire a list of all instantiated objects of a certain type

I don't think so (not easily... maybe with reflection).

Untested pseudocode suggestion... (p1, p2 stand in for whatever parameters you need other than the base controller to initialise a dragger).

Code:
BaseController controller = new BaseController();

controller.newBaseDragger( p1, p2 );

class BaseController
{
 private ArrayList allDraggers;

 public void newBaseDragger( int p1, int p2 )
 {
   allDraggers.add( new BaseDragger( this, p1, p2 ) );
 }
}

class BaseDragger
{
 private BaseController controller;
 int field1;
 int field2;
 int field3;

 public BaseDragger( BaseController controller, int p1, int p2 )
 {
   this.controller = controller;
   field1 = p1;
   field2 = p2;
 }
}


Re: getting a list of all objects of a certain class
Reply #2 - Nov 10th, 2009, 4:57am
 
blindfish wrote on Nov 10th, 2009, 1:27am:
Is it possible to automatically acquire a list of all instantiated objects of a certain type

No.
Unless you use reflection, but it is kind of overkill.

If I understood correctly the problem, you want something like the Observer pattern or the Listener Pattern: the Controller indeed maintains an ArrayList of Draggers, but that's each Dragger, when instantiated, which registers itself to the listener/observer. Thus, you can't miss a registration.

If I answer beside the question, just nag me... Smiley
Re: getting a list of all objects of a certain class
Reply #3 - Nov 10th, 2009, 7:09am
 
Doh!

Of course - I just pass the Controller as an argument to the Dragger and the Dragger adds itself to the ArrayList in Controller...  and that means there's no need to construct an ArrayList of Draggers to pass as an argument to the Controller == less work!

I actually haven't really read up on design patterns properly yet: obviously something worth following up on!

Thanks Smiley
Page Index Toggle Pages: 1