With the rapid development of Java, the JDK version is constantly updated, and the new event model of Java is essentially different from the old JDK model, its event model is also very different. Because the current programming is event-driven, it is necessary to understand the event model. We will discuss it in detail from the actual situation of custom events.
For example, if you want to implement a Timer class, its main function is to start a specific event processing after a certain period of time.
Therefore, we can design two classes: the Timer class (timer) and the call class (clock). The sequence diagram of the corresponding use case is as follows:
There are two instructions: 1) design the main class based on the complexity of the problem. When the problem is small, you can specify the "call class" as the main class; 2) the callback function is used in this sequence diagram. The implementation of this function in C ++ is through pointers, but there is no pointer concept in Java. We use interfaces here. Here we use a general interface design style [1] service interface style.
In order to make the design of the class scalable and reusable, the interface is added on the basis of the class. For convenience, we only consider the interaction of two classes. Because interfaces only contain methods and do not contain attributes, they cannot reference other objects. Therefore, there are only three common interface modes:
| Object Adapter |
Object Manager |
Launch Event Mode on the Service Interface |
|
|
|
In this application, we use the preceding service interface mode to process events. Because the event has two participants: the supplier and user of the event, in the specific application of event processing, it can be subdivided into "Launch Event Mode" and "pull into event mode ".
Reconciliation Interface
We know a principle of programming: separating what remains unchanged from what changes occur [2]. Here, we use interfaces to hold variable-prone Code (for example, some events are triggered after the time set by the timer, which should be set by the user and can be changed ). In Java, the interface can implement the callback function. Therefore, we use callback technology to encapsulate the changed code in the interface, while the code that remains unchanged is the code that "Callback" changes. Objects have different expressions, but can accept the same parameters.
Implementation
Based on the above service interface mode and callback technology, we have established the following framework:
The timerlistner Interface contains the processevent () method. In this method, place the code of the action we need when an event occurs. The following is the sample code of the program:
Interface timerlistener {
Public void processevent ();
}
Public class clock implements timerlistener {
Clock (){
Timer T = new timer (this); // register with the Timer class
}
Public void processevent (){
// Your event processing code
}
}
Class timer extends thread {
Private timerlistener TL;
Timer (timerlistener TL ){
This. TL = TL;
}
Public void run (){
While (true ){
Sleep (1000 );
TL. processevent ();
}
}
}
References
- UML object design and programming
- Java programming ideas
- Java2 Core Technology
- Design Patterns
About the author
|
|
|
Tang Xianfeng has authored this article |