Thinking in Java API callback Revision

Source: Internet
Author: User

Callback Concept


What is "Callback, callback?

In the previous study, we learned that in the event-driven design mode, callback is to pass a method pointer to the event source.

This method is called when an event occurs. This process is called callback, And the called method is called callback method.

But what we know is that java object-oriented programming does not support method pointers and it seems difficult to implement them? But think about it.

You can actually know that java can use the interface technology to implement the same callback.


Interface callback
The Implementation Technique of interface callback in java is to define a simple interface and declare the method we need to call in the interface. When

When an event occurs, we call back through the method that implements this interface. This method of the interface is the callback method.

It is hard to understand. Let's see how to implement it!

Simple implementation of interface callback
In fact, in the previous article about "anonymous internal classes", java interface callback has been used.

First, write an interface and declare some callback methods.

[Java]
Package com. kiritor;
/** @ Author Kiritor
* Event interface */
Interface EventListener {
Void clicked ();
Void pressed ();
/* Others will not be written */
}

Package com. kiritor;
/** @ Author Kiritor
* Event interface */
Interface EventListener {
Void clicked ();
Void pressed ();
/* Others will not be written */
}
Sometimes there are too many interface methods, so it is inconvenient to use the internal class to implement the call.
Adapter ".


[Java]
Package com. kiritor;
 
/**
* @ Author the adapter for event listening
*/
Public class EventAdapter implements EventListener {
@ Override
Public void clicked (){
// TODO Auto-generated method stub
 
}
 
Public void pressed (){
};
 
}

Package com. kiritor;

/**
* @ Author the adapter for event listening
*/
Public class EventAdapter implements EventListener {
@ Override
Public void clicked (){
// TODO Auto-generated method stub

}

Public void pressed (){
};

} Compile a button class, which can be used to add a listener, that is, to listen to changes in the button status.

And perform related actions!


[Java]
Package com. kiritor;
 
/**
* @ Author Kiritor: a button
*/
Public class MyButton {
Private int flag = 0; // 0 indicates click, and 1 indicates Press
 
Public MyButton (int flag ){
This. flag = flag;
}
// Add a listener to the button
Public void addListener (EventListener eventListener ){
If (flag = 0 ){
EventListener. clicked ();
} Else
EventListener. pressed ();
}
 
}

Package com. kiritor;

/**
* @ Author Kiritor: a button
*/
Public class MyButton {
Private int flag = 0; // 0 indicates click, and 1 indicates Press

Public MyButton (int flag ){
This. flag = flag;
}
// Add a listener to the button
Public void addListener (EventListener eventListener ){
If (flag = 0 ){
EventListener. clicked ();
} Else
EventListener. pressed ();
}

} Take A Look At The test class:
[Java]
Package com. kiritor;
 
Public class Test {
Public static void main (String [] args ){
MyButton button = new MyButton (1 );
// Add a listener to the button in the Adapter Mode
Button. addListener (new EventAdapter (){
@ Override
Public void clicked (){
// TODO Auto-generated method stub
System. out. println ("the button is clicked ");
}
 
@ Override
Public void pressed (){
// TODO Auto-generated method stub
System. out. println ("the button is pressed ");
}
});
 
}
}

Package com. kiritor;

Public class Test {
Public static void main (String [] args ){
MyButton button = new MyButton (1 );
// Add a listener to the button in the Adapter Mode
Button. addListener (new EventAdapter (){
@ Override
Public void clicked (){
// TODO Auto-generated method stub
System. out. println ("the button is clicked ");
}

@ Override
Public void pressed (){
// TODO Auto-generated method stub
System. out. println ("the button is pressed ");
}
});

}
}
The above code basically implements the same features of awt in java, but it means that the button status listening is manual, because
Button status changes are only known at the underlying layer.

Read the code carefully to see how java calls back through interfaces!

First, let's take a look at the EventListener interface. Needless to say, it must define the method for callback.

The second is EventAdapter, which implements EventListener, which is not too demanding here. Readers can also

Do not implement it. (In fact, this is a design model, so I will not talk about it much)

Next, click "MyButton". The main method is "method.


[Java]
// Add a listener to the button
Ublic void addListener (EventListener eventListener ){
If (flag = 0 ){
EventListener. clicked ();
} Else
EventListener. pressed ();
}

// Add a listener to the button
Public void addListener (EventListener eventListener ){
If (flag = 0 ){
EventListener. clicked ();
} Else
EventListener. pressed ();
}
Here we will take a look at the test code:


[Java]
Button. addListener (new EventAdapter (){
@ Override
Public void clicked (){
// TODO Auto-generated method stub
System. out. println ("the button is clicked ");
}
 
@ Override
Public void pressed (){
// TODO Auto-generated method stub
System. out. println ("the button is pressed ");
}
);

 
Button. addListener (new EventAdapter (){
@ Override
Public void clicked (){
// TODO Auto-generated method stub
System. out. println ("the button is clicked ");
}

@ Override
Public void pressed (){
// TODO Auto-generated method stub
System. out. println ("the button is pressed ");
}
});
A new button and add a listener to it to form an anonymous internal class object and pass it to the EventListener interface.

It calls back the corresponding interface methods. The anonymous internal class can have the method required for implementation. Why?

The reason why the EventListener interface is not used directly. We know that java is dynamically bound, and the method called at last is also

Is the re-implementation method in the anonymous internal class.

Therefore, the output result is: the button is pressed.

Re-understand callback
By creating an anonymous class, the callback method of the interface is dynamically implemented, focusing on the interface.

EventAdapter, but we can see from the source code that we haven't done much for it. It's just a "helper"

Means! What we really care about is the re-implementation method, which provides an external implementation and is called back by the interface.

In general, it is: provide an interface method to the Coder implementation, but after the coder implementation, the interface

. Coder cannot be called directly.

 

 

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.