An analysis of the event handling mechanism of Java and C #

Source: Internet
Author: User
Tags event listener

Both Java and C # event handling are implemented by the event source-event responder mechanism, but not exactly the same. Java implementation is an event source and event responder level Two entity object way, the event responder here is also the event listener, and C # is a kind of event source-proxy-event responder Level three entity object way. Here are the two ways to specify.

Java Event handling

Conceptually, an event is a passing mechanism between the "source object" and the "listener object", in which a state has changed. Events can be used for many different purposes, such as mouse events that are often handled in Windows systems, window boundary changes, keyboard events, and so on. In Java, you define a common, extensible event mechanism that can:

The definition and expansion of event types and transitive models provides a common framework and is suitable for a wide range of applications.

Has a high degree of integration with the Java language and environment.

Events can be captured and triggered by the description environment.

Enables other construction tools to take some technology to control the event directly at design time, and the connection between the event source and the event listener.

The event mechanism itself does not depend on complex development tools.

An event is passed from the event source to the listener through a Java method call to the target listener object. A clear Java method is defined accordingly for each specific occurrence of the event. These methods are centrally defined in the event Listener (EventListener) interface, which inherits Java.util.EventListener. The class that implements some or all of the methods in the event listener interface is the event listener. Along with the occurrence of events, the corresponding states are usually encapsulated in the event state object, which must inherit from the Java.util.EventObject. The event state object is passed as a single parameter to the listener method that should respond to the event. An event source that emits a particular event is identified by following the specified design format, defining the registration method for the event listener, and accepting a reference to the specified event listener interface instance. Sometimes, an event listener cannot implement the event listener interface directly, or if there are other additional actions, it is necessary to establish a connection between one source and one or more listeners by inserting an instance of the event adapter class.

Event State Object

State information related to event occurrence is generally encapsulated in an event state object, which is java. Util EventObject subclass of the class. By design practice, the name of this event state object class should end with an event. For example:

public class MouseMovedExampleEvent extends java。util。EventObject
{ protected int x, y;
/* 创建一个鼠 标移动事件MouseMovedExampleEvent */
 MouseMovedExampleEvent (java.awt.Component source, Point location) {
super(source);
x = location.x;
y = location.y;
}
/* 获取鼠标位置*/
public Point getLocation() {
return new Point(x, y);
}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.