Hello friends today I will be discussing callback mechanism.Callback Mechanism is of two types Synchronous callback and Asynchronous callback.I have already talked about these types of callback mechanism before but lets recapitulate.Let me explain using an example.

In Synchronous callback my father will give me his laptop and i will be working on that laptop.Now my dad will be sitting idle and cannot do anything.For my dad it will be a wastage of time whereas in Asynchronous mechanism my dad will give me a spare laptop which we will be calling a resource.Now my dad can also work and i will also work simultaneously.

So in this scenario we will use Asynchronous mechanism.

Callback mechanism and observer pattern is kind of similar with the difference being that in observer pattern there can be multiple observers/callers and in callback mechanism there can only be one caller.

To start with there will be an Interface called Callback.

In this interface there are two methods one is onStartTask and the other is one FinishTask.Now, there are are two class one is callee which is me and caller which is my dad.So caller will be implementing the Callback Interface.In the caller class we will have a method called delegateTaskToCallee which will be telling the callee to do the task and another function for caller to do its own task.

After that in the onStartTask method we will write that the background task is starting and in the onFinishTask we will write that the background task is finished and thanking the callee.

In the class callee we will have a reference of the interface callback and write a function called doItInBackground in which we will call the callbacks onStartTask method in the beginning and then will create a thread which is the task here and once the task is done we will finish the task and notify the caller using the onFinishTask method.Here is a preview of what the program will look like if executed.

Java Source Code:

package com.ridit.java.Callback;

public class Main {

        public static void main(String[] args) {

                // TODO Auto-generated method stub

                Caller caller = new Caller();

                caller.delegateTaskToCallee();

                caller.CallerOwnTask();

        }

}

package com.ridit.java.Callback;

public interface Callback {

        

        public void onStartTask();

        public void onFinishTask();

}

package com.ridit.java.Callback;

public class Caller implements Callback {

        

        Callee callee = new Callee(this);

        

        public void delegateTaskToCallee() {

                this.callee.doItInBackground();

        }

        public void CallerOwnTask() {

                for (int i = 0;i<10;i++) {

                        System.out.println("Caller is doing its own task");

                }

        }

        

        @Override

        public void onStartTask() {

                // TODO Auto-generated method stub

                

                System.out.println("The background Task is about to start");

        }

        @Override

        public void onFinishTask() {

                // TODO Auto-generated method stub

                System.out.println("The task is finished thank you for taking my burden, callee");

        }

        public Callee getCallee() {

                return callee;

        }

        public void setCallee(Callee callee) {

                this.callee = callee;

        }

}

package com.ridit.java.Callback;

public class Callee {

        

        private Callback cb;

        

        public Callee(Callback cb) {

                this.setCb(cb);

        }

        public Callback getCb() {

                return cb;

        }

        public void setCb(Callback cb) {

                this.cb = cb;

        }

        

        public void doItInBackground() {

                this.cb.onStartTask();

                

                Thread thread = new Thread (new Runnable() {

                        

                        

                        @Override

                        public void run() {

                                // TODO Auto-generated method stub

                                

                                

                                

                                for(int i = 0;i<10;i++) {

                                        System.out.println("Callee is doing the background task");

                                }

                                cb.onFinishTask();

                        }

                        

                        

                });

                thread.start();

                

        }

}

Source Code In C++

Note:This program requires C++11 or more.

//============================================================================

// Name        : Callback.cpp

// Author      : Ridit

// Version     :

// Copyright   : @Ridit...

// Description : Hello World in C++, Ansi-style

//============================================================================

#include <iostream>

#include "Callback.h"

#include "Caller.h"

#include "Callee.h"

using namespace std;

int main() {

        Caller* caller = new Caller();

        caller->delegateTaskToCallee();

        caller->doOwnTask();

        return 0;

}

/*

* Callback.h

*

*  Created on: 28-Dec-2023

*      Author: ridit

*/

#ifndef CALLBACK_H_

#define CALLBACK_H_

class Callback {

public:

        virtual void onStartTask() = 0;

        virtual void onFinishTask() = 0;

};

#endif /* CALLBACK_H_ */

/*

* Callee.h

*

*  Created on: 28-Dec-2023

*      Author: ridit

*/

#ifndef CALLEE_H_

#define CALLEE_H_

#include <iostream>

#include <thread>

#include "Callback.h"

using namespace std;

class Callee {

private:

        Callback* cb;

public:

        Callee(Callback* cb){

                this->cb = cb;

        }

        virtual ~Callee(){

        }

        static void task(){

                for(int i = 0;i<10;i++){

                        cout<<"Callee is doing the background task"<<endl;

                }

        }

        void doBackgroundTask(){

                this->cb->onStartTask();

                std::thread thread_object(Callee::task);

                thread_object.join();

                this->cb->onFinishTask();

        }

};

#endif /* CALLEE_H_ */

/*

* Caller.h

*

*  Created on: 28-Dec-2023

*      Author: ridit

*/

#ifndef CALLER_H_

#define CALLER_H_

#include <iostream>

#include "Callee.h"

using namespace std;

class Callee;

class Caller : public Callback{

private:

        Callee* callee;

public:

        Caller(){

                callee = new Callee(this);

        }

        void doOwnTask(){

                for(int i = 0;i<20;i++){

                        cout<<"Caller is doing its own task"<<endl;

                }

        }

        void delegateTaskToCallee(){

                callee->doBackgroundTask();

        }

        virtual ~Caller(){

        }

        void onStartTask(){

                cout<<"The background task is starting"<<endl;

        }

        void onFinishTask(){

                cout<<"The background task is finished thank you for taking my burden,Callee"<<endl;

        }

};

#endif /* CALLER_H_ */

Output

The background task is starting

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

Callee is doing the background task

The background task is finished thank you for taking my burden,Callee

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task

Caller is doing its own task