Talking about the Event Processing Mechanism of Java and C #

xiaoxiao2021-03-06  51

Karai 2002-11-20 10:36:40

-------------------------------------------------- ------------------------------

Java and C # event handlers are implemented in event source-event responder mechanisms, but not exactly the same. Java implementation is an event source and event responder two-level entity object. The event respondent here is also an event listener, and C # implements an event source-agent-event response person three-level entity object. Below will be specifically described in these two ways.

Java event handling

Conceptually, an event is a transmission mechanism that changes between "source objects" and "listener objects". There are many different purposes, such as mouse events, window boundaries, etc., and keyboard events, etc., for example, in Windows systems. In Java, it is defined a general, expandable event mechanism, which can:

• Provide a public framework for the definition and expansion of the model type and the model of the passage, and is suitable for extensive applications.

· There is a high integration with Java language and the environment.

• The event can be described as an environment capture and ignition.

· It can make other constructors to take some techniques to direct the connection between events, as well as between event source and event listeners.

· The event mechanism itself does not rely on complex development tools.

The event from the event source to the listener is conducted by the Java method call to the target listener object. A clear Java method is defined accordingly for each of the clear events. These methods are centralized in the event listener (EventListener interface, this interface should inherit Java.util.EventListener. A class that implements some or all of the methods in an event listener interface is an event listener. With the occurrence of the event, the corresponding state is usually packaged in the event status object, which must inherit from Java.util.EventObject. Event status objects are transmitted to the listener method that should respond to the event as a single ginseng. The identity of the event source that issues a certain particular event is that the design format of the specified design is the registration method for event listeners and accepts references to the specified event listener interface instance. Sometimes, event listeners cannot directly implement event listener interface, or other additionalctions, it is necessary to insert an instance of an event adapter class between a source and other one or more listeners to establish them. Contact.

Event status object (Event State Object)

Status information related to the event is generally packaged in an event status object, which is a subclass of java.util.EventObject. According to the design habits, the names of this event status object class should be ended at Event. E.g:

Public class mousemovededexampleEvent extends java.util.eventObject

{protected int x, y;

/ * Create a mouse mobile event MousemoveDexampleEvent * /

MouseMoveDexampleEvent (java.awt.component source, point location) {

Super (Source);

x = location.x;

y = location.y;

}

/ * Get mouse position * /

Public Point getLocation () {

Return New Point (x, y);

}

Event listener interface and event listener

Since the Java event model is based on method calls, a way to define and organize event handling methods. Event manipulation methods are defined in the EventListener interface inherited by the Java.util.EventListener class, and the naming of the EventListener interface is ending with Listener. Any class If you want to manipulate the method defined in the EventListener interface, you must perform this interface. This class is the event monitors. For example: / * Define a mouse mobile event object * /

Public class mousemovededexampleEvent extends java.util.eventObject {

/ / In this class, the state information related to the mouse movement event is included.

...

}

/ * Define the listener interface of the mouse mobile event * /

Interface mousemovededexampleListener extends java.util.EventListener {

/ * The method should support the mouse mobile event listener in this interface * /

Void MouseMoved (MousemoveDexampleEvent MME);

}

Only the method name, the parameters of the method, and the return value type are defined in the interface. Such as: The specific implementation of the mousemoved method in the above interface is defined in the ArbitraryObject class below.

Class ArbitraryObject Implements MousemovedexampleListener {

Public void mousemoved (MousemoveDexampleEvent MME)

{...}

}

ArbitraryObject is the listener of the MousemoveDexampleEvent event.

Registration and logout of event listeners

For various possible event listeners, the event streams between the source and event monitors are established in the appropriate event source, and the event source must provide registration and logout for event listeners. This use process has been seen in the previous Bound property introduction, in actual, event listener's registration and logout to use standard design format:

Public void add ( listener);

Public Void Remove ( listener);

For example, an event listener interface is defined first:

Public interface modelchangedlistener extends java.util.eventlistener {

Void ModelChanged (EventObject E);

}

Then define the event source class:

Public Abstract Class Model {

Private vector listener = new vector ();

/ / Define an array of storage event listeners

/ *

Public Synchronized Void AddmodelchangeDlistener

(ModelChangeDlistener MCL)

{Listener.AddeElement (MCL);} // Register the listener into the listeners array

Public Synchronized Void RemoveModelchangedListener

(ModelChangeDlistener MCL)

{listeners.removeEleMent (MCL); // Logger listing the listener from listeners

} The front of the above two methods is crown in synchronized, because when running in a multi-threaded environment, there may be several objects at the same time to register and log out, using Synchronized to ensure that they are synchronized. Developing tools or programmers use these two ways to establish event streams between source and listeners. protected void notifymodelchanged () {

/ * Event source uses this method to notify the listener's modelchanged event * /

Vector L;

EventObject E = New EventObject (THIS);

First, copy the listener to the L array, freeze the status of EventListeners to pass the event. This way to ensure that the corresponding method of the target listener that has received the event is not effective before the event is passed to all listeners.

Synchronized (this) {

l = (vector) listener.clone ();

}

For (int i = 0; i

/ * Introduction to the registration in the listener queue, the modelchanged event,

And transmit the event status object E as a parameter to each listener in the listener queue * /

(ModelchangeDlistener) L.Elementat (i)). ModelChanged (e);

}

}

}

In the program, the event source model class explicitly calls the modelchanged method in the interface. It is actually transmitting the event status object E as a parameter to the MODELCHANGED method in the listener class.

Adaptation class

Adaptation class is an extremely important part of the Java event model. In some applications, events are "forwarded" from the source to the transmission. For example: When an event source is issued, there are several event listener objects to receive the event, but only when the specified object reacts, an event adapter class is to be inserted between event sources and event listeners. The adapter class to specify which listeners should be responded. The adaptation class has become an event listener. The event source actually uses the adaptation class as a listener into the listener queue, and the real event responder is not in the listener queue, the event responder should do by appropriate Classification decision. At present, most developments are generated when generating code, and event processing is made by adaptation classes.

C # event processing

In the .NET application development, whether it is Web Forms (ASP.NET) or Windows Forms, all involves the event response and processing of a large number of objects, such as the customer 's online submission, or move the mouse on the Windows window. There will be an event. So, how do you declare an event and add a response method for an event?

In C #, events (events) are used to declare a class event. Declare an event member in a class generally adopted the following grammatical form:

Public Event represents the name of the event name.

If a Click event member is declared in the Control class, its syntax is as follows:

Public Event even Click;

In C #, a new data type Delegate is added to resolve the event handling problem. The representative data type is very similar to a pointer in the C language, which is different from the pointer, which is a secure, manageable. Due to the simplicity of C # itself, it is also very easy to understand Delegate for programs that have not been used by C and pointers.

In C #, by using Delegate, you can add one or even multiple response methods to an event in the .NET object via " =" (add equal) operator; can also be very simple "- =" (Equivalence equivalent) operator cancels these response methods. Add the statement of the Click event as the Temp button: Temp.click = new system.eventhandler (this.test); / / Add event processing method for TEST

In the statement of the event above, EventHandler is a Delegate type, which is declared in the .NET class library:

Public Delegate Void EventHandler (Object Sender, Eventargs E);

In this way, all words such as: Void correspondence (Object parameter name, Eventargs parameter name); functions can be used as a Click event response method for the Control class. An event response method as defined below:

Private void Button1_Click (Object Sender, System.Eventargs E)

Since the event is processed by Delegate, it is possible to have multiple response methods by accumulating an event; at the same time, a method can also be a response method of multiple events. (Note: There is only " =" and "- =" in the C # language class, " =" and "- =" can appear to add an operator to the cancel event response function.)

Whether it is ASP.NET or average Windows Forms programming, in C #, basically we encounter the event response method to explain the following form:

Private void Button1_Click (Object Sender, System.Eventargs E)

Then, if an event response method, whether the return value type, parameter and type or even method name, etc. must be fixed? The answer is: no!

Under normal circumstances, there are two parameters in the response method of the event, one of which represents the object of the incident, the Sender, which is unpredictable, so we declare it as an object type, all objects apply. The second parameter represents the specific information of the incident, which may be different in various types of events, which is determined according to the instructions for event members.

We know that the event is processed by delegate. Assuming that the representative of the event will be indicated by the following form:

Delegate Int MyEventHandler (Object Sender, ToolbarButtonclicKeventargs E);

Then, when the above event response functions are declared, they must be declared as the following form:

Private Int MyTest (Object Sender, ToolbarButtonclicKeventargs E) {}

Implement the following code when adding an event response method to an object:

Control.event = new mytesthandler (mytest);

In general, the Java event handles more direct and simple. And C # event handles due to reference agents, make the program more flexible, more reflecting the looseness between the program. US Shenn Company announced on the Java development platform Realize Microsoft .NET, named inet. In recent launch of the Inet's beta3 version, including the three-level event handling mechanism for C # with Java.

(Author mailbox: coffeemilklee@sohu.com)

转载请注明原文地址:https://www.9cbs.com/read-40723.html

New Post(0)