JDK-4909261 : RFE: built-in support for listener registration and event firing
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-08-20
  • Updated: 2006-11-15
  • Resolved: 2006-11-14
Related Reports
Relates :  
Relates :  
Relates :  
Description
Name: jl125535			Date: 08/19/2003


A DESCRIPTION OF THE REQUEST :
I propose a mechanism which has the benefit both to distinguish event registering and raising from other methods and to remove from the developer the need to rewrite anytime the broadcast loop.

Suppose we have an EventListener interface

------------------
import java.awt.event.ActionListener
...
------------------


I propose that the declaration of the event may be done
through a new keyword, "broadcast".

--------------------------------
import java.awt.event.ActionListener

//a class that raises action events
class MySource
{
	public broadcast ActionListener actionHandler;
}
------------------

The MySource developer would not intantiate the any ActionListener. The
keyword broadcast tells the compiler (or the jvm) that actionHandler is a bag
of ActionListener. The MySource developer would not write any code to register
listeners nor any loop to dispatch the event. The broadcast keyword may be used
only with interfaces that extends the EventListener interface.

A client would register through the following mechanism:

---------------------------
import java.awt.event.ActionListener

//a class that raises action events
class MyListener implements ActionListener
{
	MySource source = new MySource ();
	MyListener ()
	{
		source.actionHandler += this;
	}
}
-----------------------------

The MySource developer would trigger the event through the following mechanism
(no loop, simply use of the listener interface):

--------------------------------
import java.awt.event.ActionListener
import java.awt.event.ActionEvent

//a class that raises action events
class MySource
{
	public broadcast ActionListener actionHandler;

	void aMethod ()
	{
		actionHandler.actionPerformed (new ActionEvent (this));
	}
	
}
------------------


Conclusion. This mechanism would lay aside event declaration and registration
from normal methods, making the code more clear. It would also simplify
greatly event declaration on a class (which now involves to implement addXXX removeXXX fireXXX
method for any event we want to raise).



JUSTIFICATION :
Two benefits -
1) Distinguish event registering and raising from other methods
2) Remove the need to rewrite anytime the broadcast loop.

Javadoc documentation would present distinct event documentation
from fields and methods
(Incident Review ID: 182979) 
======================================================================

Comments
EVALUATION The 'source.actionHandler += this;' operation is essentially a request for operator overloading on ActionListener. As for the 'broadcast' keyword saving a loop, I am not convinced this justifies a new keyword.
14-11-2006