Thinking in Java interface callback

Source: Internet
Author: User
Sorry, the typographical problem has been rewritten now:

Address:Http://blog.csdn.net/kiritor/article/details/8755535

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 the Java object-orientedProgramThe design does not support method pointers and does it seem 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.

 
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 ".

 
Package COM. kiritor;/*** @ author kiritor event listening adapter */public class eventadapter implements eventlistener {@ override public void clicked () {// todo auto-generated method stub} public void pressed (){};}

 

Compile a button class, which has a method to add a listener, that is, to listen for changes in the button status

And perform related actions!

Package COM. kiritor;/*** @ author kiritor a button */public class mybutton {private int flag = 0; // 0 indicates clicking, 1 indicates pressing public mybutton (INT flag) {This. flag = flag;} // Add listener public void addlistener (eventlistener) {If (flag = 0) {eventlistener. clicked ();} else eventlistener. pressed ();}}

Take a look at the test class:

Package COM. kiritor; public class test {public static void main (string [] ARGs) {mybutton button = new mybutton (1); // Add a listener to the button, the adapter mode button is used. addlistener (New eventadapter () {@ overridepublic void clicked () {// todo auto-generated method stubsystem. out. println ("the button is clicked");} @ overridepublic void pressed () {// todo auto-generated method stubsystem. out. println ("the button is pressed ");}});}}

AboveCodeBasically, the same functions of AWT are implemented in Java, but the button status listening is manual, because

 

The underlying layer only knows when the button status changes.

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 ".

 
// Add listener public void addlistener (eventlistener) {If (flag = 0) {eventlistener. clicked ();} else eventlistener. Pressed ();}
 
 

 

 
 

 

Here we will look at the following in combination with the test class:

 
Button. addlistener (New eventadapter () {@ overridepublic void clicked () {// todo auto-generated method stubsystem. out. println ("the button is clicked");} @ overridepublic void pressed () {// todo auto-generated method stubsystem. 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.

 

By itsCallbackThe corresponding interface method. 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 the source code of the reader can be known. We didn't do much for it. It's just a "helper".

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

In general, I provide you with 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.