深入理解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());
}
}
总结
猜你喜欢
- 今天,基基,跟大家分享一下,IntelliJ IDEA 2021 Win 和 Mac 快捷键大全,IDEA 基本所有功能都可以通过快捷键来完
- WebView是Android中一个非常实用的组件,它和Safai、Chrome一样都是基于Webkit网页渲染引擎,可以通过加载HTML数
- C#Process OutputDataReceived事件不触发问题描述项目需要用cmd调用其它软件,实时获取软件处理结果,并根据获取到的
- 本文实例讲述了C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法。分享给大家供大家参考,具体如下:Windo
- final,static,this,super 关键字总结正文开始@Assassin1. final 关键字:final 关键字,意思是最终
- 1.情景展示将要访问的接口地址等常用的配置添加到properties文件中,比直接写到java类中的好处在于:当我们需要修改相应配置时,直接
- 前言:事情是这样的:运营人员反馈,通过Excel导入数据时,有一部分成功了,有一部分未导入。初步猜测,是事务未生效导致的。查看代码,发现导入
- 一、 四种修饰符的说明public修饰符表示 公有 。此修饰符的范围最大。当不声明任何修饰符时,系统会默认使用此修饰符。internal修饰
- 案例:当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Lu
- 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很
- 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条。数据保持最新:设计字段sign的值(0,1)
- 本文实例讲述了C#双向链表LinkedList排序实现方法。分享给大家供大家参考。具体如下:1.函数打印链表函数PrintLinkedLis
- 本文介绍了详解Android中PopupWindow在7.0后适配的解决,分享给大家,具体如下:这里主要记录一次踩坑的经历。需求:如上图左侧
- 为什么要限流在保证可用的情况下尽可能多增加进入的人数,其余的人在排队等待,或者返回友好提示,保证里面的进行系统的用户可以正常使用,防止系统雪
- 本文实例为大家分享了opencv利用视频的前n帧求平均图像的具体代码,供大家参考,具体内容如下自己写的哈,可以用该小程序对视频求解平均模型。
- 一 引入项目有个新需求,当点击或触碰TextBox时,基于TextBox的相对位置,弹出一个自定义的Keyboard,如下图所示:二 Key
- 本文分享的示例代码实现提取2个集合中相同和不同的元素此处需要使用Collection集合所提供的一个方法:removeAll(Cellect
- 目录一、简介二、环境介绍三、主题1. ThemeData2. main.dart or MaterialApp四、全局配置1. Global
- 1. JSCH简介JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可
- 本文实例讲述了C#精确计算年龄的方法。分享给大家供大家参考。具体如下:该源码在vs2010测试通过using System;using Sy