Exploring the Buidler mode of Android development with AlertDialog. Builder

Source: Internet
Author: User

Exploring the Buidler mode of Android development with AlertDialog. Builder
What is the Buidler mode? The construction of a complex object is separated from its representation, so that different representations can be created during the same construction process. the Builder mode is used to create complex objects step by step. It allows users to build complex objects by specifying the types and content of complex objects.

So why is Buidler used?

This is to separate the process of building a complex object from its components. Because a complex object has many components, such as the AlertDialog dialog box, there are many components, such as Tittle and Message, icon, PositiveButton, and so on. But how can we assemble these components into an AlertDialog dialog box? This assembly process may be a very complicated step, the Builder mode is used to separate parts from the assembly process. In layman's terms, each component is produced separately and then assembled by another class.

How to use it?

Let's take a look at the source code of AlertDialog. Builder in Android.

Public class AlertDialog extends Dialog implements DialogInterface {// Controller, accepts the private AlertController mAlert parameter in the Builder member variable P; // constructor protected AlertDialog (Context context, int theme) {this (context, theme, true) ;}// 4: Construct AlertDialog (Context context, int theme, boolean createContextWrapper) {super (context, resolveDialogTheme (context, theme ), createContextWrapper); mWindow. AlwaysReadCloseOnTouchAttr (); mAlert = new AlertController (getContext (), this, getWindow ();} // actually calls the setTitle method of mAlert @ Override public void setTitle (CharSequence title) {super. setTitle (title); mAlert. setTitle (title);} // actually calling the setCustomTitle method of mAlert is public void setCustomTitle (View customTitleView) {mAlert. setCustomTitle (customTitleView);} public void setMessage (CharSequence message ){ MAlert. setMessage (message );} // other AlertDialog code is omitted. // ************* the internal class of AlertDialog is ************* * ***** public static class Builder {// 1: this class is used to store parameters of AlertDialog, such as title, message, and icon. private final AlertController. alertParams P;/*** Constructor using a context for this builder and the {@ link AlertDialog} it creates. */public Builder (Context context) {this (context, resolveDialogThe Me (context, 0);} public Builder (Context context, int theme) {P = new AlertController. alertParams (new ContextThemeWrapper (context, resolveDialogTheme (context, theme); mTheme = theme;} // 2: set various parameters to P public Builder setTitle (CharSequence title) {P. mTitle = title; return this;} public Builder setMessage (CharSequence message) {P. mMessage = message; return this;} public Builder setIcon (int ico NId) {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: Construct AlertDialog, pass the public AlertDialog create () {// call new AlertDialog to construct the object, and pass the Parameter Deliver individual AlertDialog final AlertDialog dialog = new AlertDialog (P. mContext, mTheme, false); // 5: Apply the parameters in P to the mAlert object in the dieme. // this step is the core method. Let's wait and see the source code to continue talking 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 alertdiener show () {// 6: displays dialog AlertDialog dialog = create (); dialog. show (); return dialog ;}}}
From the source code above, we can see that the construction of the dialog box is to set the title, message, button and other parameters in AlertDialog through Builder. These parameters are stored in the type AlertController. in the member variable P of AlertParams, AlertController. alertParams contains the corresponding member variables. The AlertDialog is created only when the create function of the Builder class is called. The parameters saved in the Builder member variable P are applied to the alertdiert mAlert object, and the displayed dialog box is called.

Now let's take a look at the Section P. apply (dialog. mAlert. Let's look at the implementation of the apply function.

public void apply(AlertController dialog) {      if (mCustomTitleView != null) {          dialog.setCustomTitle(mCustomTitleView);      } 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 an      // adapter or a cursor      if ((mItems != null) || (mCursor != null) || (mAdapter != null)) {          createListView(dialog);      }      if (mView != null) {          if (mViewSpacingSpecified) {              dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,                      mViewSpacingBottom);          } else {              dialog.setView(mView);          }      }  }
In fact, it is to set the parameters in P to AlertController one by one, that is, the mAlert object in AlertDialog. From the setter methods of AlertDialog, we can also see that the setter methods corresponding to mAlert are also called.
After reading the source code above, we can find that, no wonder, we can directly use it when calling the dialog box at ordinary times. AlertDialog dialog = new AlertDialog (P. mContext, mTheme, false); no Alert. the Builder method can also be created because it is essentially the same. Builder only converts the component production process into a step-by-step implementation.

What is the actual effect of this operation?

In actual use of Java, we often use the concept of "Pool". When the resource provider cannot provide enough resources and these resources need to be shared by many users, you need to use the pool. "Pool" is actually a piece of memory. When the pool has some complex "broken limbs" (such as the database connection pool, sometimes a connection is interrupted ), if these "broken limbs" are recycled, the memory usage efficiency will be improved and the pool performance will be improved. Here, AlertDialog. builder is the pool. Modify p in Builder mode. apply (assembly) Class enables you to diagnose the part on which the "disconnected" part is broken, 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.