Question

I've just moved into java (from AS3) and I understand the syntax and all the basics, but I can't find any tutorials or books on basic program architecture.

For example,

ObjectA has a property: ObjectB. 
ObjectB has a property: x
ObjectB.x changes at some point. 

How can I have Object A 'catch' or 'hook into' or 'listen for' the change in ObjectB.x?

In AS3, I would make ObjectB an event Dispatcher and have ObjectA listen for some custom Event.

Or in the Flex Framework I would make a changeWatcher.

How is this done in Java and where can I find these architectual basics?

Was it helpful?

Solution

Check out the JavaBeans specification, it includes guidelines on how to create events and listeners so that they conform to the standard (it was standardized because Sun thought there was going to be some Delphi-like market in GUI widgets which never materialized, but it doesn't hurt to have a standard way of doing things). Here's a tutorial on writing event listeners.

OTHER TIPS

If you really need to, you can create an Event and listen to it.

In your ObjectB.setX() method, you can fire your Event to let ObjectA know the value has been changed.

Here is an article that describes how to do it : http://www.javaworld.com/javaqa/2000-08/01-qa-0804-events.html

But I'm not sure I would do it this way. In my ObjectB.setX(), I would look for an ObjectA parent, and if it exists, use a function defined on ObjectA.

You may want to look at observer pattern

In Java bean world, observer pattern is implemented using property change listener This java tutorial provides a good example. Ignore the Java bean GUI part of it and focus on the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top