最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入理解Android中的建造者模式

 更新時(shí)間:2016年09月30日 09:49:16   投稿:daisy  
建造者模式將一個(gè)復(fù)雜的構(gòu)建與其表示相分離,使得同樣的構(gòu)建過程可以創(chuàng)建不同的表示。所以這篇文章主要介紹了Android中的建造者模式,有需要的朋友們可以參考借鑒。

前言

在Android開發(fā)過程中,我發(fā)現(xiàn)很多安卓源代碼里應(yīng)用了設(shè)計(jì)模式,比較常用的有適配器模式(各種adapter),建造者模式(Alert Dialog的構(gòu)建)等等。雖然我們對(duì)大多數(shù)設(shè)計(jì)模式都有所了解,但是在應(yīng)用設(shè)計(jì)模式的這個(gè)方面,感覺很多人在這方面有所不足。所以這篇文章我們一起深入的理解Android中的建造者模式。

建造者模式(Builder Pattern)也叫生成器模式,其定義如下:

separate the construction of a complex object from its representation so that the same construction process can create different representations.將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的標(biāo)示分離,這樣的話就可以使同樣的構(gòu)建過程可以創(chuàng)建不同的表示。

我的理解:就是把一個(gè)產(chǎn)品(對(duì)象)表示(展示)和構(gòu)建(創(chuàng)建)過程分離開來,這樣產(chǎn)品的構(gòu)建流程相同卻可以有不同的產(chǎn)品表示。

應(yīng)用場(chǎng)景

這里舉出Android中常見的例子:

android中的AlertDialog對(duì)話框的構(gòu)建過程就是建造者模式的典型應(yīng)用。


看一下Builder源碼

 public static class Builder {
  private final AlertController.AlertParams P;

  public Builder(Context context) {
    this(context, resolveDialogTheme(context, 0));
  }


  public Builder(Context context, int themeResId) {
    P = new AlertController.AlertParams(new ContextThemeWrapper(
        context, resolveDialogTheme(context, themeResId)));
  }

  //各種set參數(shù)方法
  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 be called instead to both
   * create and display the dialog.
   */
  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;
  }


  // 這個(gè)show方法是builder對(duì)象的,里面封裝了create()和show()可以直接調(diào)取創(chuàng)建并顯示對(duì)話框
  public AlertDialog show() {
    final AlertDialog dialog = create();
    dialog.show();
    return dialog;
  }
}

分析源碼可以看到內(nèi)部類Builder是用來構(gòu)建這個(gè)對(duì)話框的:

    1、創(chuàng)建builder的時(shí)候,會(huì)把這個(gè)AlertController.AlertParams P;對(duì)象P創(chuàng)建new出來

    2、在對(duì)bilder設(shè)置各種參數(shù)的時(shí),這些參數(shù)都存在了對(duì)象P中

    3、然后builder參數(shù)設(shè)置完畢后在調(diào)用create方法。

final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
P.apply(dialog.mAlert); // mAlert是外部類中的

這個(gè)方法中會(huì)首先調(diào)用外部類AlertDialog的構(gòu)造方法,new出一個(gè)外部類對(duì)象,然后p.apply()方法會(huì)將P這個(gè)對(duì)象作為內(nèi)部類的屬性賦值給AlertController的對(duì)象mAlert。這樣就完成了一次的構(gòu)建。

下面是AlertDialog的部分源碼

public class AlertDialog extends Dialog implements DialogInterface {
  // 這個(gè)對(duì)象用來承接builder內(nèi)部所設(shè)置的參數(shù)
  private AlertController mAlert;

  //以下幾個(gè)構(gòu)造方法決定只能通過內(nèi)部類builder來構(gòu)建
  protected AlertDialog(Context context) {
    this(context, 0);
  }

  protected AlertDialog(Context context, boolean cancelable,   OnCancelListener cancelListener) {
    this(context, 0);
    setCancelable(cancelable);
    setOnCancelListener(cancelListener);
  }


  protected AlertDialog(Context context, @StyleRes int themeResId) {
    this(context, themeResId, true);
  }

  AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
    super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
      createContextThemeWrapper);

    mWindow.alwaysReadCloseOnTouchAttr();
    mAlert = new AlertController(getContext(), this, getWindow());
    }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論

荥阳市| 子长县| 栾川县| 永昌县| 县级市| 毕节市| 若尔盖县| 阜南县| 于都县| 宜昌市| 涡阳县| 上蔡县| 永春县| 中牟县| 营口市| 获嘉县| 萝北县| 丰原市| 翁源县| 理塘县| 东城区| 屏东市| 五原县| 嘉鱼县| 岫岩| 彭阳县| 青州市| 凌源市| 扶沟县| 绿春县| 洛扎县| 尖扎县| 新疆| 桂林市| 宁强县| 高陵县| 香港| 车险| 阿鲁科尔沁旗| 万州区| 安陆市|