On the implementation of event driven mechanism with Java

Due to project requirements, it is necessary to provide java with a set of class libraries supporting event driven mechanism, which can implement event and delegate mechanisms similar to those in C #. As we all know, the Java language itself and its standard library do not provide relevant interfaces for event driven mechanism, Although swing (I don't think it belongs to the standard library, because it is generally not used:) there are relevant classes in support of this mechanism to realize the event processing of components, but it is coupled with GUI after all, and it seems awkward and lack of generality in other types of applications. Therefore, it is necessary to implement a set of general Java event driven mechanism class library and apply it to Common Java applications, although this is not difficult:)

Let's take a look at c#'s event driven mechanism writing method. The event keyword provided in C # can be easily used to define an event. Then, by adding an event handling function to the event (generally referring to a function by delegate in C #), the trigger event can call the relevant handling function, that is, the event driven process. For example:

The above code is a simple example of the event driven mechanism implemented by c# and it can be seen that it is very simple, which stems from the convenience provided by c# at the language level (actually CLR). Unfortunately, Java does not provide such convenience and needs to be implemented manually. The following article will provide two methods to implement the event driven mechanism for reference only.

Observer mode

Observer mode is a common design mode. The observer subscribes to the subject first, so that once the subject changes, the observer will be notified of the change.

This design pattern can just be used in the event driven mechanism. An event is equivalent to a subject. Once an event is triggered, the event handler function will be called. It can be seen that the event handler function (the delegate in c#) can be regarded as an observer. Therefore, the above functions can be implemented as follows.

The above java code implements a simple event driven mechanism. The principle is very simple. It is a typical application case of observer mode.

Using reflection

The Java language provides a powerful reflection function. You can obtain and operate various components of a class (such as class name, class member function, class attribute, etc.) at runtime. The following uses reflection to implement a simple event driven mechanism.

The above code shows the event driven mechanism implemented by reflection. The advantage of using reflection mechanism is that it has strong scalability. For example, a formal parameter of EventArgs can be introduced into my event handling function, so that the event itself can have parameters, so that the event can carry more information. The rewritten event handling function is shown in the following code:

Is it deja vu? Yes, the event handling function (onclick function in the code) automatically generated by visual studio for you has almost the same form as when using c# to write WinForm forms, but at this time, we implement it in Java.

Of course, in addition to the two methods mentioned above that can realize the event driven mechanism of Java, there are other methods. For example, they can be realized by using the internal classes of Java. The author has also written some example code. There is no verbosity here, which will be discussed later.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>