Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 t...
Function Pointer
● Yes we can declare a non-const pointer to a
function as the following
int (*ptrFunc) ()
– It implies th...
Normal Function
● #include<stdio.h>
● /* function prototype */
● int func(int, int);
● int main(void)
● {
● int result;
● ...
Function Pointer
● #include<stdio.h>
● int func(int, int);
● int main(void)
● {
● int result1,result2;
● /* declaring a po...
Callback
● In computer programming, a callback is a
reference to executable code, or a piece of
executable code, that is p...
Callback
● Callback mechanism is heavily used in
Asynchronous programming. Its basic purpose
is to notify the caller when ...
Example of Callback (in C)
/* callback.c */
●
#include<stdio.h>
●
#include"reg_callback.h"
●
/* callback function definiti...
Callback - Example
● /* reg_callback.h */
● typedef void (*callback)(void);
● void register_callback(callback ptr_reg_call...
Callback - Example
● /* reg_callback.c */
● #include<stdio.h>
● #include"reg_callback.h"
● /* registration goes here */
● ...
Callback - Example
Result of the previous example
● This is a program demonstrating function callback
● inside register_ca...
Callback in Java – Using Interface
● Java does not have the concept of function
pointer
● It implements Callback mechanism...
Callback in Java – Using Interface
public interface Callback
● {
● public void notify(Result result);
● }
Callback in Java – Using Interface
public Class Caller implements Callback
● {
Callee ce = new Callee(this);
– //Other fun...
Callback in Java – Using Interface
public Class Callee {
● Callback cb;
● Callee(Callback cb){
– this.cb = cb;
● }
● doAsy...
EventListener/Observer
● This pattern is used to notify 0 to n numbers of
Observers/Listeners that a particular task has
f...
EventListener
● //public interface Events {
●
● public void clickEvent();
● public void longClickEvent();
● }
Widget
● See the following link
SourceCode_Of_Widget
(https://docs.google.com/document/d/18dTyRas
JF3698XQA909ozgkmWIxd6pY...
Button Class
public class Button extends Widget{
● private String mButtonText;
● public Button () { }
● public String getB...
CheckBox Class
public class CheckBox extends Widget{
● private boolean checked;
● public CheckBox() {
● checked = false;}
...
Activity Class
● See the source code
● Activity-Class Source Code
● (https://docs.google.com/document/d/18dTyRas
JF3698XQA...
Other Class
public class OtherClass implements Widget.OnClickEventListener{
● Button mButton;
● public OtherClass(){
● mBu...
Main Class
public class Main {
● public static void main(String[] args) {
● // TODO Auto-generated method stub
● Activity ...
Explanation
As you can see that the OtherClass is also
interested in the Click event of the Button inside
the Activity. Th...
Thank You!!!
● Get the source code from
https://github.com/sommukhopadhyay/EventListenerExample
● Reference : http://opens...
Upcoming SlideShare
Loading in …5
×

Different ways of Callback Mechanism and EventListener Pattern

803 views

Published on

My effort to explain the Callback mechanism that was used in Old C/C++ and now in a new form in Modern Java. This document does not include the new Callback mechanism of the latest C++. This document also explains the EventListener/Observer Pattern and tells the difference between Callback and Eventlistener pattern.

Published in: Technology

Different ways of Callback Mechanism and EventListener Pattern

  1. 1. Callback mechanisms using Function Pointer (in C), Interface (in Java) And EventListener Pattern by Somenath Mukhopadhyay +91 9748185282 som@som-itsolutions.com som.mukhopadhyay@gmail.com
  2. 2. 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???
  3. 3. 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
  4. 4. 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 = %dn",result); ● return 0; ● } ● /* func definition goes here */ ● int func(int x, int y) ● { ● return x+y; ● }
  5. 5. 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 = %dn",result1,result2); ● return 0; ● } ● int func(int x, int y) ● { ● return x+y; ● }
  6. 6. 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.
  7. 7. 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.
  8. 8. 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_callbackn"); ● } ● int main(void) ● { ● /* initialize function pointer to ● my_callback */ ● callback ptr_my_callback=my_callback; ● printf("This is a program demonstrating function callbackn"); ● /* register our callback function */ ● register_callback(ptr_my_callback); ● printf("back inside main programn"); ● return 0; ● }
  9. 9. Callback - Example ● /* reg_callback.h */ ● typedef void (*callback)(void); ● void register_callback(callback ptr_reg_callback);
  10. 10. 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_callbackn"); ● /* calling our callback function my_callback */ ● (*ptr_reg_callback)(); ● }
  11. 11. Callback - Example Result of the previous example ● This is a program demonstrating function callback ● inside register_callback ● inside my_callback ● back inside main program
  12. 12. 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
  13. 13. Callback in Java – Using Interface public interface Callback ● { ● public void notify(Result result); ● }
  14. 14. Callback in Java – Using Interface public 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 ● } ● }
  15. 15. Callback in Java – Using Interface 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 ● } ● }
  16. 16. 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)
  17. 17. EventListener ● //public interface Events { ● ● public void clickEvent(); ● public void longClickEvent(); ● }
  18. 18. Widget ● See the following link SourceCode_Of_Widget (https://docs.google.com/document/d/18dTyRas JF3698XQA909ozgkmWIxd6pYqbibOlqfzDW0/ edit?usp=sharing)
  19. 19. Button Class public class Button extends Widget{ ● private String mButtonText; ● public Button () { } ● public String getButtonText() { ● return mButtonText;} ● public void setButtonText(String buttonText) { ● this.mButtonText = buttonText;} ● }
  20. 20. CheckBox Class 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;} ● }
  21. 21. Activity Class ● See the source code ● Activity-Class Source Code ● (https://docs.google.com/document/d/18dTyRas JF3698XQA909ozgkmWIxd6pYqbibOlqfzDW0/ edit?usp=sharing)
  22. 22. Other Class public 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..."); ● } ● } ●
  23. 23. 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); ● } ● }
  24. 24. Explanation As 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.
  25. 25. 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/

×