Java: what are the event notification options in my own API?

Suppose you have written an API that can collect data in real time in a separate thread Data access is thread - safe and through the interface For example GetData () does not block and provides the latest information The API is used in many applications; Some GUI drivers, other console programs

What options do you have to notify your application of data updates? We absolutely don't want the API to be application dependent, so we don't want firetabledatachanged () or similar APIs In our GUI application, we will call fireTableDataChanged () and so on after API notifications us or we look at API.

Thank you.

Solution

One option is to write your own event generator class

>Ieventlistener < activity > > ieventproducer < listener extends ieventlistener < event >, event > > eventproducer < listener extends ieventlistener < event >, event > implements ieventproducer < listener, event >

Ieventlistener is implemented by the observer It has a way to receive events

Ieventproducer is implemented by any observable eventproducer It has methods to add and remove listeners

Eventproducer is thread safe It has a method to send events to the listener's copyonwritearraylist It captures and logs any exceptions thrown by the listener It also provides methods to add and remove listeners

Then, any observable class can implement ieventproducer through the internal eventproducer

If you try to support two listener types on the same class, typing erasure will require you to name different add / remove listener methods for different listeners

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>