盘点android中常见的设计模式(二) -- Builder模式/建造者模式

简介:

设计模式的类型

创建型模式:

这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用new运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。

常见的这类设计模式有工厂模式、抽象工厂模式、单例模式、建造者模式、原型模式

结构性模式:

这些设计模式关注和对象的组合,继承的概念被用来组合接口和定义组合对象获得新功能的方式。

常见的这类设计模式有适配器模式、桥接模式、过滤器模式、组合模式、装饰器模式、代理模式等。

行为型模式:

这些设计模式特别关注对象之间的通信和对象的行为。

常见的这类设计模式有责任链模式、解释器模式、迭代器模式、中介者模式、观察者模式、访问者模式等。

Builder模式/建造者模式:

建造者模式使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。一个Builder类会一步一步构造最终的对象,该Builder类是独立于其他对象的。

Android中的使用:

builder模式在android中的使用场景还是挺多了,常见的就有AlertDialog的创建过程。

        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("title")
                .setCancelable(true)
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("this is a message");
        builder.create().show();

为了更好地分析builder模式的使用,接下来我们可以分析下android系统的开发者是如何运用的。以AlertDialog为例,首先我们可以看到这里的builder是AlertDialog的一个静态内部类。

public class AlertDialog extends AppCompatDialog implements DialogInterface {

    ......

    public static class Builder {
        private final AlertController.AlertParams P;
        private final int mTheme;


        public Builder(@NonNull Context context) {
            this(context, resolveDialogTheme(context, 0));
        }

        public Builder(@NonNull Context context, @StyleRes int themeResId) {
            P = new AlertController.AlertParams(new ContextThemeWrapper(
                    context, resolveDialogTheme(context, themeResId)));
            mTheme = themeResId;
        }


        @NonNull
        public Context getContext() {
            return P.mContext;
        }

        ......

        public Builder setTitle(@Nullable CharSequence title) {
            P.mTitle = title;
            return this;
        }


        public Builder setMessage(@Nullable CharSequence message) {
            P.mMessage = message;
            return this;
        }


        public Builder setIcon(@Nullable Drawable icon) {
            P.mIcon = icon;
            return this;
        }
        
        ......

        public AlertDialog create() {
            // We can't use Dialog's 3-arg constructor with the createThemeContextWrapper param,
            // so we always have to re-set the theme
            final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
            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;
        }


        public AlertDialog show() {
            final AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }

}

可以看到这里的builder在初始化构造函数中创建了一个AlertController.AlertParams对象,并持有了它的引用p。而这个对象就是用来管理AlertDialog创建时各个复杂的属性的。比如builder中的setTitle方法就是对p.mTitle属性进行赋值,其他的属性也是如此,然后在返回自身this,使之可以形成链式调用。最后通过builder.create()方法创建出AlertDialog对象并将p中的属性应用给AlertDialog。至此AlertDialog这个复杂的创建过程就通过简单的链式调用完成了。而我们用的时候也不需要对所有的属性进行赋值,只需要关心用的到的即可。

发布了10 篇原创文章 · 获赞 3 · 访问量 500

猜你喜欢

转载自blog.csdn.net/ledding/article/details/104284867