国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 05:07:46
字體:
來源:轉載
供稿:網友

前言

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

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

separate the construction of a complex object from its representation so that the same construction process can create different representations.將一個復雜對象的構建與它的標示分離,這樣的話就可以使同樣的構建過程可以創建不同的表示。

我的理解:就是把一個產品(對象)表示(展示)和構建(創建)過程分離開來,這樣產品的構建流程相同卻可以有不同的產品表示。

應用場景

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

android中的AlertDialog對話框的構建過程就是建造者模式的典型應用。


看一下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參數方法  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;  }  // 這個show方法是builder對象的,里面封裝了create()和show()可以直接調取創建并顯示對話框  public AlertDialog show() {    final AlertDialog dialog = create();    dialog.show();    return dialog;  }}

分析源碼可以看到內部類Builder是用來構建這個對話框的:

    1、創建builder的時候,會把這個AlertController.AlertParams P;對象P創建new出來

    2、在對bilder設置各種參數的時,這些參數都存在了對象P中

    3、然后builder參數設置完畢后在調用create方法。

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

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

下面是AlertDialog的部分源碼

public class AlertDialog extends Dialog implements DialogInterface {  // 這個對象用來承接builder內部所設置的參數  private AlertController mAlert;  //以下幾個構造方法決定只能通過內部類builder來構建  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());    }}

總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 田阳县| 赞皇县| 新邵县| 建水县| 洞口县| 塔河县| 会泽县| 同江市| 新化县| 中山市| 金堂县| 潜江市| 建湖县| 卓尼县| 云梦县| 泽州县| 汕头市| 黄大仙区| 青龙| 海门市| 探索| 抚顺县| 马山县| 塔城市| 南皮县| 金门县| 吴川市| 孟津县| 衡阳县| 兴安县| 沛县| 喀什市| 宁陕县| 那坡县| 吐鲁番市| 清远市| 增城市| 高唐县| 咸丰县| 霍邱县| 淮南市|