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