什么是Buidler模式呢?就是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构建它们.

那么要为何使用Buidler呢?

是为了将构建复杂对象的过程和它的部件分开因为一个复杂的对象,不但有很多大量组成部分,如AlertDialog对话框,有很多组成部件,比如Tittle,Message,icon,PositiveButton等等,但远不止这些,如何将这些部件装配成一个AlertDialog对话框呢,这个装配程可能也是一个很复杂的步骤,Builder模式就是为了将部件和组装过程分开。通俗点说,就是我先分开生产好各个组件,然后交由另一个类去组装这些组件。

如何使用?

我们来看一下Android内部关于AlertDialog.Builder的源代码便可以知晓。

public class AlertDialog extends Dialog implements DialogInterface {  // Controller, 接受Builder成员变量P中的各个参数  private AlertController mAlert;  // 构造函数  protected AlertDialog(Context context, int theme) {  this(context, theme, true);  }  // 4 : 构造AlertDialog  AlertDialog(Context context, int theme, boolean createContextWrapper) {  super(context, resolveDialogTheme(context, theme), createContextWrapper);  mWindow.alwaysReadCloseOnTouchAttr();  mAlert = new AlertController(getContext(), this, getWindow());  }  // 实际上调用的是mAlert的setTitle方法  @Override  public void setTitle(CharSequence title) {  super.setTitle(title);  mAlert.setTitle(title);  }  // 实际上调用的是mAlert的setCustomTitle方法  public void setCustomTitle(View customTitleView) {  mAlert.setCustomTitle(customTitleView);  }  public void setMessage(CharSequence message) {  mAlert.setMessage(message);  }  // AlertDialog其他的代码省略  // ************  Builder为AlertDialog的内部类   *******************  public static class Builder {  // 1 :该类用来存储AlertDialog的各个参数, 例如title, message, 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, resolveDialogTheme(context, 0));  }  public Builder(Context context, int theme) {  P = new AlertController.AlertParams(new ContextThemeWrapper(  context, resolveDialogTheme(context, theme)));  mTheme = theme;  }  // 2:设置各种参数到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 iconId) {  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 : 构建AlertDialog, 传递参数  public AlertDialog create() {  // 调用new AlertDialog构造对象, 并且将参数传递个体AlertDialog  final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);  // 5 : 将P中的参数应用的dialog中的mAlert对象中  //这一步是核心方法我们等下看源码继续讲  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 AlertDialog show() {  //6:显示dialog  AlertDialog dialog = create();  dialog.show();  return dialog;  }  }  }

从上面的源码中我们可以看到,对话框的构建是通过Builder来设置AlertDialog中的title, message, button等参数, 这些参数都存储在类型为AlertController.AlertParams的成员变量P中,AlertController.AlertParams中包含了与之对应的成员变量。在调用Builder类的create函数时才创建AlertDialog, 并且将Builder成员变量P中保存的参数应用到AlertDialog的mAlert对象中,最后调用dialog.show方法显示对话框。

现在我们再来看看即P.apply(dialog.mAlert)代码段。我们看看apply函数的实现 。

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);  }  }
}

实际上就是把P中的参数挨个的设置到AlertController中, 也就是AlertDialog中的mAlert对象。从AlertDialog的各个setter方法中我们也可以看到,实际上也都是调用了mAlert对应的setter方法。
       综上看完上面源码之后我们就可以发现,怪不得我们平时调用对话框的时候可以直接使用,AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);不用Alert.Builder方法创建也可以,因为其本质是一样的,Builder只是把组件的生产过程化成一步步实行而已。

这样做有什么实际作用呢?

在Java实际使用中,我们经常用到"池"(Pool)的概念,当资源提供者无法提供足够的资源,并且这些资源需要被很多用户反复共享时,就需要使用池。"池"实际是一段内存,当池中有一些复杂的资源的"断肢"(比如数据库的连接池,也许有时一个连接会中断),如果循环再利用这些"断肢",将提高内存使用效率,提高池的性能,而在这里AlertDialog.builder就是这个池,修改Builder模式中p.apply(组装)类使之能诊断"断肢"断在哪个部件上,再修复这个部件.

