different ways of callback mechanism
TRANSCRIPT
Callback mechanisms using Function
Pointer (in C), Interface (in Java)And EventListener Pattern
by
Somenath Mukhopadhyay
Function Pointer
● Pointer is a variable that holds the address of another data
● When we declare int func(), it implies that func is kind of constant pointer to a method
● Can't we declare a non-const pointer to a method???
Function Pointer
● Yes we can declare a non-const pointer to a function as the following
int (*ptrFunc) ()
– It implies that ptrFunc is a pointer to a function which takes no parameter and return an integer
Normal Function● #include<stdio.h>
● /* function prototype */
● int func(int, int);
● int main(void)
● {
● int result;
● /* calling a function named func */
● result = func(10,20);
● printf("result = %d\n",result);
● return 0;
● }
● /* func definition goes here */
● int func(int x, int y)
● {
● return x+y;
● }
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;● }
Callback
● 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.” (Source : Wikipedia)
● In other words, Callback is the way through which we pass a function pointer to a code from where we want our callback/handler to be called.
Callback
● Callback mechanism is heavily used in Asynchronous programming. Its basic purpose is to notify the caller when the callee has finished some job
● So the caller called upon the callee and then does not block until the callee finishes the task.
● When the callee finishes its task, it notifies the caller and then the caller gets back the result.
Example of Callback (in 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;● }
Callback - Example
● /* reg_callback.h */● typedef void (*callback)(void);
● void register_callback(callback ptr_reg_callback);
Callback - Example
● /* 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)(); ● }
Callback - Example
Result of the previous example
● This is a program demonstrating function callback
● inside register_callback
● inside my_callback
● back inside main program
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
Callback in Java – Using Interface
public interface Callback● {● public void notify(Result result);● }
Callback in Java – Using Interfacepublic Class Caller implements Callback
● {
Callee ce = new Callee(this);
– //Other functionality
//Call the Asynctask
ce.doAsynctask();//pass self to the callee
● public void notify(Result result){
//Got the result after the callee has finished the task
//Can do whatever i want with the result
● }● }
Callback in Java – Using Interfacepublic 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● }● }
EventListener/Observer
● This pattern is used to notify 0 to n numbers of Observers/Listeners that a particular task has finished
● 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)
EventListener
● //public interface Events {●
● public void clickEvent();● public void longClickEvent();● }
Widget
● See the following link
SourceCode_Of_Widget
(https://docs.google.com/document/d/18dTyRasJF3698XQA909ozgkmWIxd6pYqbibOlqfzDW0/edit?usp=sharing)
Button Classpublic class Button extends Widget{
● private String mButtonText;● public Button () { } ● public String getButtonText() {● return mButtonText;}● public void setButtonText(String buttonText) {● this.mButtonText = buttonText;}● }
CheckBox Classpublic 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
● See the source code● Activity-Class Source Code● (https://docs.google.com/document/d/18dTyRas
JF3698XQA909ozgkmWIxd6pYqbibOlqfzDW0/edit?usp=sharing)
Other Classpublic class OtherClass implements Widget.OnClickEventListener{
● Button mButton;● public OtherClass(){● mButton = Activity.getActivityHandle().mButton;● mButton.setOnClickEventListner(this);● }● @Override● public void onClick(Widget source) {● if(source == mButton){● System.out.println("Other Class has also received the event
notification...");● }● }●
Main Classpublic 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);● }● }
ExplanationAs you can see that the OtherClass is also interested in the Click event of the Button inside the Activity. The Activity is responsible for the Button's click event. But alongwith the Activity (the Caller) the other parts of the Application (i.e. OtherClass) is also able to get this notification. This is one of the main significances of EventListener/Observer pattern.
Thank You!!!
● Get the source code from https://github.com/sommukhopadhyay/EventListenerExample
● Reference : http://opensourceforu.com/2012/02/function-pointers-and-callbacks-in-c-an-odyssey/