Tag Archives: Design pattern

Java callback function implementation case

1. What is a callback function

Callback function is a function called by function pointer. If you pass a function pointer (address) as a parameter to another function, when the pointer is used to call the function it points to, we say it is a callback function. In Java, pointers are called references. </ u> the callback function is not called directly by the implementer of the function, but by another party when a specific event or condition occurs, which is used to respond to the event or condition.

A callback method is any method that is called by another method with the callback method as its first parameter. Many times, a callback is a method that is called when something happens.

2. Application scenarios

Event driven mechanism

For example: Party A hires Party B to repair the car; then Party A goes to do other things; Party B notifies Party A that the car has been repaired, please come and get it. There is no need for Party A to wait for Party B to finish repairing the car.

3. Code examples

1. Test class

package com.callBack;
public class CallBckTest {

    public static void main(String[] args) {
        MainBusiness mainBusiness = new MainBusiness();

        System.out.println("*********The specific implementation class implements the callback method *********");
        mainBusiness.execute(new CallbackServiceImpl());

        System.out.println("********* anonymous internal class implements the callback method ********* flexible *********");
        mainBusiness.execute(new CallbackService() {
            public void callBackFunc() {
                System.out.println("The anonymous internal class callback function started executing...") ;
                System.out.println("Anonymous internal class callback function ends execution...\n");
            }
        });
    }
}

2. Business class and method

package com.callBack;
public class MainBusiness {

    private CallbackService callback;

    public void execute(CallbackService callback) {
        this.callback = callback;
        callBack();
    }

    public void callBack() {
        callback.callBackFunc();
    }
}

3. Callback function interface

package com.callBack;
//Callback functions of the interface and methods
public interface CallbackService {
    void callBackFunc();
}

4. Callback function implementation class

package com.callBack;
public class CallbackServiceImpl implements CallbackService {
    @Override
    public void callBackFunc() {
        System.out.println("The concrete implementation class callback function starts execution...") ;
        System.out.println("Concrete implementation class callback function ends execution...\n");
    }
}

5. Print results

*********Concrete implementation class implements the callback method *********
Concrete implementation class callback function starts execution...
Concrete implementation class callback function ends execution...

********* callback methods implemented by anonymous internal classes *********
Anonymous internal class callback function start execution...
Anonymous internal class callback function ends execution...