On the Buidler mode of Android development combined with Alertdialog.builder explanation

Source: Internet
Author: User

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 the user to build them only by specifying the type and content of the complex objects.

So why use Buidler?

is to separate the process of building complex objects from its parts because of a complex object, not only a lot of components, such as the Alertdialog dialog box, there are many components, such as Tittle,message,icon,positivebutton, etc. But much more than that, how to assemble these parts into a alertdialog dialog, which can be a complex step, the builder pattern is to separate parts from the assembly process. The popular point is that I first separate the components and then put them into another class to assemble the components.

How do I use it?

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 parameter 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 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 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 continues to talk about 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 above source code, we can see that the dialog box is built through the builder to set the title, Message, button and other parameters, 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 parameters 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 parameters in P are 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, we can find that no wonder we usually call the dialog box can be used directly, alertdialog dialog = new Alertdialog (P.mcontext, Mtheme, false); It is not possible to create a Alert.builder method, because it is essentially the same, and builder simply takes the production process of the component into one step.

What is the practical effect of this?

In Java practical use, we often use the concept of "pool", when the resource provider is unable to provide sufficient resources, and these resources need to be shared repeatedly by many users, it is necessary to use the pool. "Pool" is actually a memory, when there are some complex resources in the pool "limb" (such as the database connection pool, perhaps sometimes a connection will be interrupted), if the recycling of these "limbs", will improve memory efficiency, improve the performance of the pool, And here Alertdialog.builder is the pool, modifying the builder mode of the P.apply (assembly) class so that it can diagnose "limb" broken on which part, and then repair the part.



Related Article

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.