I have a java class which fires custom java events. The structure of the code is the following:
public class AEvent extends EventObject {
...
}
public interface AListener extends EventListener {
public void event1(AEvent event);
}
public class A {
public synchronized void addAListener(AListener l) {
..
}
public synchronized void removeAListener(AListener l) {
..
}
protected void fireAListenerEvent1(AEvent event) {
..
}
}
Everything works correctly, but I'd like to create a new subclass of A (call it B), which may fire a new event. I'm thinking of the following modification:
public class BEvent extends AEvent {
...
}
public interface BListener extends AListener {
public void event2(BEvent event);
}
public class B extends A {
public synchronized void addBListener(BListener l) {
..
}
public synchronized void removeBListener(BListener l) {
..
}
protected void fireBListenerEvent2(AEvent event) {
..
}
}
Is this the correct approach? I was searching the web for examples, but couldn't find any.
There are a few things I don't like in this solution:
BListener
has two methods one usesAEvent
the other usesBEvent
as a parameter.B
class both hasaddAListener
andaddBListener
methods. Should I hide addAListener with private keyword? [UPDATE: it's not possible to hide with private keyword]- Similar problem with
fireAListenerEvent1
andfireBListenerEvent1
methods.
I'm using Java version 1.5.