Android in-depth design mode (a) Entrust mode

Source: Internet
Author: User

(a) A brief introduction to the mode of entrust

Entrust mode is one of the main design patterns. Entrusted. That is, let another object help you do things.

More models, such as state mode, strategy mode, and interview mode, are essentially used in a more special situation.

The commit mode allows us to use aggregations instead of inheritance. java-combination is better than inheritance.

The simplest Java commit mode

Class Realprinter {    void print () {        System.out.println ("Real Printer");}    } Class Printer {     realprinter realprinter = new Realprinter ();     public void print () {        realprinter.print ();     }}
/** * Simple Entrust Mode *  * @author Peter_wang * @create-time 2014-5-19 pm 5:39:42 */public class Delegationdemo {    /**     * @ param args     *    /public static void main (string[] args) {        Printer Printer = new Printer ();        Printer.print ();    }}

(ii) the trust mode in Android

The Listerner event in Android is a commit mode, for example, a button click event.

Let's simulate how the entire click event is using the commit model.

/** * Analog Basic View *  * @author Peter_wang * @create-time 2014-5-19 pm 5:03:55 */public Class View {    private onclicklist Ener Monclicklistener;    /**     * Analog Click event */public    void Clickevent () {        if (monclicklistener! = null) {            Monclicklistener.onclick (this);        }    }    public void Setonclicklistener (Onclicklistener onclicklistener) {        this.monclicklistener = Onclicklistener;    }    /**     * Click event interface     *      * @author peter_wang     * @create-time 2014-5-19 pm 5:05:45    */Public Interface Onclicklistener {public        void OnClick (View v);    }}

/** * Analog button *  * @author Peter_wang * @create-time 2014-5-19 pm 5:17:57 */public class Button    extends View {}

/** * Simulated basic Activity class *  * @author Peter_wang * @create-time 2014-5-19 pm 5:20:38 */public class Activity {public    STA tic void Main (string[] args) {        activity activity = new activity ();        Activity.oncreate ();    }    /**     * Analog OnCreate method     *    /protected void OnCreate () {    }}

/** * Entrust Simulation page *  * @author Peter_wang * @create-time 2014-5-19 pm 5:19:22 */public class delegationactivity    extends A Ctivity    implements Onclicklistener {    private Button Mbutton;    @Override    protected void OnCreate () {        super.oncreate ();        Mbutton = new Button ();        Mbutton.setonclicklistener (this);        Analog Click event        mbutton.clickevent ();    }    @Override public    void OnClick (View v) {        if (v = = Mbutton) {            System.out.println ("OnClick () is callback!");        }    }}



Android in-depth design mode (a) Entrust mode

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.