https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/builder/mr.simple

1. 模式介绍

模式的定义

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

模式的使用场景

  1. 相同的方法,不同的执行顺序,产生不同的事件结果时;
  2. 多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不相同时;
  3. 产品类非常复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建造者模式非常合适;

2. UML类图

角色介绍

  • Product 产品类 : 产品的抽象类。
  • Builder : 抽象类, 规范产品的组建,一般是由子类实现具体的组件过程。
  • ConcreteBuilder : 具体的构建器.
  • Director : 统一组装过程(可省略)。

3. 模式的简单实现

简单实现的介绍

电脑的组装过程较为复杂,步骤繁多,但是顺序却是不固定的。下面我们以组装电脑为例来演示一下简单且经典的builder模式。

实现源码

package com.dp.example.builder;

/**
* Computer产品抽象类, 为了例子简单, 只列出这几个属性
*
* @author mrsimple
*
*/
public abstract class Computer { protected int mCpuCore = 1;
protected int mRamSize = 0;
protected String mOs = "Dos"; protected Computer() { } // 设置CPU核心数
public abstract void setCPU(int core); // 设置内存
public abstract void setRAM(int gb); // 设置操作系统
public abstract void setOs(String os); @Override
public String toString() {
return "Computer [mCpuCore=" + mCpuCore + ", mRamSize=" + mRamSize
+ ", mOs=" + mOs + "]";
} } package com.dp.example.builder; /**
* Apple电脑
*/
public class AppleComputer extends Computer { protected AppleComputer() { } @Override
public void setCPU(int core) {
mCpuCore = core;
} @Override
public void setRAM(int gb) {
mRamSize = gb;
} @Override
public void setOs(String os) {
mOs = os;
} }
package com.dp.example.builder;

/**
* builder抽象类
*
*/
public abstract class Builder {
// 设置CPU核心数
public abstract void buildCPU(int core); // 设置内存
public abstract void buildRAM(int gb); // 设置操作系统
public abstract void buildOs(String os); // 创建Computer
public abstract Computer create(); } package com.dp.example.builder; public class ApplePCBuilder extends Builder {
private Computer mApplePc = new AppleComputer(); @Override
public void buildCPU(int core) {
mApplePc.setCPU(core);
} @Override
public void buildRAM(int gb) {
mApplePc.setRAM(gb);
} @Override
public void buildOs(String os) {
mApplePc.setOs(os);
} @Override
public Computer create() {
return mApplePc;
} } package com.dp.example.builder; public class Director {
Builder mBuilder = null; /**
*
* @param builder
*/
public Director(Builder builder) {
mBuilder = builder;
} /**
* 构建对象
*
* @param cpu
* @param ram
* @param os
*/
public void construct(int cpu, int ram, String os) {
mBuilder.buildCPU(cpu);
mBuilder.buildRAM(ram);
mBuilder.buildOs(os);
}
} /**
* 经典实现较为繁琐
*
* @author mrsimple
*
*/
public class Test {
public static void main(String[] args) {
// 构建器
Builder builder = new ApplePCBuilder();
// Director
Director pcDirector = new Director(builder);
// 封装构建过程, 4核, 内存2GB, Mac系统
pcDirector.construct(4, 2, "Mac OS X 10.9.1");
// 构建电脑, 输出相关信息
System.out.println("Computer Info : " + builder.create().toString());
}
}

通过Builder来构建产品对象, 而Director封装了构建复杂产品对象对象的过程,不对外隐藏构建细节。

Android源码中的模式实现

在Android源码中,我们最常用到的Builder模式就是AlertDialog.Builder, 使用该Builder来构建复杂的AlertDialog对象。简单示例如下 :

    //显示基本的AlertDialog
private void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.icon);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("Button1",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的Button1");
}
});
builder.setNeutralButton("Button2",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的Button2");
}
});
builder.setNegativeButton("Button3",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的Button3");
}
});
builder.create().show(); // 构建AlertDialog, 并且显示
}

结果 : 

下面我们看看AlertDialog的相关源码 :

