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.
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Different ways of Callback Mechanism and EventListener Pattern
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. 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. 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. 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. 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. 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. 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. 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;
●
}
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. 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. Callback in Java – Using Interface
public interface Callback
● {
● public void notify(Result result);
● }
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. 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. 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)
18. Widget
● See the following link
SourceCode_Of_Widget
(https://docs.google.com/document/d/18dTyRas
JF3698XQA909ozgkmWIxd6pYqbibOlqfzDW0/
edit?usp=sharing)
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. 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. Activity Class
● See the source code
● Activity-Class Source Code
● (https://docs.google.com/document/d/18dTyRas
JF3698XQA909ozgkmWIxd6pYqbibOlqfzDW0/
edit?usp=sharing)
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. 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. 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. 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/