Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Property Change listeners

Tags:

java

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

like image 967
Jijoy Avatar asked Sep 14 '26 02:09

Jijoy


1 Answers

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.

like image 73
Kevin Day Avatar answered Sep 15 '26 14:09

Kevin Day



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!