1. What is a callback function
Callback functions, as the name implies, functions for callbacks. A callback function is just a functional fragment, a function implemented by the user in accordance with the callback function calling convention. A callback function is a part of a workflow that determines the timing of the call (callback) of a function by a workflow. The callback function contains the following features:
(1) belongs to a part of the workflow;
(2) must be declared (defined) according to the calling convention specified by the workflow;
(3) The timing of his call is determined by the workflow, and the implementation of the callback function cannot directly invoke the callback function to realize the function of the workflow;
2. Callback mechanism
A callback mechanism is a common design pattern that exposes a workflow to an external user in a given interface, provides data to external users, or requires external users to provide data.
Callback mechanism for 3.java
There is always a certain interface between software modules, which can be divided into three categories: synchronous invocation, callback, asynchronous invocation.
Synchronous invocation: A blocking call in which the caller waits for the other party to complete before returning, which is a one-way invocation;
Callback: A two-way invocation pattern, that is, the callee calls the interface when the interface is called
Asynchronous invocation: A mechanism similar to a message or event, but it is called in the opposite direction, and the service of the interface proactively notifies the client (that is, the interface that invokes the client) when a message is received or an event occurs.
Callbacks are very closely related to asynchronous calls: Use callbacks to register asynchronous messages, and implement notification of messages through asynchronous calls.
for the moment, let's not discuss some of the terminology and operating mechanisms of callbacks, first of all, why is there a callback call? The emergence of synchronous and asynchronous mechanisms do not need to say, we know that the callback mechanism why it appears? In our real life, there is a situation where a boss is busy, he has no time to stare at his employees, and then he tells his employees to finish the current events and tell him the results of his work. This example is actually a callback + asynchronous example, another example, a programmer wrote a program A, which reserved the callback function interface, and encapsulated the program, Programmer b Let a call its own program B in a method, so he through the interface in a called back to the method in B, You may be indefinitely here, and you will continue to explain why the callback occurred. Next we turn the above example into code, and see many people on the net finally confusing async and callbacks, so the example does not add an asynchronous call. ( Note: Callbacks are not a solution to the problem of long call times, it's asynchronous!) )
Start by creating a callback interface that tells the boss how to find his way through the job: Leave the boss's office address:
Package net.easyway.test; /** * This interface is a way of contacting, whether it is a phone number or a contact address, as * The owner must implement this interface * @author Administrator * */Public Interface Callbackinterface {public void execute (); }
Create callback object, is the boss himself, because the staff to call him after the work, so the boss must implement callback interface, or where employees go to find the Boss?
Package net.easyway.test; /** * The boss is a top-level application, the lower-level application (employees) is not aware of the method, so he wants to be the lower-level application (employee) Call must implement this interface * @author Administrator * */ Public class Boss implements Callbackinterface { @Override public Void execute () { System.out.println ("Roger It!! "+ System.currenttimemillis ()); } }
Create the control class, which is the employee object, he must hold the boss's address (callback interface), even if the boss changed stubble and stubble, the office is unchanged, always find the corresponding boss.
Package net.easyway.test; /** * Employee class, it must be remembered that this is an underlying class, the bottom layer is not aware of the upper service * @author Administrator * * */Public class Employee { Private Callbackinterface callBack = null; Tell the boss the contact way, that is, register public void Setcallback (Callbackinterface callBack) { this.callback = callBack; } Workers work public void Dosome () { //1. Started working for (int i=0;i<10;i++) {System.out.println ("" " + i +" " It's done! "); } 2. Tell the boss to finish Callback.execute (); } }
test Class Code:
Package net.easyway.test; public class Client {public static void Main (string[] args) { employee emp = new Employee (); The callback object (upper object) is passed in and registered Emp.setcallback (new Boss ()); Open the Controller object to run emp.dosome ();} }
In this example, you can compare it with the example of programmer A and programmer B.
Looking at the example above, some people may think that this is not interface-oriented programming? How can it be a callback, and you think about it, our interface-oriented programming call relationship? In layer three, when the business layer calls the data layer, it does not need to pass the business layer itself to the data layer, and this is an upper layer call the underlying relationship, such as when we use the framework, the general direct call to the framework provided by the API can be, but the callback is different, when the framework does not meet the requirements, We want the framework to call its own class method, how to do it? There's no way to change the frame. Many excellent frameworks are almost all related interfaces, we just need to implement the relevant interface, we can complete the registration, and then let the framework to call our own class at the appropriate time, remember when we use struts, when we write the action, we need to inherit the action class, Then implement the Execute () method, write our own business logic code in the Execute () method, and complete the processing of the user request. This makes it possible to guess that the framework and the container provide a large number of callback interfaces to suit individual customization.
In short, to be clear, the first thing is to understand the reason for the callback function, that is, the application of the scene, in order to clear the callback mechanism, otherwise less effort.
Finally, to give an example, in order to make our function close to perfect, we will part of the function of outsourcing to others, let others personalized customization, as for others how to achieve regardless, I have to do is to define the relevant interface, This design allows the underlying code to invoke high-level defined subroutines, enhance program flexibility, And reflection has the same kind of wonderful, I think this is the real reason for the callback, the above is my personal understanding, hope to discuss!
Research on Java callback mechanism