In object-oriented languages, callbacks are implemented through interfaces or abstract classes, and we refer to the class that implements the interface as the callback class, where the object of the callback class is called the callback object, and the method that handles the event is called the callback method. (Excerpt from Baidu Encyclopedia)
Then through the above sentence to the Baidu encyclopedia "callback function" translated into Java version: The callback method is a reference to the callback object (in Java, the reference to the object's address) called method. If you put
The callback object's reference (address) is passed as a parameter to another method, and when the reference is used to invoke the method it points to, we say this is a callback method. The callback method is not called directly by the implementing party of the method.
Instead, it is called by another party when a particular event or condition occurs, and is used to respond to the event or condition.
Here we come up with a small demand to implement callbacks, the need for: Wang Gang eggs to the restaurant to eat, the restaurant waiter according to their appetite for their meals. We first conceived, first of all, to have an interface, the interface defines an abstract method for the appetite (callback interface), followed by the creation of Wang Gang Eggs (callback Class), let the Wang Gang egg implementation of this interface, and want to good appetite (callback method); Finally, creating a restaurant, The restaurant attendant (the other party) receives the Wang Gang Egg (callback object) and eats the meal according to the appetite (callback method) it provides (the event invokes the callback method). On the code:
/*** Customers, to eat in the cafeteria has a premise, to tell the waiter their appetite, will give you sheng fan *@authorCoder **/Interfaceclient{/*** Tell the waiter his appetite *@return */ PublicString appetite ();}classWanggangdanImplementsclient{@Override PublicString Appetite () {return"A Car of rice"; }}/*** Canteen*/classrestaurant{/*** Meal method, if the customer to inform the waiter your appetite, he will be based on your appetite to give you "sheng" Rice *@paramClient Queuing Customer *@return */ PublicString dozenrice (client client) {return"Sheng" +Client.appetite (); }} Public classTest1 {//Business Processing class, Lao Wang to make dinner Public Static voidMain (string[] args) {Wanggangdan Laowang=NewWanggangdan ();//Wang Gang egg with alias Laowang go to the cafeteria beggingRestaurant waiter=NewRestaurant (); String Dozenrice=Waiter.dozenrice (Laowang); System.out.println (Dozenrice); //finally Lao Wang "Sheng A car Rice"//but usually we do not tell the waiter what our name is in the meal, so it is too troublesome, so can we just tell the waiter//How much would you like to eat? As a matter of common sense, we go to a restaurant and we only eat once. //Anonymous internal classes are used to implement callbacks, simpleString Dozenrice1=waiter.dozenrice (NewClient () {@Override PublicString Appetite () {return"One pot of rice"; }}); System.out.println (DozenRice1); }}
Here will use the template method pattern to explain what the hook method, many design patterns are used in callbacks, hooks and other concepts, these basic points understand, some patterns are less obscure, slightly rewrite the code above can be:
/*** Template Method pattern: Defines the skeleton of an algorithm in one method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm. * "Hook Method": the principle is to implement an empty method, in a task before, after, * Execute, report an exception after the method (is not a familiar feeling). * Usually the hook method is implemented by an abstract class or an empty method in this class. * */ Abstract classclient{/*** "Template Method"*/ Public voidTemplatemethod () {before (); Appetite (); After (); } /*** "Hook method" before the meal (an empty implementation)*/ protected voidbefore () {}; /*** "Abstract method" tells the waiter his appetite *@returnAppetite*/ Public Abstract voidappetite (); /*** "Specific method" after Sheng fan*/ Private voidAfter () {//The actual project here is the common business logicSystem.out.println ("Take the chopsticks, find the table, open the food ..."); }}/*** Canteen*/classrestaurant{/*** Meal method, if the customer to inform the waiter your appetite, he will be based on your appetite to give you "sheng" Rice *@paramClient Queuing Customer *@return */ Public voiddozenrice (client client) {Client.templatemethod (); }} Public classTest1 {//Business Processing class, Lao Wang to make dinner Public Static voidMain (string[] args) {Restaurant waiter=NewRestaurant (); Waiter.dozenrice (NewClient () {@Overrideprotected voidbefore () {System.out.println ("Glaring to the waiter!" "); } @Override Public voidappetite () {System.out.println ("A pot of rice"); }}); }}
Java callback methods, hook methods, and template method patterns