16) JAVA Implementation callback (implementation of various listener types in Android and Swing) and androidlistener
Developers familiar with the MS-Windows and X Windows event-driven design modes usually pass a method pointer to the event source, this method (also called "Callback") is called when an event occurs "). Java's object-oriented model currently does not support method pointers, and it seems that this convenient mechanism cannot be used.
Java supports interfaces, and the same callback can be implemented through interfaces. The trick is to define a simple interface and declare a method to be called back.
For example, if an event is notified, we can define an interface:
Public interface InterestingEvent {
// This is just a common method that can receive parameters or return values.
Public void interestingEvent ();
}
In this way, we have any controller grip that implements this interface class object.
When an event occurs, you need to notify the object implementing the InterestingEvent interface and call the interestingEvent () method.
Class EventNotifier {
Private InterestingEvent ie;
Private boolean somethingHappened;
Public
EventNotifier(InterestingEvent event ){
Ie = event;
SomethingHappened = false;
}
Public void
DoWork(){
If (somethingHappened ){
// When an event occurs, this method is called to notify
Ie. interestingEvent ();
}
}
}
In this example, somethingHappened is used to indicate whether an event has occurred.
To receive Event Notifications, you must implement the InterestingEvent interface and pass your reference to the Event Notification owner.
Public class CallMe implements InterestingEvent {
Private EventNotifier en;
Public CallMe (){
// Create an Event Notification object and pass it to it
En = new EventNotifier (this );
}
// Method for actually handling an event when an event occurs
Public void interestingEvent (){
// Handle this event
}
}
The above is a very simple example to illustrate the implementation of callback in Java.
Of course, you can also register multiple objects of interest to this event in the event management or event notification class. 1. Define an interface InterestingEvent. The callback method nterestingEvent (String event) simply receives a String parameter. Interface InterestingEvent {public void interestingEvent (String event );
}
2. Implement the InterestingEvent interface. The event processing class CallMe implements InterestingEvent {
Private String name;
Public CallMe (String name ){
This. name = name;
}
Public void interestingEvent (String event ){
System. out. println (name + ": [" + event + "] happened ");
}
}
3. event manager or event notification recipient
Class EventNotifier {
Private List <CallMe> callMes = new ArrayList <CallMe> ();
Public void regist (CallMe callMe ){
CallMes. add (callMe );
}
Public void doWork (){
For (CallMe callMe: callMes ){
CallMe. interestingEvent ("sample event ");
}
}
}
4. Test
Public class CallMeTest {
Public static void main (String [] args ){
EventNotifier ren = new EventNotifier ();
CallMe a = new CallMe ("CallMe ");
CallMe B = new CallMe ("CallMe B ");
// Regiest
Ren. regist ();
Ren. regist (B );
// Test
Ren. doWork ();
}
}