Builder 模式 一个比较常用的创建型设计模式,中文翻译为建造者模式 或者构建者模式 ,也有人叫它生成器模式 。
1 为什么需要建造者模式? 举个栗子🌰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class ResourcePoolConfig { private static final int DEFAULT_MAX_TOTAL = 8 ; private static final int DEFAULT_MAX_IDLE = 8 ; private static final int DEFAULT_MIN_IDLE = 0 ; private String name; private int maxTotal = DEFAULT_MAX_TOTAL; private int maxIdle = DEFAULT_MAX_IDLE; private int minIdle = DEFAULT_MIN_IDLE; public ResourcePoolConfig (String name, Integer maxTotal, Integer maxIdle, Integer minIdle) { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("name should not be empty." ); } this .name = name; if (maxTotal != null ) { if (maxTotal <= 0 ) { throw new IllegalArgumentException("maxTotal should be positive." ); } this .maxTotal = maxTotal; } if (maxIdle != null ) { if (maxIdle < 0 ) { throw new IllegalArgumentException("maxIdle should not be negative." ); } this .maxIdle = maxIdle; } if (minIdle != null ) { if (minIdle < 0 ) { throw new IllegalArgumentException("minIdle should not be negative." ); } this .minIdle = minIdle; } } }
这是构建一个对象最常见、最容易想到的实现思路,因为 maxTotal、maxIdle、minIdle 不是必填变量,所以在创建 ResourcePoolConfig 对象的时候,我们通过往构造函数中,给这几个参数传递 null 值,来表示使用默认值。
但是,如果可配置项逐渐增多,变成了 8 个、10 个,甚至更多,那继续沿用现在的设计思路,构造函数的参数列表会变得很长,代码在可读性和易用性上都会变差。 在使用构造函数的时候,我们就容易搞错各参数的顺序,传递进错误的参数值,导致非常隐蔽的 bug。
1 2 ResourcePoolConfig config = new ResourcePoolConfig("dbconnectionpool" , 16 , null , 8 , null , false , true , 10 , 20 ,false , true );
解决这种问题,我们可以用 set() 函数来给成员变量赋值,以替代冗长的构造函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class ResourcePoolConfig { private static final int DEFAULT_MAX_TOTAL = 8 ; private static final int DEFAULT_MAX_IDLE = 8 ; private static final int DEFAULT_MIN_IDLE = 0 ; private String name; private int maxTotal = DEFAULT_MAX_TOTAL; private int maxIdle = DEFAULT_MAX_IDLE; private int minIdle = DEFAULT_MIN_IDLE; public ResourcePoolConfig (String name) { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("name should not be empty." ); } this .name = name; } public void setMaxTotal (int maxTotal) { if (maxTotal <= 0 ) { throw new IllegalArgumentException("maxTotal should be positive." ); } this .maxTotal = maxTotal; } public void setMaxIdle (int maxIdle) { if (maxIdle < 0 ) { throw new IllegalArgumentException("maxIdle should not be negative." ); } this .maxIdle = maxIdle; } public void setMinIdle (int minIdle) { if (minIdle < 0 ) { throw new IllegalArgumentException("minIdle should not be negative." ); } this .minIdle = minIdle; } }
name 是必填项,所有还就在了构造函数里。
1 2 3 4 ResourcePoolConfig config = new ResourcePoolConfig("dbconnectionpool" ); config.setMaxTotal(16 ); config.setMaxIdle(8 );
没有了冗长的函数调用和参数列表,代码在可读性和易用性上提高了很多。
但是还有几个问题:
name 是必填的,所以,我们把它放到构造函数中,强制创建对象的时候就设置。如果必填的配置项有很多,把这些必填配置项都放到构造函数中设置,那构造函数就又会出现参数列表很长的问题。如果我们把必填项也通过 set() 方法设置,那校验这些必填项是否已经填写的逻辑就无处安放了。
如果属性之间有依赖关系,比如,如果用户设置了 maxTotal、maxIdle、minIdle 其中一个,就必须显式地设置另外两个;或者配置项之间有一定的约束条件,比如,maxIdle 和 minIdle 要小于等于 maxTotal。如果我们继续使用现在的设计思路,那这些配置项之间的依赖关系或者约束条件的校验逻辑就无处安放了。
如果我们希望 ResourcePoolConfig 类对象是不可变对象,也就是说,对象在创建好之后,就不能再修改内部的属性值。要实现这个功能,我们就不能在 ResourcePoolConfig 类中暴露 set() 方法。
现在建造者模式就派上用场了。 🗼🗼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 public class ResourcePoolConfig { private String name; private int maxTotal; private int maxIdle; private int minIdle; private ResourcePoolConfig (Builder builder) { this .name = builder.name; this .maxTotal = builder.maxTotal; this .maxIdle = builder.maxIdle; this .minIdle = builder.minIdle; } public static class Builder { private static final int DEFAULT_MAX_TOTAL = 8 ; private static final int DEFAULT_MAX_IDLE = 8 ; private static final int DEFAULT_MIN_IDLE = 0 ; private String name; private int maxTotal = DEFAULT_MAX_TOTAL; private int maxIdle = DEFAULT_MAX_IDLE; private int minIdle = DEFAULT_MIN_IDLE; public ResourcePoolConfig build () { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("..." ); } if (maxIdle > maxTotal) { throw new IllegalArgumentException("..." ); } if (minIdle > maxTotal || minIdle > maxIdle) { throw new IllegalArgumentException("..." ); } return new ResourcePoolConfig(this ); } public Builder setName (String name) { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("..." ); } this .name = name; return this ; } public Builder setMaxTotal (int maxTotal) { if (maxTotal <= 0 ) { throw new IllegalArgumentException("..." ); } this .maxTotal = maxTotal; return this ; } public Builder setMaxIdle (int maxIdle) { if (maxIdle < 0 ) { throw new IllegalArgumentException("..." ); } this .maxIdle = maxIdle; return this ; } public Builder setMinIdle (int minIdle) { if (minIdle < 0 ) { throw new IllegalArgumentException("..." ); } this .minIdle = minIdle; return this ; } } } ResourcePoolConfig config = new ResourcePoolConfig.Builder() .setName("dbconnectionpool" ) .setMaxTotal(16 ) .setMaxIdle(10 ) .setMinIdle(12 ) .build();
这样就解决了上面三个问题。使用建造者模式创建对象,还能避免对象存在无效状态。比如我们定义了一个长方形类,如果不使用建造者模式,采用先创建后 set 的方式,那就会导致在第一个 set 之后,对象处于无效状态。
1 2 3 Rectangle r = new Rectange(); r.setWidth(2 ); r.setHeight(3 );
为了避免这种无效状态的存在,我们就需要使用构造函数一次性初始化好所有的成员变量。如果构造函数参数过多,我们就需要考虑使用建造者模式,先设置建造者的变量,然后再一次性地创建对象,让对象一直处于有效状态。
2 与工厂模式有何区别? 建造者模式是让建造者类来负责对象的创建工作。工厂模式,是由工厂类来负责对象创建的工作。那它们之间有什么区别呢?
工厂模式是用来创建不同但是相关类型的对象(继承同一父类或者接口的一组子类),由给定的参数来决定创建哪种类型的对象。 建造者模式是用来创建一种类型的复杂对象,通过设置不同的可选参数,“定制化”地创建不同的对象。
网上有一个经典的例子很好地解释了两者的区别。顾客走进一家餐馆点餐,我们利用工厂模式,根据用户不同的选择,来制作不同的食物,比如披萨、汉堡、沙拉。对于披萨来说,用户又有各种配料可以定制,比如奶酪、西红柿、起司,我们通过建造者模式根据用户选择的不同配料来制作披萨。
3 Android 源码中的建造者模式 在 Android 源码中,最常用到的 Builder 模式就是 AlertDialog.Builder,使用该 Builder 来构建复杂的 AlertDialog 对象。在开发过程中,我们经常用到 AlertDialog,具体示例如下:
1 2 3 4 5 6 7 8 9 builder.setIcon(R.mipmap.ic_launcher) .setTitle("Title" ) .setMessage("Message" ) .setPositiveButton("Button1" , new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { } }).create().show();
从类名就可以看出这就是一个 Builder 模式,通过 Builder 对象来组装Dialog 的各个部分,如 title 、buttons 、Message 等,将 Dialog 的构造和表示进行分离。
AlertDialog 的相关源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public class AlertDialog extends Dialog implements DialogInterface { private AlertController mAlert; AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) { super (context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0 ,createContextThemeWrapper); mWindow.alwaysReadCloseOnTouchAttr(); mAlert = AlertController.create(getContext(), this , getWindow()); } @Override public void setTitle (CharSequence title) { super .setTitle(title); mAlert.setTitle(title); } public void setMessage (CharSequence message) { mAlert.setMessage(message); } public static class Builder { private final AlertController.AlertParams P; public Builder setTitle (@StringRes int titleId) { P.mTitle = P.mContext.getText(titleId); return this ; } public Builder setTitle (CharSequence title) { P.mTitle = title; return this ; } public AlertDialog create () { 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; } } }
上述代码中,Builder 类可以设置 AlertDialog 中的 title 、message 、button 等参数,这些参数都存储在类型为 AlertController.AlertParams 的成员变量 P 中,AlertController.AlertParams 中包含了与 AlertDialog 视图中对应的成员变量。在调用 Builder 类的 create 函数时会创建AlertDialog ,并且将 Builder 成员变量P中保存的参数应用到 AlertDialog 的 mAlert 对象中,即 P.apply(dialog.mAlert) 代码段。
apply 函数的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 || mPositiveButtonIcon != null ) { dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText, mPositiveButtonListener, null , mPositiveButtonIcon); } if (mNegativeButtonText != null || mNegativeButtonIcon != null ) { dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText, mNegativeButtonListener, null , mNegativeButtonIcon); } if (mNeutralButtonText != null || mNeutralButtonIcon != null ) { dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText, mNeutralButtonListener, null , mNeutralButtonIcon); } 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); } } else if (mViewLayoutResId != 0 ) { dialog.setView(mViewLayoutResId); } }
在 apply 函数中,只是将 AlertParams 参数设置到 AlertController 中,例如,将标题设置到 Dialog 对应的标题视图中,将 Message 设置到内容视图中等。当我们获取到 AlertDialog 对象后,通过 show 函数就可以显示这个对话框。 Dialog 的 show 函数(该函数在 Dialog 类中):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public void show () { if (mShowing) { if (mDecor != null ) { if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR); } mDecor.setVisibility(View.VISIBLE); } return ; } mCanceled = false ; if (!mCreated) { dispatchOnCreate(null ); } else { final Configuration config = mContext.getResources().getConfiguration(); mWindow.getDecorView().dispatchConfigurationChanged(config); } onStart(); mDecor = mWindow.getDecorView(); if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { final ApplicationInfo info = mContext.getApplicationInfo(); mWindow.setDefaultIcon(info.icon); mWindow.setDefaultLogo(info.logo); mActionBar = new WindowDecorActionBar(this ); } WindowManager.LayoutParams l = mWindow.getAttributes(); boolean restoreSoftInputMode = false ; if ((l.softInputMode & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0 ) { l.softInputMode |= WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; restoreSoftInputMode = true ; } mWindowManager.addView(mDecor, l); if (restoreSoftInputMode) { l.softInputMode &= ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; } mShowing = true ; sendShowMessage(); }
在 show 函数中主要做了如下几个事情:
通过 dispatchOnCreate 函数来调用 AlertDialog 的 onCreate 函数;
然后调用 AlertDialog 的 onStart 函数;
最后将 Dialog 的 DecorView 添加到 WindowManager 中。
很明显,这就是一系列典型的生命周期函数。那么按照惯例,AlertDialog 的内容视图构建按理应该在 onCreate 函数中;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); mAlert.installContent(); } AlertController.java public void installContent () { final int contentView = selectContentView(); mDialog.setContentView(contentView); setupView(); } AppCompatDialog.java @Override public void setContentView (@LayoutRes int layoutResID) { getDelegate().setContentView(layoutResID); }
这里就和 Activity 中的 setContentView 逻辑一样了。
setContentView 后续 Android setContentView源码解析
感谢
设计模式之美
《Android 源码设计模式解析与实战》
以及上文中的链接