builder设计模式适用于什么意思

构建者(Builder)设计模式(又叫生成器设计模式):
当一个类的内部数据过于复杂的时候(通常是负责持有数据的类,比如Config、VO、PO、Entity...),要创建的话可能就需要了解这个类的内部结构,还有这些东西是怎么组织装配等一大坨乱七八糟的东西,这个时候就会增加学习成本而且会很混乱,这个时候就想啊想一种什么法子来管理一下这个类中的数据呢,怎么在创建的时候让它按部就班的来,并且代码可读性很好别让我看花了眼啊,我要的东西也能都很好设置进来,这就是Builder模式的应用场景,Builder模式可以将一个类的构建和表示进行分离。
看一个例子:
public class Student {
private int
// 构造器尽量缩小范围
private Student() {
// 构造器尽量缩小范围
private Student(Student origin) {
// 拷贝一份
this.id = origin.
this.name = origin.
this.passwd = origin.
this.sex = origin.
this.address = origin.
public int getId() {
public String getName() {
public String getPasswd() {
public String getSex() {
public String getAddress() {
* Student的创建完全依靠Student.Builder,使用一种方法链的方式来创建
public static class Builder {
public Builder() {
target = new Student();
public Builder address(int id) {
target.id =
return this;
public Builder name(String name) {
target.name =
return this;
public Builder password(String passwd) {
target.passwd =
return this;
public Builder sex(String sex) {
target.sex =
return this;
public Builder address(String address) {
target.address =
return this;
public Student build() {
return new Student(target);
Student并不是直接new出来的,对其构造器进行了处理使其可访问范围尽可能的小,只让它通过Student.Builder来构建自己,在Student.Builder中提供了一种类set的方法链的方式来设置值,然后在最后的build()方法的时候会返回一个Student对象,现在要创建一个Student对象,代码如下:
Student s=new Student.Builder().name("CC").password("qwerty").sex("男").address("银河系第二旋臂").build();
再对比一下如果不使用构造者模式(一般情况下的用法):
* 学生实体
* @author CC
public class Student {
private int
public Student() {
public Student(String name, String passwd, String sex, String address) {
this.name =
this.passwd =
this.sex =
this.address =
public int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
public String getPasswd() {
public void setPasswd(String passwd) {
this.passwd =
public String getSex() {
public void setSex(String sex) {
this.sex =
public String getAddress() {
public void setAddress(String address) {
this.address =
创建对象:
Student s=new Student("CC","qwerty","男","银河系第二旋臂");
对比一下进行一个优劣性分析:
一般的套路:优点是比较简单,开发效率高,缺点是如果参数真的很多的话鬼知道每个对应的是什么意思啊。
Builder模式:优点是可以将构造器的setter方法名取成类似注释的方式,这样我们可以很清晰的知道刚才究竟设置的什么值,可读性较高,缺点是比较冗长。
总结:初步的理解Builder模式解决了要设置的参数过多不好管理的问题(感觉每次构建不同对象是废话 - -)。
从Struts2框架中拿出来的两个Builder模式的例子(都是Config类):
ActionConfig :
public class ActionConfig extends Located implements Serializable {
public static final String WILDCARD = "*";
protected List&InterceptorMapping& // a list of interceptorMapping Objects eg. List&InterceptorMapping&
protected Map&String,String&
protected Map&String, ResultConfig&
protected List&ExceptionMappingConfig& exceptionM
protected String classN
protected String methodN
protected String packageN
protected S
protected Set&String& allowedM
protected ActionConfig(String packageName, String name, String className) {
this.packageName = packageN
this.name =
this.className = classN
params = new LinkedHashMap&String, String&();
results = new LinkedHashMap&String, ResultConfig&();
interceptors = new ArrayList&InterceptorMapping&();
exceptionMappings = new ArrayList&ExceptionMappingConfig&();
allowedMethods = new HashSet&String&();
allowedMethods.add(WILDCARD);
* Clones an ActionConfig, copying data into new maps and lists
* @param orig The ActionConfig to clone
* @Since 2.1
protected ActionConfig(ActionConfig orig) {
this.name = orig.
this.className = orig.classN
this.methodName = orig.methodN
this.packageName = orig.packageN
this.params = new LinkedHashMap&String,String&(orig.params);
this.interceptors = new ArrayList&InterceptorMapping&(orig.interceptors);
this.results = new LinkedHashMap&String,ResultConfig&(orig.results);
this.exceptionMappings = new ArrayList&ExceptionMappingConfig&(orig.exceptionMappings);
this.allowedMethods = new HashSet&String&(orig.allowedMethods);
public String getName() {
public String getClassName() {
return classN
public List&ExceptionMappingConfig& getExceptionMappings() {
return exceptionM
public List&InterceptorMapping& getInterceptors() {
public Set&String& getAllowedMethods() {
return allowedM
* Returns name of the action method
* @return name of the method to execute
public String getMethodName() {
return methodN
* @return Returns the packageName.
public String getPackageName() {
return packageN
public Map&String, String& getParams() {
public Map&String, ResultConfig& getResults() {
public boolean isAllowedMethod(String method) {
if (allowedMethods.size() == 1 && WILDCARD.equals(allowedMethods.iterator().next())) {
return true;
return allowedMethods.contains(method);
@Override public boolean equals(Object o) {
if (this == o) {
return true;
if (!(o instanceof ActionConfig)) {
return false;
final ActionConfig actionConfig = (ActionConfig)
if ((className != null) ? (!className.equals(actionConfig.className)) : (actionConfig.className != null)) {
return false;
if ((name != null) ? (!name.equals(actionConfig.name)) : (actionConfig.name != null)) {
return false;
if ((interceptors != null) ? (!interceptors.equals(actionConfig.interceptors)) : (actionConfig.interceptors != null))
return false;
if ((methodName != null) ? (!methodName.equals(actionConfig.methodName)) : (actionConfig.methodName != null)) {
return false;
if ((params != null) ? (!params.equals(actionConfig.params)) : (actionConfig.params != null)) {
return false;
if ((results != null) ? (!results.equals(actionConfig.results)) : (actionConfig.results != null)) {
return false;
if ((allowedMethods != null) ? (!allowedMethods.equals(actionConfig.allowedMethods)) : (actionConfig.allowedMethods != null)) {
return false;
return true;
@Override public int hashCode() {
result = (interceptors != null ? interceptors.hashCode() : 0);
result = 31 * result + (params != null ? params.hashCode() : 0);
result = 31 * result + (results != null ? results.hashCode() : 0);
result = 31 * result + (exceptionMappings != null ? exceptionMappings.hashCode() : 0);
result = 31 * result + (className != null ? className.hashCode() : 0);
result = 31 * result + (methodName != null ? methodName.hashCode() : 0);
result = 31 * result + (packageName != null ? packageName.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (allowedMethods != null ? allowedMethods.hashCode() : 0);
@Override public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{ActionConfig ");
sb.append(name).append(" (");
sb.append(className);
if (methodName != null) {
sb.append(".").append(methodName).append("()");
sb.append(")");
sb.append(" - ").append(location);
sb.append("}");
return sb.toString();
* The builder for this object.
An instance of this object is the only way to construct a new instance.
* purpose is to enforce the immutability of the object.
The methods are structured in a way to support chaining.
* After setting any values you need, call the {@link #build()} method to create the object.
public static class Builder implements InterceptorListHolder{
private ActionC
public Builder(ActionConfig toClone) {
target = new ActionConfig(toClone);
public Builder(String packageName, String name, String className) {
target = new ActionConfig(packageName, name, className);
public Builder packageName(String name) {
target.packageName =
return this;
public Builder name(String name) {
target.name =
return this;
public Builder className(String name) {
target.className =
return this;
public Builder defaultClassName(String name) {
if (StringUtils.isEmpty(target.className)) {
target.className =
return this;
public Builder methodName(String method) {
target.methodName =
return this;
public Builder addExceptionMapping(ExceptionMappingConfig exceptionMapping) {
target.exceptionMappings.add(exceptionMapping);
return this;
public Builder addExceptionMappings(Collection&? extends ExceptionMappingConfig& mappings) {
target.exceptionMappings.addAll(mappings);
return this;
public Builder exceptionMappings(Collection&? extends ExceptionMappingConfig& mappings) {
target.exceptionMappings.clear();
target.exceptionMappings.addAll(mappings);
return this;
public Builder addInterceptor(InterceptorMapping interceptor) {
target.interceptors.add(interceptor);
return this;
public Builder addInterceptors(List&InterceptorMapping& interceptors) {
target.interceptors.addAll(interceptors);
return this;
public Builder interceptors(List&InterceptorMapping& interceptors) {
target.interceptors.clear();
target.interceptors.addAll(interceptors);
return this;
public Builder addParam(String name, String value) {
target.params.put(name, value);
return this;
public Builder addParams(Map&String,String& params) {
target.params.putAll(params);
return this;
public Builder addResultConfig(ResultConfig resultConfig) {
target.results.put(resultConfig.getName(), resultConfig);
return this;
public Builder addResultConfigs(Collection&ResultConfig& configs) {
for (ResultConfig rc : configs) {
target.results.put(rc.getName(), rc);
return this;
public Builder addResultConfigs(Map&String,ResultConfig& configs) {
target.results.putAll(configs);
return this;
public Builder addAllowedMethod(String methodName) {
target.allowedMethods.add(methodName);
return this;
public Builder addAllowedMethod(Collection&String& methods) {
target.allowedMethods.addAll(methods);
return this;
public Builder location(Location loc) {
target.location =
return this;
public ActionConfig build() {
target.params = Collections.unmodifiableMap(target.params);
target.results = Collections.unmodifiableMap(target.results);
target.interceptors = Collections.unmodifiableList(target.interceptors);
target.exceptionMappings = Collections.unmodifiableList(target.exceptionMappings);
target.allowedMethods = Collections.unmodifiableSet(target.allowedMethods);
ActionConfig result =
target = new ActionConfig(target);
ExceptionMappingConfig:
public class ExceptionMappingConfig extends Located implements Serializable {
private String exceptionClassN
private Map&String,String&
protected ExceptionMappingConfig(String name, String exceptionClassName, String result) {
this.name =
this.exceptionClassName = exceptionClassN
this.result =
this.params = new LinkedHashMap&String,String&();
protected ExceptionMappingConfig(ExceptionMappingConfig target) {
this.name = target.
this.exceptionClassName = target.exceptionClassN
this.result = target.
this.params = new LinkedHashMap&String,String&(target.params);
public String getName() {
public String getExceptionClassName() {
return exceptionClassN
public String getResult() {
public Map&String,String& getParams() {
public boolean equals(Object o) {
if (this == o) {
return true;
if (!(o instanceof ExceptionMappingConfig)) {
return false;
final ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig)
if ((name != null) ? (!name.equals(exceptionMappingConfig.name)) : (exceptionMappingConfig.name != null)) {
return false;
if ((exceptionClassName != null) ? (!exceptionClassName.equals(exceptionMappingConfig.exceptionClassName)) : (exceptionMappingConfig.exceptionClassName != null))
return false;
if ((result != null) ? (!result.equals(exceptionMappingConfig.result)) : (exceptionMappingConfig.result != null))
return false;
if ((params != null) ? (!params.equals(exceptionMappingConfig.params)) : (exceptionMappingConfig.params != null))
return false;
return true;
public int hashCode() {
hashCode = ((name != null) ? name.hashCode() : 0);
hashCode = (29 * hashCode) + ((exceptionClassName != null) ? exceptionClassName.hashCode() : 0);
hashCode = (29 * hashCode) + ((result != null) ? result.hashCode() : 0);
hashCode = (29 * hashCode) + ((params != null) ? params.hashCode() : 0);
return hashC
* The builder for this object.
An instance of this object is the only way to construct a new instance.
* purpose is to enforce the immutability of the object.
The methods are structured in a way to support chaining.
* After setting any values you need, call the {@link #build()} method to create the object.
public static class Builder{
private ExceptionMappingC
public Builder(ExceptionMappingConfig toClone) {
target = new ExceptionMappingConfig(toClone);
public Builder(String name, String exceptionClassName, String result) {
target = new ExceptionMappingConfig(name, exceptionClassName, result);
public Builder name(String name) {
target.name =
return this;
public Builder exceptionClassName(String name) {
target.exceptionClassName =
return this;
public Builder result(String result) {
target.result =
return this;
public Builder addParam(String name, String value) {
target.params.put(name, value);
return this;
public Builder addParams(Map&String,String& params) {
target.params.putAll(params);
return this;
public Builder location(Location loc) {
target.location =
return this;
public ExceptionMappingConfig build() {
target.params = Collections.unmodifiableMap(target.params);
ExceptionMappingConfig result =
target = new ExceptionMappingConfig(target);
参考资料:
阅读(...) 评论()Character Builder_百度百科
清除历史记录关闭
声明:百科词条人人可编辑,词条创建和修改均免费,绝不存在官方及代理商付费代编,请勿上当受骗。
Character Builder
本词条缺少概述、名片图,补充相关内容使词条更完整,还能快速升级,赶紧来吧!
Character Builder介绍
Character Builder是世界上最领先的动画人物创建工具。它包含了多种卡
通和真人模板和互动模板。只需要通过点击和简单设置你就可以创建出各种生动的FLASH动画人物和场景。将他们插入你的E-learning课件,你的课件立即变得生动活泼而富有专业性。
Character Builder对课程开发者的优势
1、 最强大Flash动画人物开发工具,富含多种人物模型,适合您的各种需求;
2、 实现人物眼神、口形、肢体动作和声音的同步;
3、 结合互动问答、演示背景等使课件更加生动;
4、提供超过20种人物造型并不断加入新的人物;
5、可以和Articulate, OutStart Trainer, Composica等E-learning课件制作工具集成
Character Builder主要包含的模块
Character Builder演讲模块
Character Builder可以让你快速的创建动画人物,运用于幻灯片演示以及网站Flash所需素材等。并且你不需要是一个漫画家或会使用专业的制作Flash的软件。
Character Builder互动模板
用Character Builder制作而生成的Flash可以上传到任何网络服务器。每个专案由一些数量的动画人物讲演组成,并能生成有次序性和互动性的Flash。
.E-learning之家 Character Builder 版块[引用日期]
清除历史记录关闭PE builder_百度百科
清除历史记录关闭
声明:百科词条人人可编辑,词条创建和修改均免费,绝不存在官方及代理商付费代编,请勿上当受骗。
PE builder
本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来吧!
art's PE Builder 是一个可以帮助你快速的创建可引导的CD或者DVD光盘的工具,用来诊断和修复系统错误时使用。
最新版本支持Windows XP 和Windows Server 2003!不同于一般的引导光盘制作工具,本程序可以帮助你创建纯32位的图形界面操作环境,支持网络和远程管理功能,支持FAT/NTFS/CDFS等各种常见分区格式,采用800X600的分辨率,用此光盘引导系统,你可以进行系统错误判断和修复,文件数据的抢修,病毒的扫描和杀毒等操作,非常容易使用。
Bart's PE Builder 帮助你建立和从光盘启动Windows XP和Windows Server 2003核心的迷你操作系统,类似Microsoft的Windows PE,直接在BIOS里设置光盘启动然后WIN PE会自动引导进入一个命令行界面(象从前的DOS),可以读写FAT32、NTFS等格式的分区,可以对、格式化(可以格成NTFS的);还可以直接使用网络。
清除历史记录关闭Flash Builder_百度百科
清除历史记录关闭
声明:百科词条人人可编辑,词条创建和修改均免费,绝不存在官方及代理商付费代编,请勿上当受骗。
Flash Builder
Flash Builder是Adobe公司的Flex Builder的下一代产品,Flash Builder将构成应用程序的资源(文件夹和文件)组合到一个容器中,项目包含一组属性,这些属性控制应用程序的构建方式、构建的应用程序所在的位置、调试的处理方式以及该项目于工作空间中其他项目的关系。Flex Builder创建的目的是为了能创建Flex框架。
Flash Builder简介
Flash Builder
日,Adobe宣布,下一代Flex Builder4将改名为Flash Builder 4,这引起了众多开发者的异议。Lee Brimelow是Adobe平台的宣传者,他向诸多开发者解释了为什么将Flex Builder更名为Flash Builder。
Adobe Flex Builder将成为历史
关于IDE方面的问题,Lee Brimelow称,不会重新定位新的Flash IDE,Flash 仍然将是Flash平台动画和设计的首要工具,Adobe会将一部分的精力放在Flash CS5代码剪辑器上,以满足部分ActionScript开发者。由于Flex是一种用来创建RIA的ActionScript框架,所以,名字更改对于 Flex来说,反而更容易使Flash品牌更加牢固易懂。
诸多开发者仍然是单纯的使用ActionScript来进行工作。更名称Flash Builder后,仍然将继续是基于Eclipse产品,并将添加更多的特性。
关于Flex方面的问题,Lee Brimelow称,Adobe还没有计划重新命名SDK,他说,很多开发者用它来编译纯粹的ActionScript项目,与别人交谈时,重点仍然是技术而不是在表述上。最新版本为4.7,2012年10月发布。
Flash Builder特性
1) Package Explorer
如果你熟悉Eclipse中JDT,对这个特性一定不陌生。Flex Builder 3只支持资源浏览器,即以工程文件的形式浏览。而Package Explorer,顾名思义,是针对Package的结构进行浏览,这种模式更适于开发者使用,另外也可以展开一个类来浏览该类的结构(同 Outline)。不仅如此,Package Explorer还支持以这种结构浏览SWC文件。
2) 代码模板
虽然我们也可以通过来实现代码模板,但是总还是原生支持来的更舒服。代码模板还支持“”,例如你可以向模板中添加“${project_name}”,则这部分内容会被转化成工程名称。代码模板可以在偏好(Preference)中进行配置。
重构功能一直是我对Flex Builder比较不满意的地方。说实话,基于包的重构应该是比较基本的功能了。
4) 悬停时的ASDoc提示
也是从Eclipse中“继承”下来的功能,支持ASDoc中的链结
5) Getter & Setter
方便地在代码中添加Getter和Setter。(这里有个小插曲,Heidi在演示前忘了把代码恢复成没有Setter的状态,所以她不得不现场把代码改回去,还很可爱的对观众们说“别看” ^_^ )。
6) 自动生成Event Handler
这个功能比用代码模板要方便得多。
7) Run to Line
有的时候我们调试时会发现设置的并不合理,例如断点位置离我们关注的还远得很,这时候可以使用Run to Line功能立刻将程序执行到指定位置。
8) 条件断点
顾名思义,就是当满足某种条件的时候才会中断程序,个人认为非常有用,特别是在调试一些复杂逻辑时,会大大节约时间,同时也能放松你的神经,免得一不注意错过了还要从头来过。
9) Network Monitor
调试模式下可以监控网络连接的数据,例如WebService的SOAP数据以及HTTP的请求和响应内容,也是一个非常有用的特性。
(事实上Heidi到此为止一共只介绍了9个新特性,介绍第8个特性之后她似乎数错了。所以我把一开始介绍的那个Service Explorer算上,一共是10个)
10)Service Explorer
Service Explorer可以方便的浏览服务端API(如WebServices或Remoting Services)极其Value Object,同时也可以根据服务端的VO自动创建AS端的VO。
清除历史记录关闭HBuilder_百度百科
清除历史记录关闭
声明:百科词条人人可编辑,词条创建和修改均免费,绝不存在官方及代理商付费代编,请勿上当受骗。
HBuilder是()推出的一款支持的开发。
HBuilder的编写用到了、、Web和。HBuilder主体是由Java编写。它基于,所以顺其自然地兼容了的。
快,是HBuilder的最大优势,通过完整的语法提示和输入法、代码块等,大幅提升、、的开发效率。
.CSDN[引用日期]
.开源中国社区.[引用日期]
清除历史记录关闭}

我要回帖

更多关于 builder设计模式适用于 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信