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 & HelpSyntax Questions › Event notification between objects
Page Index Toggle Pages: 1
Event notification between objects (Read 517 times)
Event notification between objects
Jan 13th, 2010, 3:45pm
 
I want to learn how to add 'event notification' between classes and am wondering how best to proceed.  Since this is just a learning exercise I'm working with my layer ordering demo.  In that example I want my bfLayerManager class to know when a new layer is added or removed to/from bfLayers.

The reason I'm posting the question under 'syntax' is I gather there's some kind of event handling system built into Java (I could be totally wrong here!) and I wondered if that was the simplest approach.

Otherwise, since the bfLayerManager class has a reference to bfLayers I can think of various crude approaches I could take - e.g. creating dedicated methods to pass 'events' between the two classes.  However that seems rather inelegant - I'd much rather be able to have a generic solution that any class can use to listen for events in the bfLayers class.

I'll go away and try and read up on this; but in the meantime if anyone has any suggestions on where to start they'll be gratefully received Smiley
Re: Event notification between objects
Reply #1 - Jan 14th, 2010, 1:11am
 
I'd go with the Observer pattern: http://en.wikipedia.org/wiki/Observer_pattern

Just make a class where you can register event listeners and then send events to that class, and they will be dispatched to the classes that are listening.
Page Index Toggle Pages: 1