Overview
When you learn. NET, learn to entrust, also commissioned to do the package of workflow, and think he is very important, so, in learning Java, also to learn this piece of content, do not know, a learning is very surprised, because the Java delegation is not like. NET, the implementation of the underlying class library support, but the method of the callback to introduce the real Now this mechanism, the implementation of Java delegation begins.
implementation of the "delegate"frame composition and class diagram
Code implementationB-Layer class and interface code
Callback interface Public interface Computecallback {public void oncomputeend (); The Listener class public class Callbackbll {public void Compute (Computecallback callback) { if (callback! = null) { C12/>callback.oncomputeend ();}}}
code for the U-layer class
Callback Interface implementation class public class Mycomputecallbackimpl implements computecallback{ @Override public Void Oncomputeend () { System.out.println ("delegate" of the External Class);} } public class Testdelegate {public static void Main (string[] args) { Testcallback testcallback = new Testcallback ( ); "Delegate" of the anonymous class, omitting //external class "delegate" mycomputecallbackimpl Mycomputecallbackimpl = new Mycomputecallbackimpl (); Testcallback.compute (3, MYCOMPUTECALLBACKIMPL1); } }
Summary
The idea of entrusting is to give the specific part to the caller to handle.
"Delegates" in Java