Java callback function Introduction

Source: Internet
Author: User
Case 1 The following uses the Java callback function to implement a tool class for testing the function running time: if we want to test the execution time of a class method, we usually do this:
  1. PublicClassTestobject {
  2. /**
  3. * A method used for testing carries out a time-consuming cycle.
  4. */
  5. PublicStaticVoidTestmethod (){
  6. For(IntI =0; I <100000000; I ++ ){
  7. }
  8. }
  9. /**
  10. * A Simple Method for Testing the execution time
  11. */
  12. PublicVoidTesttime (){
  13. LongBegin = system. currenttimemillis ();// Test start time
  14. Testmethod (); // Test Method
  15. LongEnd = system. currenttimemillis ();// Test End Time
  16. System. Out. println ("[Use time]:"+ (End-begin ));// Print the usage time
  17. }
  18. PublicStaticVoidMain (string [] ARGs ){
  19. Testobject test =NewTestobject ();
  20. Test. testtime ();
  21. }
  22. }

As you can see, only the "// test method" of the testtime () method needs to be changed. Let's create a function to implement the same function but be more flexible:

First, define a callback interface:

    1. PublicInterfaceCallback {
    2. // Method for executing the callback operation
    3. VoidExecute ();
    4. }

Then write a tool class:

  1. PublicClassTools {
  2. /**
  3. * Test the function usage time by defining the execute method of the callback Interface
  4. * @ Param callback
  5. */
  6. PublicVoidTesttime (callback ){
  7. LongBegin = system. currenttimemillis ();// Test start time
  8. Callback.exe cute (); /// Perform callback
  9. LongEnd = system. currenttimemillis ();// Test End Time
  10. System. Out. println ("[Use time]:"+ (End-begin ));// Print the usage time
  11. }
  12. PublicStaticVoidMain (string [] ARGs ){
  13. Tools tool =NewTools ();
  14. Tool. testtime (NewCallback (){
  15. // Define the execute Method
  16. PublicVoidExecute (){
  17. // Add one or more methods to test the running time.
  18. Testobject. testmethod ();
  19. }
  20. });
  21. }
  22. }

As you can see, the callback function can be implemented by passing testtime () to the execute () method that defines the callback interface.

 

Case 2

 ProgramMember a writes a program (program A), which reserves the callback function interface and encapsulates the program. Programmer B wants a to call a method in program B, So he calls back the method in B through the interface in. .In C/C ++, a callback function is used. If a function is dropped, the caller must be notified of its own pointer address, but there is no pointer in Java. What should I do? We can use interfaces to define callback functions.
Suppose I am a programmer, and the following is my program:

 

[Java] View plaincopyprint?

  1. Public ClassCaller
  2. {
  3. PublicMycallinterface MC;
  4. Public VoidSetcallfuc (mycallinterface MC)
  5. {
  6. This. MC = MC;
  7. }
  8. Public VoidCall (){
  9. This. MC. Method ();
  10. }
  11. }

 

 

I also need to define an interface so that programmer B can write the program implementation interface according to my definition.

[Java] View plaincopyprint?

    1. Public InterfaceMycallinterface
    2. {
    3. Public VoidMethod ();
    4. }



 

As a result, programmer B only needs to implement this interface to achieve the purpose of callback:

[Java] View plaincopyprint?

    1. Public ClassBImplementsMycallinterface
    2. {
    3. Public VoidMethod ()
    4. {
    5. System. Out. println ("Callback");
    6. }
    7. Public Static VoidMain (string ARGs [])
    8. {
    9. Caller call =NewCaller ();
    10. Call. setcallfuc (NewB ());
    11. Call. Call ();
    12. }
    13. }

 

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.