Java callback Java implements callback.

Source: Internet
Author: User
ArticleDirectory
    • 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.
    • Of course, you can also register multiple objects of interest to this event in the event management or event notification class.
JAVA Implementation callback

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.

Notification implementation is required when an event occurs.InterestingeventInterface object, and callInterestingevent ()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 exampleSomethinghappenedTo indicate whether an event occurs.

Classes that want to receive Event Notifications must be implementedInterestingeventAnd pass your reference to the Event Notification.
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 , Callback Method Nterestingevent (string event) Simply receive String Parameters.

Interface interestingevent {
Public void interestingevent (string event );
}

 

2. Implement the interestingevent interface and event processing class

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 ){
Callmes. Add (callme );
}

Public void dowork (){
For (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 ();
}
}

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.