⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 +

📁 java 24种设计模式 含代码 java 24种设计模式 含代码
💻
📖 第 1 页 / 共 2 页
字号:
<html><!-- #BeginTemplate "/Templates/module.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" --> 
<title>  
    设计模式之Composite 组成模式 树模式
      </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">&nbsp;</td>
            <td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/index.html" class="a03">首&nbsp;&nbsp;&nbsp;&nbsp;页</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">设计模式之Composite(组合)</h3>
      <p align="center"><a href="http://www.jdon.com/aboutme.htm">板桥里人</a> http://www.jdon.com
        2002/04/27(转载请保留)</p>
      <p align="center"><a href="../mybook/index.htm" target="_blank"><strong>模式实战书籍《Java实用系统开发指南》</strong></a></p>
      <p><b>Composite模式定义</b>:<br>
        将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性.</p>
      <p>Composite比较容易理解,想到Composite就应该想到树形结构图。组合体内这些对象都有共同接口,当组合体一个对象的方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。可以用牵一动百来形容。</p>
      <p>所以Composite模式使用到Iterator模式,和Chain of Responsibility模式类似。</p>
      <p><b>Composite好处</b>:<br>
        1.使客户端调用简单,客户端可以一致的使用组合结构或其中单个对象,用户就不必关系自己处理的是单个对象还是整个组合结构,这就简化了客户端代码。<br>
        2.更容易在组合体内加入对象部件. 客户端不必因为加入了新的对象部件而更改代码。</p>
      <p><b>如何使用Composite?</b><br>
        首先定义一个接口或抽象类,这是设计模式通用方式了,其他设计模式对接口内部定义限制不多,Composite却有个规定,那就是要在接口内部定义一个用于访问和管理Composite组合体的对象们(或称部件Component).</p>
      <p>下面的代码是以抽象类定义,一般尽量用接口interface,</p>
      <table width="80%" border="0" cellspacing="3" cellpadding="3">
        <tr> 
          <td bgcolor="#CCCCCC">public abstract class Equipment<br>
            {<br>
              private String name; <br>
                          //实价<br>
              public abstract double netPrice();<br>
              //折扣价格<br>
              public abstract double discountPrice();<br>
              //增加部件方法  <br>
              public boolean add(Equipment equipment) { return false; }<br>
              //删除部件方法<br>
              public boolean remove(Equipment equipment) { return false; }<br>
              //注意这里,这里就提供一种用于访问组合体类的部件方法。<br>
              public Iterator iter() { return null; }<br>
              <br>
              public Equipment(final String name) { this.name=name; }<br>
            } </td>
        </tr>
      </table>
      <p>抽象类Equipment就是Component定义,代表着组合体类的对象们,Equipment中定义几个共同的方法。</p>
      <table width="80%" border="0" cellspacing="3" cellpadding="3">
        <tr> 
          <td bgcolor="#CCCCCC">public class Disk extends Equipment<br>
            {<br>
              public Disk(String name) { super(name); }<br>
                          //定义Disk实价为1<br>
              public double netPrice() { return 1.; }<br>
              //定义了disk折扣价格是0.5 对折。<br>
              public double discountPrice() { return .5; }<br>
            }</td>
        </tr>
      </table>
      <p>Disk是组合体内的一个对象,或称一个部件,这个部件是个单独元素( Primitive)。<br>
        还有一种可能是,一个部件也是一个组合体,就是说这个部件下面还有'儿子',这是树形结构中通常的情况,应该比较容易理解。现在我们先要定义这个组合体:</p>
      <table width="80%" border="0" cellspacing="3" cellpadding="3">
        <tr> 
          <td bgcolor="#CCCCCC"> 
            <p>abstract class CompositeEquipment extends Equipment<br>
              {<br>
                private int i=0; <br>
                //定义一个Vector 用来存放'儿子'<br>
                private Lsit equipment=new ArrayList();<br>
              <br>
                public CompositeEquipment(String name) { super(name); }<br>
              <br>
                public boolean add(Equipment equipment) { <br>
                   this.equipment.add(equipment); <br>
                   return true; <br>
                 }<br>
              <br>
                public double netPrice() <br>
                {<br>
                  double netPrice=0.;<br>
                  Iterator iter=equipment.iterator();<br>
                  for(iter.hasNext())<br>
                    netPrice+=((Equipment)iter.next()).netPrice();<br>
                  return netPrice;<br>
                }<br>
              <br>
                public double discountPrice() <br>
                {<br>
                  double discountPrice=0.;<br>
                  Iterator iter=equipment.iterator();<br>
                  for(iter.hasNext())<br>
                    discountPrice+=((Equipment)iter.next()).discountPrice();<br>
                  return discountPrice;<br>
                }<br>
                <br>
            </p>
            <p>  //注意这里,这里就提供用于访问自己组合体内的部件方法。<br>
                //上面dIsk 之所以没有,是因为Disk是个单独(Primitive)的元素.<br>
                public Iterator iter()<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -