What is Buidler mode? is to separate the construction of a complex object from its representation so that the same build process can create different representations. The builder pattern is a step-by-step creation of a complex object that allows users to build them simply by specifying the type and content of complex objects.
So why use Buidler?
is to separate the process of building a complex object from its parts because of a complex object, not only a very large number of components, such as the Alertdialog dialog box, there are very many components, such as Tittle,message,icon,positivebutton, etc. But more than that, how to assemble these parts into a Alertdialog dialog, this assembly process can also be a very complex step, and the builder mode is designed to separate parts from the assembly process. The popular point is that I first separate the components and then put them into a class to assemble the components.
How to use?
Let's take a look at the source code for Alertdialog.builder inside Android to know.
public class Alertdialog extends Dialog implements Dialoginterface {//Controller, accept each of the arguments in the builder member variable p private Alertcontroller Malert; Constructor protected Alertdialog (context context, int theme) {This (context, theme, true); }//4: Construct Alertdialog Alertdialog (context context, int theme, Boolean createcontextwrapper) {Super (Context, Resolvedialogtheme (context, theme), createcontextwrapper); Mwindow.alwaysreadcloseontouchattr (); Malert = new Alertcontroller (GetContext (), this, GetWindow ()); }//is actually called the Settitle method of the Malert @Override public void Settitle (charsequence title) {Super.settit Le (title); Malert.settitle (title); }//is actually called the Setcustomtitle method of Malert public void Setcustomtitle (View customtitleview) {Malert.setcus Tomtitle (Customtitleview); public void Setmessage (Charsequence message) {malert.setmessage (message); }//Alertdialog other code omitted//************ builder for Alertdialog inner class ******************* Publi C Static Class Builder {///1: This class is used to store the various parameters of the Alertdialog, such as title, Message, icon, and so on. Private final Alertcontroller.alertparams P; /** * Constructor Using a context for this builder and the {@link Alertdialog} it creates. */Public Builder (context context) {This (context, resolvedialogtheme (context, 0)); } public Builder (context context, int theme) {P = new Alertcontroller.alertparams (New context Themewrapper (Context, Resolvedialogtheme (context, theme)); Mtheme = theme; }//2: Set various parameters to P public Builder Settitle (charsequence title) {P.MTI tle = title; return this; } public Builder Setmessage (charsequencE message) {p.mmessage = message; return this; Public Builder setIcon (int iconid) {P.miconid = iconID; return this; Public Builder Setpositivebutton (charsequence text, final Onclicklistener listener) {P . mpositivebuttontext = text; P.mpositivebuttonlistener = listener; return this; Public Builder Setview (view view) {P.mview = view; P.mviewspacingspecified = false; return this; }//3: Build Alertdialog, pass the parameters public Alertdialog Create () {//Call new Alertdialog Alertdialog Final Alertdialog Dialog = new Alertdialog (P.mcontext, Mtheme, false); 5: Apply the parameters in P to the Malert object in the dialog//This step is the core method we wait to see the source code continue to speak P.apply (Dialog.malert); Dialog.setcancelabLe (p.mcancelable); if (p.mcancelable) {dialog.setcanceledontouchoutside (true); } dialog.setoncancellistener (P.moncancellistener); if (P.monkeylistener! = null) {Dialog.setonkeylistener (P.monkeylistener); } return dialog; Public Alertdialog Show () {//6: Display dialog alertdialog dialog = Create (); Dialog.show (); return dialog; } } }from the source code above we can see that the dialog is built by builder to set the title, Message, button and other parameters in the Alertdialog, These parameters are stored in the member variable p with type Alertcontroller.alertparams, and the corresponding member variable is included in the Alertcontroller.alertparams. The Alertdialog is created when calling the CREATE function of the builder class, and the arguments saved in the Builder member variable p are applied to the Malert object in Alertdialog, and the Dialog.show method is called to display the dialog box.
Now let's take a look at the p.apply (dialog.malert) code snippet. Let's look at the implementation of the Apply function.
public void Apply (Alertcontroller dialog) {if (Mcustomtitleview! = null) {Dialog.setcustomtitle (mcustomtit Leview); } else {if (mtitle! = null) {dialog.settitle (mtitle); } if (Micon! = null) {Dialog.seticon (Micon); } if (Miconid >= 0) {Dialog.seticon (Miconid); } if (Miconattrid > 0) {dialog.seticon (Dialog.geticonattributeresid (Miconattrid)); }} if (mmessage! = null) {dialog.setmessage (mmessage); } if (Mpositivebuttontext! = null) {Dialog.setbutton (dialoginterface.button_positive, Mpositivebuttontext, Mpositivebuttonlistener, NULL); } if (Mnegativebuttontext! = null) {Dialog.setbutton (dialoginterface.button_negative, Mnegativebuttontext, Mnegativebuttonlistener, NULL); } if (Mneutralbuttontext! = null) {Dialog.setbutton (DIaloginterface.button_neutral, Mneutralbuttontext, mneutralbuttonlistener, NULL); } if (Mforceinversebackground) {dialog.setinversebackgroundforced (true); }//For a list, the client can either supply an array of items or a//adapter or a cursor if ((Mitems! = NULL) | | (Mcursor! = null) | | (Madapter! = null)) {Createlistview (dialog); if (MView! = null) {if (mviewspacingspecified) {Dialog.setview (MView, Mviewspacingleft, MVi Ewspacingtop, Mviewspacingright, Mviewspacingbottom); } else {Dialog.setview (MView); } } }In fact, the number of references in P is set to Alertcontroller, which is the Malert object in Alertdialog. From the various setter methods of alertdialog, we can also see that the Malert corresponding setter method is actually called.
After reading the above source code we can find, no wonder we usually call the dialog box can be used directly, alertdialog dialog = new Alertdialog (P.mcontext, Mtheme, false); Without Alert.builder method creation can also, because its essence is the same, builder is only the production process of the components into a step by step implementation.
What is the practical effect of this?
In the actual use of Java, we often use the concept of "pool", when the resource provider is unable to provide sufficient resources, and these resources need to be repeatedly shared by a very large number of users, you need to use the pool. "Pool" is actually a piece of memory, when there are some complex resources in the pool "limb" (such as database connection pool, perhaps sometimes a connection will be interrupted), assuming that the recycling of these "limbs", will improve memory efficiency, improve the performance of the pool, And here Alertdialog.builder is the pool, changing the builder mode of the P.apply (assembly) class so that it can diagnose "limb" broken on which part, and then repair the part.
On the Buidler mode of Android development combined with Alertdialog.builder commentary