Android Builder (builder) mode

Source: Internet
Author: User

About Builder mode details: http://blog.csdn.net/jjwwmlp456/article/details/39890699


Let's start with a picture.

There are still a lot of places where you can see the Builder mode used in Android.


The use of the following is approximate:

NotificationNotification. Builder (context). Build ();  Alertdialog.builder (context). Create ();

You can also create multiple properties before builder () and create (), similar to the following example


Example

/** * Author:stone * email  : [email protected] * Time   : 15/7/3 */public class Testbuilder {    private in t A;    Private String B;    public int Geta () {        return A;    }    Public String Getb () {        return b;    }    protected Testbuilder (Builder builder) {        this.a = builder.ma;        this.b = BUILDER.MB;    }    public static class Builder {        private int ma;        private String MB;        Public Builder Createa (int a) {            this.ma = A;            return this;        }        Public Builder SHOWB (String b) {            this.mb = b;            return this;        }        Public Testbuilder Build () {            return to new Testbuilder (this);}    }    public static void Main (string[] args) {        Testbuilder TB = new Testbuilder.builder ()                . Createa (.).                SHOWB ( "Susan")                . Build ();}            }

Note:

1. This is the construction of parts by static internal class builder

2. How each part is constructed returns the builder

3. The constructor of the external actual object should be private or protected so that it can only be created by an inner class


In contrast to the common Java-bean:

To assign a value to a property by using a bunch of parameters in a setter or construction method in a bean

Here, with new Builder (). A.B.C ... Build ();

Personal feeling, the advantage is:

1. There is a choice to build the required properties, do not need to use which construction method and tangled

2. When called, when a property is assigned, the compiler immediately prompts the other executable method of building properties, writing more convenient, fast






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Builder (builder) 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.