📄 +
字号:
<html><!-- #BeginTemplate "/Templates/module.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title> 设计模式之Builder 生成器模式</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../jdon.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="http://www.jdon.com/favicon.ico">
<meta http-equiv="refresh" content="1200">
</head>
<body bgcolor="#014E82" text="#000000">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="129" rowspan="3" align="center" valign="bottom" bgcolor="#FFFFFF">
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td align="center" >
<img src="http://www.jdon.com/images/jdon.gif" width="120" height="60"></td>
</tr>
<tr>
<td height="9"></td>
</tr>
</table>
</td>
<td valign="bottom" >
<table cellpadding="0" cellspacing="0" height="100%" width="100%" >
<tr bgcolor="#CAC9BB">
<td height="70" align="right" valign="bottom" bgcolor="#CAC9BB">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="619" height="60">
<param name="movie" value="../images/jd.swf">
<param name="quality" value="high"><param name="BGCOLOR" value="#CAC9BB">
<embed src="../images/jd.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="619" height="60"></embed>
</object></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<td height="26" bgcolor="#707070" width="222"> </td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/index.html" class="a03">首 页</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/design.htm" class="a03">设计研究</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/product.htm" class="a03">构件产品</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/trainning.htm" class="a03">培训咨询</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jdonframework/" class="a03">Jdon框架</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jive/index.jsp" class="a03">论坛</a></div></td>
</tr></table>
</td>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="11" colspan="7" bgcolor="#FFFFFF" class="bianxian">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="1" bordercolor="#000000">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="2" bgcolor="#000000">
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" valign="top" bgcolor="#FFFFD7"><table width="100%" border="0" cellspacing="0" cellpadding="3" height="200" align="center">
<tr>
<td align="left" valign="top"><!-- #BeginEditable "Menu" --> <!-- #EndEditable --> </td>
</tr>
<tr>
<td align="right">
<p><a href="http://www.jdon.com/trainning/jiaocheng.htm" target="_blank"><font color="#FF9999"><strong>在线教程</strong></font><br>
<font color="#555555"> 原创教程区</font></a></p>
<p><a href="http://www.jdon.com/jdonframework/app.htm" target="_blank"><font color="#FF9999"><strong>Jdon框架</strong></font><br>
<font color="#555555"> 在线运行演示</font></a></p>
<br>
<div id=vgad ></div>
<!-- #BeginEditable "Menu2" --> <!-- #EndEditable -->
</td>
</tr>
</table>
</td>
<td width="1" height="100%" >
<table height="100%" cellpadding="0" cellspacing="0" background="../images/blackpoint.gif" >
<tr><td> <img name="" src="" width="1" height="1" alt="" ></td></tr></table>
</td >
<td valign="top" >
<table width="100%" border="0" cellspacing="13" cellpadding="2">
<tr>
<td class="article"><!-- #BeginEditable "main" -->
<h3 align="center">设计模式之Builder</h3>
<p align="center"><a href="http://www.jdon.com/aboutme.htm">板桥里人</a> http://www.jdon.com 2002/05/07</p>
<p align="center"><a href="../mybook/index.htm" target="_blank"><strong>模式实战书籍《Java实用系统开发指南》</strong></a></p>
<p><b>Builder模式定义</b>:<br>
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.</p>
<p>Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构建它们.用户不知道内部的具体构建细节.Builder模式是非常类似抽象工厂模式,细微的区别大概只有在反复使用中才能体会到.</p>
<p><b>为何使用?</b><br>
是为了将构建复杂对象的<b><i>过程</i></b>和它的<i><b>部件</b></i>解耦.注意: 是解耦<b><i>过程</i></b>和<i><b>部件</b></i>.</p>
<p>因为一个复杂的对象,不但有很多大量组成部分,如汽车,有很多部件:车轮 方向盘 发动机还有各种小零件等等,部件很多,但远不止这些,如何将这些部件装配成一辆汽车,这个装配过程也很复杂(需要很好的组装技术),Builder模式就是为了将部件和组装过程分开.<br>
</p>
<p><b>如何使用?</b><br>
首先假设一个复杂对象是由多个部件组成的,Builder模式是把复杂对象的创建和部件的创建分别开来,分别用Builder类和Director类来表示.<br>
</p>
<p>首先,需要一个接口,它定义如何创建复杂对象的各个部件:</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC">
<p>public interface Builder { </p>
<p> //创建部件A 比如创建汽车车轮<br>
void buildPartA(); <br>
//创建部件B 比如创建汽车方向盘<br>
void buildPartB(); <br>
//创建部件C 比如创建汽车发动机<br>
void buildPartC(); <br>
<br>
//返回最后组装成品结果 (返回最后装配好的汽车)<br>
//成品的组装过程不在这里进行,而是转移到下面的Director类中进行.<br>
//从而实现了解耦<b><i>过程</i></b>和<i><b>部件</b></i><br>
Product getResult(); </p>
<p>} </p>
</td>
</tr>
</table>
<p>用Director构建最后的复杂对象,而在上面Builder接口中封装的是如何创建一个个部件(复杂对象是由这些部件组成的),也就是说Director的内容是如何将部件最后组装成成品:</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC">
<p>public class Director {</p>
<p> private Builder builder; </p>
<p> public Director( Builder builder ) { <br>
this.builder = builder; <br>
} <br>
// 将部件partA partB partC最后组成复杂对象<br>
//这里是将车轮 方向盘和发动机组装成汽车的过程<br>
public void construct() { <br>
builder.buildPartA();<br>
builder.buildPartB();<br>
builder.buildPartC(); </p>
<p> } </p>
<p>} </p>
</td>
</tr>
</table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -