简体   繁体   中英

propertyChange Support vs EventListenerList differences and when to prefer each ones?

I have read the docs for PropertyChange support and EventListenerList . From my understanding, both serve similar purposes in holding a list of listeners and notifying them when event/propertyChange occurs. Is it only in case of GUI applications , that EventListenerList becomes handy?. For a simple JavaBean application that does not use GUI components , is there an advantage of using one over the other.

Generally speaking, a PropertyChangeEvent occurs when some property value of the object changes (a property/value which you can read), where a (general) event could describe any kind of event (such as a change in selection or a mouse click), it doesn't have to represent a change in the state of the object

PropertyChangeSupport is part of the bean framework (in particular, but not limited to) GUI editors. This doesn't mean you can't use it, in fact, many objects rely on this functionality, such as SwingWorker and many of the objects from SwingLabs for example.

With that in mind, you should use ProperyChangeSupport when you want to notify interested parties that a property/value of an object changes and EventListenerList when you want to provide general event notification for things that are occurring within the object (but don't have to be related to a specific property or state)

The only issue I have with ProptertyChanegSupport , is it can less obvious which properties are bound and which aren't, sometimes making it difficult to get started with new objects, where as it's reasonably easy to look up all the "addListener" methods, but that's just me

While I'm sure the original intention of the EventListenrerList was for GUIs, I've used them for non-GUI work before, but you might find it easier to to use a List if you only have support for a single listener though, just saying

由于PropertyChangeListener只是EventListener的特定“子类”(扩展接口)-这是一个标记接口,没有定义任何方法,因此与EventListenerList一起使用PropertyChangeSupport要容易得多-这是因为如果从EventListenerList开始,需要始终执行instanceof检查和强制转换以获取侦听器的实际“业务”方法(因为它们都实现的接口没有方法)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM