What is a callback function?

前端 未结 21 2439
耶瑟儿~
耶瑟儿~ 2020-11-21 23:01

What is a callback function?

相关标签:
21条回答
  • 2020-11-21 23:38

    A callback function is a function you pass (as a reference or a pointer) to a certain function or object. This function or object will call this function back any time later, possibly multiple times, for any kind of purpose :

    • notifying the end of a task
    • requesting comparison between two item (like in c qsort())
    • reporting progress of a process
    • notifying events
    • delegating the instanciation of an object
    • delegating the painting of an area

    ...

    So describing a callback as a function being called at the end of another function or task is overly simplifying (even if it's a common use case).

    0 讨论(0)
  • 2020-11-21 23:39

    “In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.” - Wikipedia

    Callback in C using Function Pointer

    In C, callback is implemented using Function Pointer. Function Pointer - as the name suggests, is a pointer to a function.

    For example, int (*ptrFunc) ();

    Here, ptrFunc is a pointer to a function that takes no arguments and returns an integer. DO NOT forget to put in the parenthesis, otherwise the compiler will assume that ptrFunc is a normal function name, which takes nothing and returns a pointer to an integer.

    Here is some code to demonstrate the function pointer.

    #include<stdio.h>
    int func(int, int);
    int main(void)
    {
        int result1,result2;
        /* declaring a pointer to a function which takes
           two int arguments and returns an integer as result */
        int (*ptrFunc)(int,int);
    
        /* assigning ptrFunc to func's address */                    
        ptrFunc=func;
    
        /* calling func() through explicit dereference */
        result1 = (*ptrFunc)(10,20);
    
        /* calling func() through implicit dereference */        
        result2 = ptrFunc(10,20);            
        printf("result1 = %d result2 = %d\n",result1,result2);
        return 0;
    }
    
    int func(int x, int y)
    {
        return x+y;
    }
    

    Now let us try to understand the concept of Callback in C using function pointer.

    The complete program has three files: callback.c, reg_callback.h and reg_callback.c.

    /* callback.c */
    #include<stdio.h>
    #include"reg_callback.h"
    
    /* callback function definition goes here */
    void my_callback(void)
    {
        printf("inside my_callback\n");
    }
    
    int main(void)
    {
        /* initialize function pointer to
        my_callback */
        callback ptr_my_callback=my_callback;                        
        printf("This is a program demonstrating function callback\n");
        /* register our callback function */
        register_callback(ptr_my_callback);                          
        printf("back inside main program\n");
        return 0;
    }
    
    /* reg_callback.h */
    typedef void (*callback)(void);
    void register_callback(callback ptr_reg_callback);
    
    
    /* reg_callback.c */
    #include<stdio.h>
    #include"reg_callback.h"
    
    /* registration goes here */
    void register_callback(callback ptr_reg_callback)
    {
        printf("inside register_callback\n");
        /* calling our callback function my_callback */
        (*ptr_reg_callback)();                               
    }
    

    If we run this program, the output will be

    This is a program demonstrating function callback inside register_callback inside my_callback back inside main program

    The higher layer function calls a lower layer function as a normal call and the callback mechanism allows the lower layer function to call the higher layer function through a pointer to a callback function.

    Callback in Java Using Interface

    Java does not have the concept of function pointer It implements Callback mechanism through its Interface mechanism Here instead of a function pointer, we declare an Interface having a method which will be called when the callee finishes its task

    Let me demonstrate it through an example:

    The Callback Interface

    public interface Callback
    {
        public void notify(Result result);
    }
    

    The Caller or the Higher Level Class

    public Class Caller implements Callback
    {
    Callee ce = new Callee(this); //pass self to the callee
    
    //Other functionality
    //Call the Asynctask
    ce.doAsynctask();
    
    public void notify(Result result){
    //Got the result after the callee has finished the task
    //Can do whatever i want with the result
    }
    }
    

    The Callee or the lower layer function

    public Class Callee {
    Callback cb;
    Callee(Callback cb){
    this.cb = cb;
    }
    
    doAsynctask(){
    //do the long running task
    //get the result
    cb.notify(result);//after the task is completed, notify the caller
    }
    }
    

    Callback Using EventListener pattern

    • List item

    This pattern is used to notify 0 to n numbers of Observers/Listeners that a particular task has finished

    • List item

    The difference between Callback mechanism and EventListener/Observer mechanism is that in callback, the callee notifies the single caller, whereas in Eventlisener/Observer, the callee can notify anyone who is interested in that event (the notification may go to some other parts of the application which has not triggered the task)

    Let me explain it through an example.

    The Event Interface

    public interface Events {
    
    public void clickEvent();
    public void longClickEvent();
    }
    

    Class Widget

    package com.som_itsolutions.training.java.exampleeventlistener;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Widget implements Events{
    
        ArrayList<OnClickEventListener> mClickEventListener = new ArrayList<OnClickEventListener>(); 
        ArrayList<OnLongClickEventListener> mLongClickEventListener = new ArrayList<OnLongClickEventListener>();
    
        @Override
        public void clickEvent() {
            // TODO Auto-generated method stub
            Iterator<OnClickEventListener> it = mClickEventListener.iterator();
                    while(it.hasNext()){
                        OnClickEventListener li = it.next();
                        li.onClick(this);
                    }   
        }
        @Override
        public void longClickEvent() {
            // TODO Auto-generated method stub
            Iterator<OnLongClickEventListener> it = mLongClickEventListener.iterator();
            while(it.hasNext()){
                OnLongClickEventListener li = it.next();
                li.onLongClick(this);
            }
    
        }
    
        public interface OnClickEventListener
        {
            public void onClick (Widget source);
        }
    
        public interface OnLongClickEventListener
        {
            public void onLongClick (Widget source);
        }
    
        public void setOnClickEventListner(OnClickEventListener li){
            mClickEventListener.add(li);
        }
        public void setOnLongClickEventListner(OnLongClickEventListener li){
            mLongClickEventListener.add(li);
        }
    }
    

    Class Button

    public class Button extends Widget{
    private String mButtonText;
    public Button (){
    } 
    public String getButtonText() {
    return mButtonText;
    }
    public void setButtonText(String buttonText) {
    this.mButtonText = buttonText;
    }
    }
    

    Class Checkbox

    public class CheckBox extends Widget{
    private boolean checked;
    public CheckBox() {
    checked = false;
    }
    public boolean isChecked(){
    return (checked == true);
    }
    public void setCheck(boolean checked){
    this.checked = checked;
    }
    }
    

    Activity Class

    package com.som_itsolutions.training.java.exampleeventlistener;

    public class Activity implements Widget.OnClickEventListener
    {
        public Button mButton;
        public CheckBox mCheckBox;
        private static Activity mActivityHandler;
        public static Activity getActivityHandle(){
            return mActivityHandler;
        }
        public Activity ()
        {
            mActivityHandler = this;
            mButton = new Button();
            mButton.setOnClickEventListner(this);
            mCheckBox = new CheckBox();
            mCheckBox.setOnClickEventListner(this);
            } 
        public void onClick (Widget source)
        {
            if(source == mButton){
                mButton.setButtonText("Thank you for clicking me...");
                System.out.println(((Button) mButton).getButtonText());
            }
            if(source == mCheckBox){
                if(mCheckBox.isChecked()==false){
                    mCheckBox.setCheck(true);
                    System.out.println("The checkbox is checked...");
                }
                else{
                    mCheckBox.setCheck(false);
                    System.out.println("The checkbox is not checked...");
                }       
            }
        }
        public void doSomeWork(Widget source){
            source.clickEvent();
        }   
    }
    

    Other Class

    public class OtherClass implements Widget.OnClickEventListener{
    Button mButton;
    public OtherClass(){
    mButton = Activity.getActivityHandle().mButton;
    mButton.setOnClickEventListner(this);//interested in the click event                        //of the button
    }
    @Override
    public void onClick(Widget source) {
    if(source == mButton){
    System.out.println("Other Class has also received the event notification...");
    }
    }
    

    Main Class

    public class Main {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Activity a = new Activity();
    OtherClass o = new OtherClass();
    a.doSomeWork(a.mButton);
    a.doSomeWork(a.mCheckBox);
    }
    }
    

    As you can see from the above code, that we have an interface called events which basically lists all the events that may happen for our application. The Widget class is the base class for all the UI components like Button, Checkbox. These UI components are the objects that actually receive the events from the framework code. Widget class implements the Events interface and also it has two nested interfaces namely OnClickEventListener & OnLongClickEventListener

    These two interfaces are responsible for listening to events that may occur on the Widget derived UI components like Button or Checkbox. So if we compare this example with the earlier Callback example using Java Interface, these two interfaces work as the Callback interface. So the higher level code (Here Activity) implements these two interfaces. And whenever an event occurs to a widget, the higher level code (or the method of these interfaces implemented in the higher level code, which is here Activity) will be called.

    Now let me discuss the basic difference between Callback and Eventlistener pattern. As we have mentioned that using Callback, the Callee can notify only a single Caller. But in the case of EventListener pattern, any other part or class of the Application can register for the events that may occur on the Button or Checkbox. The example of this kind of class is the OtherClass. If you see the code of the OtherClass, you will find that it has registered itself as a listener to the ClickEvent that may occur in the Button defined in the Activity. Interesting part is that, besides the Activity ( the Caller), this OtherClass will also be notified whenever the click event occurs on the Button.

    0 讨论(0)
  • 2020-11-21 23:39

    A callback function, also known as a higher-order function, is a function that is passed to another function as a parameter, and the callback function is called (or executed) inside the parent function.

    $("#button_1").click(function() {
      alert("button 1 Clicked");
    });
    

    Here we have pass a function as a parameter to the click method. And the click method will call (or execute) the callback function we passed to it.

    0 讨论(0)
  • 2020-11-21 23:40

    One important usage area is that you register one of your function as a handle (i.e. a callback) and then send a message / call some function to do some work or processing. Now after the processing is done, the called function would call our registered function (i.e. now call back is done), thus indicating us processing is done.
    This wikipedia link explains quite well graphically.

    0 讨论(0)
  • 2020-11-21 23:41

    The Callback page on Wikipedia explains it very well:

    In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

    0 讨论(0)
  • 2020-11-21 23:41

    Assume we have a function sort(int *arraytobesorted,void (*algorithmchosen)(void)) where it can accept a function pointer as its argument which can be used at some point in sort()'s implementation . Then , here the code that is being addressed by the function pointer algorithmchosen is called as callback function .

    And see the advantage is that we can choose any algorithm like:

      1.    algorithmchosen = bubblesort
      2.    algorithmchosen = heapsort
      3.    algorithmchosen = mergesort   ...
    

    Which were, say,have been implemented with the prototype:

      1.   `void bubblesort(void)`
      2.   `void heapsort(void)`
      3.   `void mergesort(void)`   ...
    

    This is a concept used in achieving Polymorphism in Object Oriented Programming

    0 讨论(0)
提交回复
热议问题