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

📄 +

📁 java 24种设计模式 含代码 java 24种设计模式 含代码
💻
📖 第 1 页 / 共 2 页
字号:
<html><!-- #BeginTemplate "/Templates/module.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" --> 
<title>设计模式之Observer 观察者模式</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">设计模式之Observer</h3>
      <p align="center"><a href="http://www.jdon.com/aboutme.htm">板桥里人</a> http://www.jdon.com 2002/3/16</p>
      <p align="center"><a href="../mybook/index.htm" target="_blank"><strong>模式实战书籍《Java实用系统开发指南》</strong></a></p>
      <p>Java深入到一定程度,就不可避免的碰到设计模式(design pattern)这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循一定的编程模式,才能使自己的代码便于理解,易于交流,Observer(观察者)模式是比较常用的一个模式,尤其在界面设计中应用广泛,而本站所关注的是Java在电子商务系统中应用,因此想从电子商务实例中分析Observer的应用.</p>
      <p>虽然网上商店形式多样,每个站点有自己的特色,但也有其一般的共性,单就&quot;商品的变化,以便及时通知订户&quot;这一点,是很多网上商店共有的模式,这一模式类似Observer
        patern观察者模式.</p>
      <p>具体的说,如果网上商店中商品在名称 价格等方面有变化,如果系统能自动通知会员,将是网上商店区别传统商店的一大特色.这就需要在商品product中加入Observer这样角色,以便product细节发生变化时,Observer能自动观察到这种变化,并能进行及时的update或notify动作.</p>
      <p><img src="images/observer1.jpg" width="443" height="323"></p>
      <p>Java的API还为为我们提供现成的Observer接口Java.util.Observer.我们只要直接使用它就可以.</p>
      <p>我们必须extends Java.util.Observer才能真正使用它:<br>
        1.提供Add/Delete observer的方法;<br>
        2.提供通知(notisfy) 所有observer的方法;<br>
      </p>
      <table width="80%" border="0" cellspacing="3" cellpadding="3" bgcolor="#CCCCCC">
        <tr> 
          <td>//产品类 可供Jsp直接使用UseBean调用 该类主要执行产品数据库插入 更新<br>
            public class product extends Observable{ 
            <p>  private String name;<br>
                private float price;</p>
            <p>  public String getName(){ return name;}<br>
                public void setName(String name){<br>
                 this.name=name;<br>
                //设置变化点 <br>
                 setChanged();<br>
                 notifyObservers(name);</p>
            <p>  }   </p>
            <p>  public float getPrice(){ return price;}<br>
                public void setPrice(float price){<br>
                 this.price=price;<br>
                //设置变化点<br>
                 setChanged();<br>
                 notifyObservers(new Float(price)); </p>
            <p>  }<br>
              <br>
                //以下可以是数据库更新 插入命令.<br>
                public void saveToDb(){<br>
                .....................<br>
            </p>
            <p>}</p>
          </td>
        </tr>
      </table>
      <p> <br>
        我们注意到,在product类中 的setXXX方法中,我们设置了 notify(通知)方法, 当Jsp表单调用setXXX(如何调用见我的<a href="../idea/&#25968;&#25454;&#24211;bean.htm" target="_blank">另外一篇文章</a>),实际上就触发了notisfyObservers方法,这将通知相应观察者应该采取行动了.</p>
      <p>下面看看这些观察者的代码,他们究竟采取了什么行动:</p>
                  <table width="99%" border="0" cellspacing="3" cellpadding="3">
                    <tr> 
          <td bgcolor="#CCCCCC"> 
            <p>//观察者NameObserver主要用来对产品名称(name)进行观察的<br>
              public class NameObserver implements Observer{</p>
            <p>  private String name=null;</p>
            <p>  public void update(Observable obj,Object arg){<br>
              <br>
                  if (arg <a href="http://www-900.ibm.com/developerWorks/java/l-leditor/index.shtml" target="_blank">instanceof</a> 
              String){</p>
            <p>     name=(String)arg;<br>
                   //产品名称改变值在name中<br>
                   System.out.println(&quot;NameObserver :name changet to &quot;+name);</p>
            <p>    }<br>
            </p>
            <p>  }</p>
            <p>}</p>
            <p>//观察者PriceObserver主要用来对产品价格(price)进行观察的<br>
              public class PriceObserver implements Observer{</p>
            <p>  private float price=0;</p>
            <p>  public void update(Observable obj,Object arg){<br>
              <br>
                  if (arg instanceof Float){</p>
            <p>     price=((Float)arg).floatValue();<br>
                <br>
                   System.out.println(&quot;PriceObserver :price changet to &quot;+price);</p>
            <p>    }<br>
            </p>
            <p>  }</p>
            <p>}</p>
          </td>
        </tr>
      </table>
      <p><br>
        Jsp中我们可以来正式执行这段观察者程序:</p>
                  <table width="96%" border="0" cellspacing="3" cellpadding="3">
                    <tr> 
          <td bgcolor="#CCCCCC"> 
            <p><b>&lt;jsp:useBean id=&quot;product&quot; scope=&quot;session&quot; 
              class=&quot;Product&quot; /&gt;<br>
              &lt;jsp:setProperty name=&quot;product&quot; property=&quot;*&quot; 

⌨️ 快捷键说明

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