// AlertDialog
public class AlertDialog extends Dialog implements DialogInterface {
// Controller, 接受Builder成员变量P中的各个参数
private AlertController mAlert; // 构造函数
protected AlertDialog(Context context, int theme) {
this(context, theme, true);
} // 4 : 构造AlertDialog
AlertDialog(Context context, int theme, boolean createContextWrapper) {
super(context, resolveDialogTheme(context, theme), createContextWrapper);
mWindow.alwaysReadCloseOnTouchAttr();
mAlert = new AlertController(getContext(), this, getWindow());
} // 实际上调用的是mAlert的setTitle方法
@Override
public void setTitle(CharSequence title) {
super.setTitle(title);
mAlert.setTitle(title);
} // 实际上调用的是mAlert的setCustomTitle方法
public void setCustomTitle(View customTitleView) {
mAlert.setCustomTitle(customTitleView);
} public void setMessage(CharSequence message) {
mAlert.setMessage(message);
} // AlertDialog其他的代码省略 // ************ Builder为AlertDialog的内部类 *******************
public static class Builder {
// 1 : 存储AlertDialog的各个参数, 例如title, message, icon等.
private final AlertController.AlertParams P;
// 属性省略 /**
* Constructor using a context for this builder and the {@link AlertDialog} it creates.
*/
public Builder(Context context) {
this(context, resolveDialogTheme(context, 0));
} public Builder(Context context, int theme) {
P = new AlertController.AlertParams(new ContextThemeWrapper(
context, resolveDialogTheme(context, theme)));
mTheme = theme;
} // Builder的其他代码省略 ...... // 2 : 设置各种参数
public Builder setTitle(CharSequence title) {
P.mTitle = title;
return this;
} public Builder setMessage(CharSequence message) {
P.mMessage = message;
return this;
} public Builder setIcon(int iconId) {
P.mIconId = iconId;
return this;
} public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
P.mPositiveButtonText = text;
P.mPositiveButtonListener = listener;
return this;
} public Builder setView(View view) {
P.mView = view;
P.mViewSpacingSpecified = false;
return this;
} // 3 : 构建AlertDialog, 传递参数
public AlertDialog create() {
// 调用new AlertDialog构造对象, 并且将参数传递个体AlertDialog
final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);
// 5 : 将P中的参数应用的dialog中的mAlert对象中
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
if (P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(P.mOnCancelListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}
} }

可以看到,通过Builder来设置AlertDialog中的title, message, button等参数, 这些参数都存储在类型为AlertController.AlertParams的成员变量P中,AlertController.AlertParams中包含了与之对应的成员变量。在调用Builder类的create函数时才创建AlertDialog, 并且将Builder成员变量P中保存的参数应用到AlertDialog的mAlert对象中,即P.apply(dialog.mAlert)代码段。我们看看apply函数的实现 :

        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) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
mPositiveButtonListener, null);
}
if (mNegativeButtonText != null) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
mNegativeButtonListener, null);
}
if (mNeutralButtonText != null) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText,
mNeutralButtonListener, null);
}
if (mForceInverseBackground) {
dialog.setInverseBackgroundForced(true);
}
// For a list, the client can either supply an array of items or an
// adapter or a cursor
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);
}
}
}

实际上就是把P中的参数挨个的设置到AlertController中, 也就是AlertDialog中的mAlert对象。从AlertDialog的各个setter方法中我们也可以看到,实际上也都是调用了mAlert对应的setter方法。在这里,Builder同时扮演了上文中提到的builder、ConcreteBuilder、Director的角色,简化了Builder模式的设计。

4. 杂谈

优点与缺点

优点

  • 良好的封装性, 使用建造者模式可以使客户端不必知道产品内部组成的细节;
  • 建造者独立,容易扩展;
  • 在对象创建过程中会使用到系统中的一些其它对象,这些对象在产品对象的创建过程中不易得到。

缺点

  • 会产生多余的Builder对象以及Director对象,消耗内存;
  • 对象的构建过程暴露。

