深入理解Android中的建造者模式
作者:daisy 发布时间:2023-06-27 05:55:17
前言
在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());
}
}
总结


猜你喜欢
- this template depends on the android support library,which is either n
- IDEA配置maven环境一、配置maven本地环境先参照以下博客进行maven的安装,配置IDEA 如何搭建maven 安装、下载、配置(
- 1、spring-cloud-starter-alibaba-nacos-discovery 这里依赖报红,无法引入,或显示无法找到,更换版
- 案例:图书管理(SpringBoot+Thymeleaf+SpringData-JPA)添加图书:图书基本信息及封面图片的上传及入库图书详细
- 本文实例讲述了Java实现接口的枚举类。分享给大家供大家参考,具体如下:一 点睛枚举类也可以实现一个或多个接口。与普通类实现一个或多个接口完
- 图片转换成字节流先要转换的IMage对象,转换之后返回字节流。字节流转换成图片,要转换的字节流,转换得到的Image对象,根据图片路径返回图
- 最近发现Java原生的Zip压缩组件在压缩过程中,不支持文件名的中文编码,会在压缩过程中把中文文件名变成乱码。Apache的ant包中的压缩
- 本文实例讲述了C#数字图像处理之图像二值化(彩色变黑白)的方法。分享给大家供大家参考。具体如下://定义图像二值化函数private sta
- 本文实例为大家分享了java封装前端查询条件的具体代码,供大家参考,具体内容如下import hengyi.oa.mobile.except
- 引言声明:文中的MPChart代指MPAndroidChart.本文主要讲解LineChart中的三个变种Chart,第一个是渐变的Line
- 1、maven引入quartz包<!-- https://mvnrepository.com/artifact/org.quartz-
- 很多Android手机上都配有LED灯,比如HTC的手机在充电、新来短信等时候都会有响应的指示,其实很简单的这都是NotificationM
- Java 判断字符串中是否包含中文的实例详解 Java判断一个字符串是否有中文是利用Unicode编码来判断,因为中
- 每天上下楼都是乘坐电梯的,就想电梯的工作原理是什么呢?于是自己写了个控制台程序来模拟一下电梯的工作原理!采用面向对象的编程思想!将电梯拆解为
- 前言C# 时间戳与 标准时间的转其实不难,但需要注意下,基准时间的问题。格林威治时间起点: 1970 年 1 月 1 日的 00:00:00
- 1、数组数组的引用传递public class TestDemo1{public static void main(String args[
- 在android support.v4 中有一个抽屉视图控件DrawerLayout。使用这个控件,可以生成通过在屏幕上水平滑动打开或者关闭
- 历史原因当系统启动一个APP时,zygote进程会首先创建一个新的进程去运行这个APP,但是进程的创建是需要时间的,在创建完成之前,界面是呈
- Android UI线程是不安全的,子线程中进行UI操作,可能会导致程序的崩溃,解决办法:创建一个Message对象,然后借助Handler
- 1.效果图如下:2.controller层代码:import java.util.HashMap;import java.util.Map;