I think the event hierarchy is a bit too much on the Everything Is A Class bandwagon. It could better be implemented like MS.
Any opinions?
.
Printable View
I think the event hierarchy is a bit too much on the Everything Is A Class bandwagon. It could better be implemented like MS.
Any opinions?
.
Everything's class-happy in Java. :)
Not quite sure what you mean. Are you talking about the
java.awt.event classes? The adapter classes, listeners, and the event classes?
Exactly.
If I create a class in VB and want to associate some events with it, I just need to create a public event procedure and I am done.
If I want to trap an object's events in my code, I need to write the procedure declared as public event handler inside the object in my code.
But look at Java. You need to create a class that implements a listener, then you have to override all the methods inside that listener, whether or not you need those events, and finally register it with the class which will receive the events. SO much work!
Mind ya, I am trying to get something out in favour of Java by criticizing it. Just my way of knowing things better.
.
It really matters what kind of event you are trying to trap.
Low level events or semantic events "high level". I noramlly dont use a listener for low level events because of the need to provide multiple method implemations. It just doesnt make sense unless you are trapping multiple events. I do use listeners for high level events like ActionListener,AdjustmentListener,ItemListener, TextListener because there is only one method defined for each listener. For low level events i use adapter classes this way i just have to override the nessary method needed.
For instance...... say if you want test for window closing events on a JFrame. You can either implement a WindowListener interface or extend "subclass" the WindowAdapter class. If you
implemented the WindowListener interface then you would have to provided a method signature in your class for seven methods
which would be a pain in the A@#. :)
I look at it this way:
In VB when I have to trap a particular event for an object, all I do is write a public sub (sort of overriding) for that event as specified by the object.
In Java, even if I want to trap the existing events, I have to go create a listener, register it and then override all its methods, as you pointed out.
Couldn't Java implement events the way MS did/does? And if not, why?
.
If you extend or "subclass" an adapter class then
all you have to do is override the necessary method.
For high level events it really doesnt make a diffrence if you implement a listener or subclass an adapter class because
of the small amount of events that you can trap. Java is true
OOP so obviously the creation of a subprocedure wouldnt make sense in terms of trying to mimic Visual Basic. I dont know i dont
see it as being much work to trap events in Java.
Yes, the OOP has been very strictly (I would say a little too strictly) implemented in Java.
It's both an advantage and a disadvantage. From VB point of view, it's just a bit complicated to get used to, while from an OOP point of view, it's just the logical way to go.
.