tags:

views:

689

answers:

2

Hi , I would like to know , how the java beans property change listeners work. Do they use , EventListeners inside ? Is it good to use , property change listeners when we can do same with POJO implementation of mediator pattern . I mean performance wise ?

Thanks J

+1  A: 

The code you look for is in java.beans.PropertyChangeSupport. You use it like this:

protected transient PropertyChangeSupport changeSupport = new PropertyChangeSupport (this);

public void addPropertyChangeListener (String propertyName, PropertyChangeListener listener)
{
    changeSupport.addPropertyChangeListener (propertyName, listener);
}

public void removePropertyChangeListener (String propertyName, PropertyChangeListener listener)
{
    changeSupport.removePropertyChangeListener (propertyName, listener);
}

public void firePropertyChange (String propertyName, BigDecimal oldValue, BigDecimal newValue)
{
    if (oldValue != null && newValue != null && oldValue.compareTo (newValue) == 0) {
        return;
    }
    changeSupport.firePropertyChange(new PropertyChangeEvent(this, propertyName,
                                               oldValue, newValue));

}

The main advantage of using this API is that everyone is familiar with it. The main disadvantage is that the Beans API is pretty old, cumbersome from todays point of view and very limiting.

For example, you need the name of a property in many places. This either means you must copy a string (which breaks if you rename the property) or you must manually define a String constant for every field which is tedious.

The implementation itself is pretty fast and follows the Observer design pattern. Of course, there are other ways to implement this. The price would be that this is no longer a bean since it doesn't follow the API. Hence, you can't use your object in many frameworks without additional glue code.

Aaron Digulla
Yeah . But I have seen EventListeners and Streams inside the implementation of these PropertyChangeSupport . So I was little doubtful that , will it have cost of performance . That's why I was asking is it Ok to replace this with mediator pattern implementation , What say ?
Jijoy
Typically if you're using PropertyChangeListeners within UI code the performance overhead in delegating to PropertyChangeSupport will be unnoticeable.
Adamski
Ok. Is the property change listeners will be treated same way as the eventlisteners inside awt. I mean will the program have to take care about it disposl ? or JVM will do ?
Jijoy
The handling of property events will be as fast as anything you can come up with. Property events behave like any other Java object: as soon as no one is using them anymore, the garbage collection (GC) will get rid of them. Unlike files, you don't need to "close" them.
Aaron Digulla
be careful with the built in java.beans.PropertyChangeSupport - it uses .equals() to determine if an event should propagate. That's fine for immutable objects, but a disaster for values like lists that might be equal (i.e. both empty) at one point in time, but not equal later on.
Kevin Day
Worse, if you use things like Date and BigDecimal, equals() doesn't work at all.
Aaron Digulla
+1  A: 

The advantage is in the area of coupling. mediator requires that your classes be aware of the mediator (e.g. by passing the mediator instance into the constructor). Mediators can be very powerful, and can definitely provide more efficient notification - but at the cost of significant complexity.

In contrast, the Observer design pattern (which JavaBeans property change listeners are an implementation of) are much easier to implement. But they can have interesting emergent behavior in complex systems.

For many situations, Observer is more than adequate (especially in the areas that JavaBeans tend to be used). In other situations, it is nowhere near adequate - a good example is in event propagation in the Glazed Lists library. They wound up having to use an EventPublisher (which is a mediator) to optimize event notification order, and ensure that dependencies are met before events propagate.

So, the answer to your question is that this is not a matter of POJOs vs JavaBeans. It's a matter of which design pattern (Observer or Mediator) makes the most sense for a given use case. Both patterns focus on decoupling parts of the system from each other. And both patterns have situations where they are appropriate. For a huge number of situations, Observer is just fine - and is really, really easy to write.

I should also mention that the use of intermediate event objects is common in both Observer and Mediator implementations, so that's kind of a side issue. But modern VMs are really efficient at dealing with short lived objects like that - so it's really a non issue.

Kevin Day