Deep understanding of the builder model in Android _android

Source: Internet
Author: User
Tags static class

Objective

During the Android development, I found that many An Zhouyuan code used design patterns, more commonly used adapter patterns (various adapter), Builder Mode (Alert dialog build), and so on. Although we know a lot about design patterns, in this aspect of applying design patterns, it feels like many people are lacking in this. So this article is a deep understanding of the builder model in Android.

Builder mode (Builder pattern) is also called the generator pattern, which is defined as follows:

Separate the construction of a complex object from it representation so this same construction process can create dif Ferent representations. Separates the build of a complex object from its markup, so that the same build process can create different representations.

My understanding: To separate a product (object) representation from the build process so that the product is built in the same way that it can have different product representations.

Application Scenarios

Here's a common example of Android:

The construction of the Alertdialog dialog box in Android is a typical application of the builder model.


Take a look at builder source

 public static class Builder {private final alertcontroller.alertparams P;
  Public Builder {This (context, resolvedialogtheme (context, 0));
        {P = new Alertcontroller.alertparams (Builder, int themeresid) (New Contextthemewrapper (
  Context, Resolvedialogtheme (context, themeresid)); }//Various set parameter Methods Settitle () .../** * Creates an {@link Alertdialog} with the arguments supplied to
   This * builder. * <p> * Calling this method does not display the dialog. If no additional * processing is needed, {@link #show ()} May was called instead to both * Create and display the dial
   og.
    * * Public Alertdialog Create () {//context has already been wrapped with the appropriate theme.
    Final Alertdialog Dialog = new Alertdialog (P.mcontext, 0, false);
    P.apply (Dialog.malert);
    Dialog.setcancelable (p.mcancelable); if (p.mcancelable) {dialog.setcanceledontouchoutside (True);
    } dialog.setoncancellistener (P.moncancellistener);
    Dialog.setondismisslistener (P.mondismisslistener);
    if (P.monkeylistener!= null) {Dialog.setonkeylistener (P.monkeylistener);
  return dialog;  //This show method is a builder object, which encapsulates create () and show () to directly Fetch and Display dialog box public Alertdialog shows () {Final Alertdialog Dialog
    = Create ();
    Dialog.show ();
  return dialog; }
}

Analysis of the source can see the inner class Builder is used to build this dialog box:

1, builder when created, will put this AlertController.AlertParams P; object p create new out

2, in the bilder setting of various parameters, these parameters exist in the object P

3, then builder the parameter is set up after the call create method.

Final Alertdialog Dialog = new Alertdialog (P.mcontext, 0, false);
P.apply (Dialog.malert); Malert is in the outer class

In this method, the constructor of the outer class is called First AlertDialog , new an external class object is created, and then the p.apply() method assigns the object p as the property of the inner class AlertController mAlert . This completes a build.

The following is part of the source code of Alertdialog

public class Alertdialog extends Dialog implements Dialoginterface {
  //This object is used to accept parameters set within the builder
  private Alertcontroller Malert;

  The following construction methods determine that only the inner class builder can be used to construct
  the protected alertdialog (context
    , 0);

  Protected Alertdialog (Context, Boolean cancelable,   Oncancellistener cancellistener) {This
    (context, 0 );
    Setcancelable (cancelable);
    Setoncancellistener (Cancellistener);
  }


  Protected Alertdialog (context, @StyleRes int themeresid) {This
    (context, themeresid, true);
  }

  Alertdialog (context, @StyleRes int Themeresid, Boolean createcontextthemewrapper) {
    Super (context, Createcontextthemewrapper? Resolvedialogtheme (Context, Themeresid): 0,
      createcontextthemewrapper);

    Mwindow.alwaysreadcloseontouchattr ();
    Malert = new Alertcontroller (GetContext (), this, GetWindow ());
    }

Summarize

The above is the full content of this article, I hope this article on the content of Android developers can help, if you have questions you can message exchange.

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.