"Delegates" in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.