Source: Click to open link
Example 1
First look at a piece of code:
Template mode:
Public abstract class b{public void execute () { getconnection (); Docrud (); Releaseconnection (); } public abstract void Docrud (); public void getconnection () { System.out.println ("Get Connected ..."); public void Releaseconnection () { System.out.println ("Release connection ...");} } public class A extends b{public void Docrud () { System.out.println ("Perform add Operation ..."); public void Add () { Docrud (); } } public class C extends b{public void Docrud () { System.out.println ("Perform a delete operation ..."); public void Delete () { Docrud (); }}
Change to callback mode:
public Interface callback{public void Docrud (); }
package Huidiao.sample;public class Hibernatetemplate {public void execute (CallBack action) {getconnection (); Action.docrud (); Releaseconnection (); } public void Add () {Execute (new CallBack () {public void Docrud () { System.out.println ("Perform add Operation ..."); } }); } public void Delete () {Execute (new CallBack () {public void Docrud () { System.out.println ("Perform delete operation ..."); } }); } public void getconnection () {System.out.println ("Get Connected ..."); } public void Releaseconnection () {System.out.println ("Release connection ..."); } }
Visibleit is easier and more flexible to abandon the method of inheriting abstract class. Instead of inheriting abstract classes in order to implement abstract methods, you simply need to add a method with a callback to make it more intuitive and flexible. This is one of the benefits of a callback.
Here's a more aboutusing callbacks with asynchronous callsA very good example of this,
Example 2
Callback Interface:
Public interface CallBack { /** * Executes the callback method * @param objects returns the processed result as a parameter to the callback method */Public void Execute (Object ... objects);
Sender of message:
/** * This class is equivalent to your own */public class Local implements callback,runnable{private remote remote; /** * Message sent out */private String message; Public Local (remote remote, String message) {super (); This.remote = remote; this.message = message; }/** * Send message */public void SendMessage () {/** The name of the current thread **/syste M.out.println (Thread.CurrentThread (). GetName ()); /** creates a new thread to send the message **/thread thread = new thread (this); Thread.Start (); /** The current thread continues execution of **/System.out.println ("Message has been sent by local~!"); /** * Callback function after sending message */public void execute (Object ... objects) {/** Prints the returned message **/ System.out.println (Objects[0]); /** prints the thread name of the sending message **/System.out.println (Thread.CurrentThread (). GetName ()); /** interrupts the thread that sent the message **/ Thread.interrupted (); } public static void Main (string[] args) {Local local = new local (new Remote (), "Hello"); Local.sendmessage (); public void Run () {remote.executemessage (message, this); This is equivalent to call the classmate, after the call, the thread can do other things, just wait until your classmates call you back when you have to respond}}
Recipient of the message:
/** * This class is equivalent to your classmate * * Public class Remote { /** * Processing message * @param msg received message * @param CallBack callback function processing class * /public void ExecuteMessage (String msg,callback callBack) { /** It may take a lot of time for the analog remote class to process other things, **/for (int i=0;i<1000000000;i++) { } /** to handle the other things, and now to process the messages **/ System.out.println (msg); System.out.println ("I Hava executed the message by Local"); /** execution Callback **/ callback.execute (new string[]{"Nice to meet you~!"}); This is equivalent to calling you after your classmates have finished execution }
Executes the main method of the Local class.
Note The row for the red background in the Local class:
Remote.executemessage (Message, this);
The ExecuteMessage method needs to receive a message parameter, which means that the messages are sent out, while the callback parameter is himself, that is, this. Indicates that the message is sent, handled by the Local class itself, and calls its own Execute method to process the message result.
If this is not used here, but with other implementations of the callback interface, then it can not be called "callback", in the OO world, it belongs to "delegation". In other words,"callback" must be the sender of the message to process the message result, otherwise it cannot be called a callback . The concept must be clear.
Callbacks and asynchronous calls