亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言

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

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

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

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

應(yīng)用場景

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

android中的AlertDialog對話框的構(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;
  }


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

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

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

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

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

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

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

下面是AlertDialog的部分源碼

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

  //以下幾個構(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)容對各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • android中貝塞爾曲線的應(yīng)用示例

    android中貝塞爾曲線的應(yīng)用示例

    本篇文章主要介紹了android中貝塞爾曲線的應(yīng)用示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Kotlin與Java相互調(diào)用的完整實例

    Kotlin與Java相互調(diào)用的完整實例

    Kotlin的設(shè)計過程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過來在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • Android 圖片處理避免出現(xiàn)oom的方法詳解

    Android 圖片處理避免出現(xiàn)oom的方法詳解

    本篇文章主要介紹了Android 圖片處理避免出現(xiàn)oom的方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android 模擬器(emulator-5554...)出現(xiàn)錯誤解決辦法

    Android 模擬器(emulator-5554...)出現(xiàn)錯誤解決辦法

    這篇文章主要介紹了Android 模擬器出現(xiàn)錯誤解決辦法的相關(guān)資料,如:Unable to get view server version from device,F(xiàn)ailed to install helloworld.apk on device 'emulator-5554': timeout,這種常見錯誤,解決辦法,需要的朋友可以參考下
    2016-11-11
  • Android提高之手游轉(zhuǎn)電視游戲的模擬操控

    Android提高之手游轉(zhuǎn)電視游戲的模擬操控

    這篇文章主要介紹了Android手游轉(zhuǎn)電視游戲的模擬操控方法,是非常具有實用價值的功能,需要的朋友可以參考下
    2014-08-08
  • 通過案例分析Android WindowManager解析與騙取QQ密碼的過程

    通過案例分析Android WindowManager解析與騙取QQ密碼的過程

    Windows Manager是一款窗口管理終端,可以遠(yuǎn)程連接到Linux的X桌面進(jìn)行管理,與服務(wù)器端產(chǎn)生一個session相互通信,通過本文給大家分享Android WindowManager解析與騙取QQ密碼的過程,需要的朋友參考下
    2016-01-01
  • Android 顯示和隱藏輸入法實現(xiàn)代碼

    Android 顯示和隱藏輸入法實現(xiàn)代碼

    本文所要介紹的這個方法可以轉(zhuǎn)換軟件輸入法在窗體中的顯示狀態(tài),具體實現(xiàn)代碼如下,感興趣的你可以參考下哈,希望可以幫助到你
    2013-03-03
  • Android添加指紋解鎖功能的實現(xiàn)代碼

    Android添加指紋解鎖功能的實現(xiàn)代碼

    當(dāng)開發(fā)的APP需要加密驗證時可以考慮添加系統(tǒng)指紋解鎖功能。這篇文章主要介紹了Android添加指紋解鎖功能的實現(xiàn)代碼,需要的朋友可以參考下
    2018-07-07
  • Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例

    Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能示例

    這篇文章主要介紹了Android開發(fā)使用json實現(xiàn)服務(wù)器與客戶端數(shù)據(jù)的交互功能,結(jié)合具體實例形式分析了Android使用json格式數(shù)據(jù)在服務(wù)器與客戶端傳遞實現(xiàn)數(shù)據(jù)庫查詢功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android Map新用法:MapFragment應(yīng)用介紹

    Android Map新用法:MapFragment應(yīng)用介紹

    MapView ,MapActivity 這種的局限在于,必須要繼承MapActivity,否則無法使用MapView,但是,MapFragment 這種的局限在于,必須要安裝Google Play Service ,也就是說必須是原生rom。而且sdk要在12以上
    2013-01-01

最新評論