I'm looking for a way to send an event carrying an object in its "payload". My goal is to send this event somewhere in an object of class Sender, and have class Receiver listen for these events, react when it detects one, and do something with the object carried by the event.
At the moment, I'm doing this in a way that I believe is not very elegant, but works fine. I'm using a PropertyChange event. This event is usually used to signal changes in a field of an object, if I have understood things correctly, and this is the reason I think my usage of it is not elegant. This is what I do.
In class Sender I have:
support.firePropertyChange("dummyName", null, objectToBeSent);
Where support
is a field of Sender that has been initialized as follows:
support = new PropertyChangeSupport(this);
So basically what I do is to signal the change of a dummy property that doesn't exist, and I signal that the old value of the property is null, and the new one is the reference to the object I want to send to the Receiver.
In class Receiver, which implements PropertyChangeListener
I have:
@Override
public void propertyChange(PropertyChangeEvent event) {
if ("dummyName".equals(event.getPropertyName())) {
// do something with event.getNewValue()...
}
}
Is there some more adequate Java class/utility that allows me to accomplish the same task in a more elegant way? Thank you in advance.