Android 中的建造者模式

作者: smart_dev | 来源:发表于2015-12-16 15:58 被阅读13753次

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对话框的构建过程就是建造者模式的典型应用。

UML

看一下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());
        }
}

相关文章

  • 建造者模式(部件构造)

    目录 建造者模式的理念 从 POJO 到建造者模式的思考 怎么来实现建造者模式 建造者模式在Android源码中的...

  • Android 中的建造者模式

    Android 中的建造者模式 概述 建造者模式(Builder Pattern)也叫生成器模式,其定义如下:se...

  • 建造者模式

    Android中建造者模式使用最普遍的就是Dialog。下面这个Dialog采用建造者模式,富文本等。 1.下面进...

  • Builder Pattern in Java

    建造者模式:建造者模式定义建造者模式应用场景实现案例Jdk中的建造者模式建造者模式的优点建造者模式的缺点 建造者模...

  • android中常见的设计模式有哪些?

    建造者模式 建造者模式最明显的标志就是Build类,而在Android中最常用的就是Dialog的构建,Notif...

  • 建造者模式

    建造者模式 首先,建造者模式的封装性很好。使用建造者模式可以有效的封装变化,在使用建造者模式的场景中,一般产品类和...

  • 一、设计模式(构建模式)——03建造模式与原型模式

    建造者模式 建造型模式用于创建过程稳定,但配置多变的对象。 建造模式的实现要点: 在建造者模式中,指挥者是直接与客...

  • Builder pattern

    Builder pattern 这里所介绍的建造者模式不是GOF中介绍的建造者模式。GOF中的建造者模式主要用于抽...

  • 设计模式之builder模式

    建造者模式也叫生成器模式,和抽象工厂模式相似,也是一种构建复杂对象的模式。 建造者模式中的角色分类: 抽象建造者...

  • Android -建造者模式

    今天我们来聊一聊我们经常用到的模式-建造者模式(Builder Pattern). 一: 1.什么是建造者模式?...

网友评论

本文标题:Android 中的建造者模式

本文链接:https://www.haomeiwen.com/subject/mstphttx.html