Android设计模式源码解析之Builder模式的更多相关文章

  1. Android设计模式源码解析之桥接模式

    模式介绍 模式的定义 将抽象部分与实现部分分离,使它们都可以独立的变化. 模式的使用场景 如果一个系统需要在构件的抽象化角色和具体化角色之间添加更多的灵活性,避免在两个层次之间建立静态的联系. 设计要 ...

  2. Android设计模式源码解析之外观模式(Facade)

    https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/facade/elsd ...

  3. Android -- AsyncTask源码解析

    1,前段时间换工作的时候,关于AsyncTask源码这个点基本上大一点的公司都会问,所以今天就和大家一起来总结总结.本来早就想写这篇文章的,当时写<Android -- 从源码解析Handle+ ...

  4. Flink 源码解析 —— Standalone session 模式启动流程

    Standalone session 模式启动流程 https://t.zsxq.com/EemAEIi 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0 ...

  5. Android AsyncTask 源码解析

    1. 官方介绍 public abstract class AsyncTask extends Object  java.lang.Object    ↳ android.os.AsyncTask&l ...

  6. Android EventBus源码解析 带你深入理解EventBus

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...

  7. Android -- 从源码解析Handle+Looper+MessageQueue机制

    1,今天和大家一起从底层看看Handle的工作机制是什么样的,那么在引入之前我们先来了解Handle是用来干什么的 handler通俗一点讲就是用来在各个线程之间发送数据的处理对象.在任何线程中,只要 ...

  8. Android LayoutInflater源码解析:你真的能正确使用吗?

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 好久没写博客了,最近忙着换工作,没时间写,工作刚定下来.稍后有时间会写一下换工作经历.接下来进入本篇主题,本来没想写LayoutInflater的 ...

  9. Android HandlerThread源码解析

    在上一章Handler源码解析文章中,我们知道App的主线程通过Handler机制完成了一个线程的消息循环.那么我们自己也可以新建一个线程,在线程里面创建一个Looper,完成消息循环,可以做一些定时 ...

随机推荐

  1. cookiecutter-flask生成的框架里边自带了一个CRUDMixin类

    单元测试的必要性 之前曾经写过一篇讲单元测试的,正好最近也在实践和摸索.我似乎有种洁癖,就是我会严格遵守流程性的东西,比如测试,注释和文档等.目前就职的公司在我接手项目的时候是没有一行单元测试的,我挺 ...

  2. Hadoop配置文件解析

    Hadoop源码解析 2 --- Hadoop配置文件解析 1 Hadoop Configuration简介    Hadoop没有使用java.util.Properties管理配置文件, 也没有使 ...

  3. 【iHMI43 应用演示】之 modbus 协议(从机)通信演示

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  4. POI设置边框

    在做一个电子表格时,边框的设置有时是必不可少的.这一节就来介绍边框,设置时,可以指定边框的位置,边框的种类,边框的顔色. 首先是边框的位置和种类.对单元格设置边框时,有上下左右位置之分,所以POI也准 ...

  5. Apache Spark源码走读之1 -- Spark论文阅读笔记

    欢迎转载,转载请注明出处,徽沪一郎. 楔子 源码阅读是一件非常容易的事,也是一件非常难的事.容易的是代码就在那里,一打开就可以看到.难的是要通过代码明白作者当初为什么要这样设计,设计之初要解决的主要问 ...

  6. 判断返回数据是否为 null

    判断是否为null //尖括号                if ([regeocode.city isEqual:[NSNull class]]) {                    NSL ...

  7. ecshop中$user对象

    ecshop中$user对象 2011-10-01 18:41:23|  分类: ECSHOP |  标签:ecshop  $user  对象  |举报|字号 订阅 http://blog.163.c ...

  8. RedirectResult,RedirectToRoute

    RedirectResult:运行重新导向到其他网址,在RedirectResult的内部,基本上还是以Response.Redirect方法响应HTTP 302暂时导向. eg: public Ac ...

  9. useful tools, excel import mysql,swagger

    http://www.th7.cn/Program/java/201602/765943.shtml------swagger http://blog.csdn.net/csfreebird/arti ...

  10. mysql-zabbix-agent

    使用Zabbix监控MySQL服务器方法 01/27/2014 从Zabbix 2.2开始,Zabbix官方已经支持了MySQL监控,但是MySQL监控默认是不可用的,需要经过额外的设置才可以使用.K ...