Android开发之Buidler模式初探结合AlertDialog.Builder讲解相关推荐

  1. Android开发之Buidler模式初探结合AlertDialog.Builder解说

          什么是Buidler模式呢?就是将一个复杂对象的构建与它的表示分离,使得相同的构建过程能够创建不同的表示.Builder模式是一步一步创建一个复杂的对象,它同意用户能够仅仅通过指定复杂对象 ...

  2. Android开发之MVVM模式实践(六),太现实了

    Interface interface FlyInterface { /** 获取文章列表 */ @GET("article/") suspend fun get_article_ ...

  3. Android开发之MVVM模式实践(六)

    Interface interface FlyInterface { /** 获取文章列表 */ @GET("article/") suspend fun get_article_ ...

  4. Android开发之MVVM模式实践(六),2021字节跳动春招技术面试题

    以上是我们创建协程的实现方式,我们可以通过指定Dispatchers来决定协程到底在什么线程中工作,而其实Kotlin的协程核心库中也为我们提供封装好了的scope,例如MainScope,源码如下: ...

  5. Android开发之MVVM模式实践:协程与网络请求的结合

    前言 大家好,我是小益!在经过前两章对协程的介绍后,我们终于又回到了 MVVM的封装 .协程在Android开发中最常用的场景应该是网络请求了,其次是一些使用 Thread 的场景,本章内容我们将着重 ...

  6. android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序   在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity, ...

  7. Android开发之旅:组件生命周期(二)

    引言 应用程序组件有一个生命周期--一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激活状态:对于活动,对用户有时候可见,有时候不可见.组件生 ...

  8. Android开发之2048安卓版

    之前是在eclipse上写的,后面换成了android sudio. 2048游戏的UI整体可以采用线性布局,即LinearLayout,其中嵌套一个线性布局和一个GridLayout,内嵌的线性布局 ...

  9. Android 开发之Windows环境下Android Studio安装和使用教程(图文详细步骤)

    鉴于谷歌最新推出的Android Studio备受开发者的推崇,所以也跟着体验一下. 一.介绍Android Studio  Android Studio 是一个Android开发环境,基于Intel ...

最新文章

  1. 【Qt】通过QtCreator源码学习Qt(九):容器算法总结
  2. 如何设计一个高可用系统?要考虑哪些地方?
  3. XPE一般性组件整理
  4. Zabbix实战之客户端自动注册
  5. 如何为SFP光模块搭配对应的光纤跳线?
  6. c++ 出现1.#IND、1.#INF
  7. ubuntu开启客户端nfs服务_LINUX系统使用NFS文件共享
  8. Struts2框架完成登录操作案例
  9. 第二章 信息的表示和处理
  10. 微信小程序|开发实战篇之九-image-picker图片选择器组件及其子组件
  11. python除了爬虫还可以干什么_python爬虫能够干什么
  12. 零元学Expression Blend 4 - Chapter 9 用实例了解布局容器系列-「Canvas」
  13. [Linux] 使用 SCP 指令,讓您傳送檔案至遠端、下載檔案
  14. 【机器学习算法实现】主成分分析(PCA)——基于python+numpy
  15. 微信小程序生成海报库
  16. php计算指数函数,指数函数运算法则公式有哪些
  17. Python 操作 Word
  18. 美团开放平台品牌商接口对接步骤(Java)
  19. 习题9-3 平面向量加法 (15 分)
  20. Cinemachine 镜头异常抖动(画面抽搐twitch)

热门文章

  1. java 传参字符串数组_JAVA语言之okhttp传递数组参数
  2. cdn托管html资源,七牛自定义域名cdn加速,静态资源托管至对象存储
  3. Abbirb120型工业机器人_工业机器人市场深度调研及投资前景预测报告2020-2024年
  4. android sco通信,android – startBluetoothSco()在ICS上抛出安全异常(BROADCAST_STICKY)
  5. windows 远程访问别人的linux下的ftp服务器,linux与windows之间使用ftp相互访问(CentOS提示ftp:command not found)...
  6. java frame paint_一个简单的java frame画图(paint)问题
  7. java中怎样存储遍历的数据_【数据算法】Java实现二叉树存储以及遍历
  8. labview波形图两个游标,LabVIEW数据可视化:使用波形图表控件逐点显示曲线的方法...
  9. 如何给6个整数的一维数组某个元素赋值_数组指针详解
  10. SpringCloud系列十二:SpringCloudSleuth(SpringCloudSleuth 简介、SpringCloudSleuth 基本配置、数据采集)...