??xml version="1.0" encoding="utf-8" standalone="yes"?>国产亚洲精品拍拍拍拍拍,亚洲色精品aⅴ一区区三区 ,亚洲AV永久无码天堂影院http://m.tkk7.com/crazycy/category/11615.html记录Ҏ 鉴往事之得失 以资于发?zh-cnFri, 07 Mar 2008 22:29:26 GMTFri, 07 Mar 2008 22:29:26 GMT60我对设计模式的理解:AOP vs 动态代理(Dynamic ProxyQ?/title><link>http://m.tkk7.com/crazycy/archive/2006/10/29/77903.html</link><dc:creator>crazycy</dc:creator><author>crazycy</author><pubDate>Sun, 29 Oct 2006 07:15:00 GMT</pubDate><guid>http://m.tkk7.com/crazycy/archive/2006/10/29/77903.html</guid><wfw:comment>http://m.tkk7.com/crazycy/comments/77903.html</wfw:comment><comments>http://m.tkk7.com/crazycy/archive/2006/10/29/77903.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://m.tkk7.com/crazycy/comments/commentRss/77903.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/crazycy/services/trackbacks/77903.html</trackback:ping><description><![CDATA[ <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000"> <font style="BACKGROUND-COLOR: #deb887">非常感谢你的阅读Q如果你觉得好或者对你有一定的帮助Q请U极参与评论以示鼓励?/font> </span> </div> <br />N到?font color="#0000ff">AOP与动态代理有什么联p?/font>”的问题Q现把个Ҏ理如下:<br /><br />我觉得,动态代理是AOP的主要实现手D之一QAOP是动态代理的一U应用深? <p>AOP是一U思想Q或者是Ҏ论,cMOOPQ是OOP的有力补充;<br />OOP侧重于主要业务功能设计(一般关注点Q;<br />AOP侧重于横切关注点Q事务、日志、安全等Q诏I于多个模块的功能)<br />分析:<br />OOP在横切关注点问题的不xQ?)代码混ؕ 2Q代码分?br />AOP很好的解决了q个问题Qƈ且可以很好的解决“R入问题”?br /><br />AOP的实玎ͼ必然要求ҎL对象q行代理Q这个地Ҏ疑要用的是动态代理机制?/p><p>如果非要说谁是谁的实现手D,我更們֐于AOP主要应用了动态代理,动态代理是AOP的实现手Dc?br /><br /><font style="BACKGROUND-COLOR: #ff1493">附记</font>Q?br />记得一个文章提及:<br />AOP是设计模式的延箋Q?br />GoF侧重于接口、抽象函数来解除耦合Q却对对象的内部无能为力?br />AOP却恰C对象的内部着手,实现调用者和被调用者的分离?br /><br />常思考之Q甚是!</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://m.tkk7.com/images/OutliningIndicators/None.gif" align="top" /><font style="BACKGROUND-COLOR: #deb887"><span style="COLOR: #000000">Thanks very much to visit blog,  welcome your feedback,  your feedback is the Driver </span><span style="COLOR: #000000">&&</span><span style="COLOR: #000000"> Power to me.</span></font></div><img src ="http://m.tkk7.com/crazycy/aggbug/77903.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/crazycy/" target="_blank">crazycy</a> 2006-10-29 15:15 <a href="http://m.tkk7.com/crazycy/archive/2006/10/29/77903.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我对设计模式的理解:工厂Ҏ模式(Factory Mehod) ?抽象工厂模式(Abstract Factory)http://m.tkk7.com/crazycy/archive/2006/10/08/73858.htmlcrazycycrazycySun, 08 Oct 2006 08:39:00 GMThttp://m.tkk7.com/crazycy/archive/2006/10/08/73858.htmlhttp://m.tkk7.com/crazycy/comments/73858.htmlhttp://m.tkk7.com/crazycy/archive/2006/10/08/73858.html#Feedback3http://m.tkk7.com/crazycy/comments/commentRss/73858.htmlhttp://m.tkk7.com/crazycy/services/trackbacks/73858.html 非常感谢你的阅读Q如果你觉得好或者对你有帮助Q请U极l一个留a反馈以示鼓励?br />
两者都pd厂模式?br />
本质都是创建具体对象的职责和过E从代码中提取出来,从而实现调用者和被调用者解耦的Q设计模式)目标?br />
但之所以会存在两个分支Q我们可以从其各自的实质来看Q?br />工厂Ҏ模式(Factory Mehod Pattern) 只生一cd象;
抽象工厂模式(Abstract Factory Pattern)产生一pd对象Q这些对象具有如下特征:相关或者相互依?/font>?br />
工厂Ҏ模式可以通过带参数的变种Q即l合j2ee Service Location Pattern方式Q,从而生多U品(即一cd象的多个不同的实例)?br />
抽象工厂模式更注重是相关或者相互依赖的一pd对象Q?font color="#a52a2a">一旦需要改动,只修Ҏ一个具体工厂即可到达修Ҏ个品系列的目的
?br />从这一点,如果是涉及一个系列的产品一起工作,我们应该放弃采用(工厂Ҏ模式的)多个工厂Ҏ来达CQ抽象工厂)相同目的的做法?br />因ؓQ?font color="#ffc0cb">如果一个系列的产品一起工作时Q一个应用一ơ只能用同一个品系列中一个对?/font>Q只需要一个具体工厂类Q抽象工厂模式)Q或?多个具体工场c(工厂Ҏ模式Q。如果这一点可以接受,那修改呢Q!

Thanks very much to visit blog,  welcome your feedback,  your feedback is the Driver && Power to me



crazycy 2006-10-08 16:39 发表评论
]]>
单例模式(SingLeton Pattern)的误?/title><link>http://m.tkk7.com/crazycy/archive/2006/07/15/58268.html</link><dc:creator>crazycy</dc:creator><author>crazycy</author><pubDate>Fri, 14 Jul 2006 16:02:00 GMT</pubDate><guid>http://m.tkk7.com/crazycy/archive/2006/07/15/58268.html</guid><wfw:comment>http://m.tkk7.com/crazycy/comments/58268.html</wfw:comment><comments>http://m.tkk7.com/crazycy/archive/2006/07/15/58268.html#Feedback</comments><slash:comments>13</slash:comments><wfw:commentRss>http://m.tkk7.com/crazycy/comments/commentRss/58268.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/crazycy/services/trackbacks/58268.html</trackback:ping><description><![CDATA[ <span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">单例(</span>SingLeton<span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">)故名思义是在一?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">JVM</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">q行中只有一个对象存在;请你务必注意到是在一?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">JVM</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">虚拟机内?br /><br />今天一个朋友问q这样一个问题:Z么他的单例每ơ都q入构造函敎ͼE序如下Q?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ServerThread </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> Runnable{<br /><br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> ServerThread instance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;  <br /><br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> ServerThread() {<br /><br />         </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br /><br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (socket </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br /><br />                 socket </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> DatagramSocket(Constants.SERVER_PORT);<br /><br />             }<br /><br />          } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br /><br />                 e.printStackTrace();<br /><br />           }<br /><br />       }<br /><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> ServerThread getInstance() {<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (instance </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />           System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">instance is null<img src="http://m.tkk7.com/images/dot.gif" /> new generate<img src="http://m.tkk7.com/images/dot.gif" />.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />           instance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ServerThread();<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> instance;<br /><br />      }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Thread getThreadInstance() {<br /><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (threadInstance </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br /><br />            threadInstance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Thread(instance);<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> threadInstance;<br />    }<br /><br />。。。。。。。?br /><br />}<br /></span></div><br /></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Main {<br /><br />      main函数有servTr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ServerThread.getInstance().getThreadInstance();一?br /></span></div><br /></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;"></span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">代码没有问题吧;Z么他的单例每ơ都q入构造函数呢Q?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">l问之下才知道了他启动了两次</span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">Main</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">c;启动一?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">Main</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">cM在一?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">JVM</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">Q两ơ的单例怎么能是同一个呢Q!</span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><font color="#000000"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">CQ是在同一?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">JVM</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">中唯一Q不是在一个物理内存中唯一。当然后者不是不能做到的Q分布式对象嘛,呵呵?/span></font></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><br /><font color="#000000"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;"></span></font><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">W二点就是:单例的构?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">:<o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">目前方式多种Q我认ؓ只有两种是正的Q?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">W一Q?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;"><span lang="EN-US">private final static DaoFactory instance = new DaoFactory();<o:p></o:p></span></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">W二Q?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> DaoFactory instance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Properties props </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br /><br />       </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Map daos </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> DaoFactory() {<br /><br />           props </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Properties();<br /><br />           daos </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap();<br /><br />      }<br /><br />     <br /><br />      </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> DaoFactory getInstance() {<br /><br />           </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> instance) {<br /><br />                 </span><span style="color: rgb(0, 0, 255);">synchronized</span><span style="color: rgb(0, 0, 0);">(DaoFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">) {<br /><br />                      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> instance) {<br /><br />                            instance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> DaoFactory();<br /><br />                            instance.initDaoFactroy();<br /><br />                      }<br /><br />                 }<br /><br />           }<br /><br />           </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> instance;<br /><br />      }<br /><br /></span></div><br /><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">看清楚了Q这里用的是</span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">while</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">Q不?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">if</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><p class="MsoNormal" style="text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">Z么不能用</span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US">if</span><span style="font-size: 10pt; line-height: 150%; font-family: 宋体; color: black;">呢?下面的一个例子说?/span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> DomainFactory {<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> List provinces;<br /> <br />           </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Map domains;<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> DomainFactory instance;<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> DomainFactory() {<br /><br />           generateDomainTree();<br /><br />      }<br /><br />      </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> DomainFactory getInstance() {<br /><br />           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> instance) {<br /><br />                 </span><span style="color: rgb(0, 0, 255);">synchronized</span><span style="color: rgb(0, 0, 0);"> (DomainFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">) {<br /><br />                      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> instance) {<br /><br />                            instance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> DomainFactory();<br /><br />                      }<br /><br />                 }<br /><br />           }<br /><br />           </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> instance;<br /><br />      }<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> generateDomainTree() {<br /><br />。。。。?br /><br />}<br /><br /></span></div><br /><p class="MsoNormal" style="margin-left: 21pt; text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> CategoryFactory {<br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Map map;     <br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> CategoryFactory instance;<br />     <br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> CategoryFactory() {<br /><br />           map </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap();<br /><br />           generateCategoryTree();<br /><br />      }     <br /><br />      </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> CategoryFactory getInstance() {<br /><br />           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> instance) {<br /><br />                 </span><span style="color: rgb(0, 0, 255);">synchronized</span><span style="color: rgb(0, 0, 0);"> (CategoryFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">) {<br /><br />                      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> instance) {<br /><br />                            instance </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> CategoryFactory();<br /><br />                      }<br /><br />                 }<br /><br />           }<br /><br />           </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> instance;<br /><br />      }     <br /><br />      </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> generateCategoryTree() {<br /><br />。。。。。?br /><br />}<br /></span></div><br /> </o:p></span></p><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> SearchAction </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> DispatchAction {<br /><br />      </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> ActionForward getCatalogData(ActionMapping mapping, ActionForm form,<br /><br />                 HttpServletRequest request, HttpServletResponse response) {<br /><br />           DomainObject domainObject </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> DomainObject();<br /><br />           domainObject.setCity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">b</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />           request.getSession().setAttribute(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">domainObject</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, domainObject);<br /><br />           request.setAttribute(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">domains</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, DomainFactory.getInstance().getCity(domainObject.getCity()));<br /><br />           </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> mapping.findForward(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">left</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />      }    <br /> <br /><br />      </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> ActionForward getCatalogDataCategory(ActionMapping mapping, ActionForm form,<br /><br />                 HttpServletRequest request, HttpServletResponse response) {<br /><br />           request.setAttribute(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">productCategories</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, CategoryFactory.getInstance().getRootProductCategories());<br /><br />           </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> mapping.findForward(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />      }<br /></span></div><br /><p class="MsoNormal" style="margin-left: 42pt; text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">frameset rows</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">80,668*</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> cols</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> frameborder</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">no</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> border</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> framespacing</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /><br />    </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">frame name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">header</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> src</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">./search_header.jsp</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> scrolling</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">no</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> title</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">header</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> noresize</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">noresize</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /><br />    </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">frameset cols</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">180,*</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> frameborder</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">yes</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> border</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> framespacing</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /><br />       </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">frame name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">left</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> scrolling</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">no</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> src</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">search.html?method=getCatalogData</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> target</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">_self</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> noresize</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">noresize</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /><br />       </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">frame name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> scrolling</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">yes</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> src</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">search.html?method=getCatalogDataCategory</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> scrolling</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">yes</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> target</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">_self</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> noresize</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">noresize</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">frameset</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">frameset</span><span style="color: rgb(0, 0, 0);">></span></div><br /><p class="MsoNormal" style="margin-left: 21pt; text-indent: 21pt; line-height: 150%;"><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p> 用if 出错了吧?Z?同时q入了if(null == instance)的判?然后争夺?l果争夺不到的只能在争夺到后Ҏ出现问题,具体q里说不清楚,希望明白贴进一步讨Z<br /><p class="MsoNormal" style="margin-left: 21pt; text-indent: 21pt; line-height: 150%;"><span style="background: white none repeat scroll 0%; font-size: 10pt; line-height: 150%; font-family: "Courier New"; color: teal; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" lang="EN-US"></span><span style="font-size: 10pt; line-height: 150%; font-family: Verdana; color: black;" lang="EN-US"><o:p></o:p></span></p><img src ="http://m.tkk7.com/crazycy/aggbug/58268.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/crazycy/" target="_blank">crazycy</a> 2006-07-15 00:02 <a href="http://m.tkk7.com/crazycy/archive/2006/07/15/58268.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>QQ?Java技术联?W三ơ技术讨Z设计模式(完整?http://m.tkk7.com/crazycy/archive/2006/07/05/56866.htmlcrazycycrazycyWed, 05 Jul 2006 14:34:00 GMThttp://m.tkk7.com/crazycy/archive/2006/07/05/56866.htmlhttp://m.tkk7.com/crazycy/comments/56866.htmlhttp://m.tkk7.com/crazycy/archive/2006/07/05/56866.html#Feedback7http://m.tkk7.com/crazycy/comments/commentRss/56866.htmlhttp://m.tkk7.com/crazycy/services/trackbacks/56866.html
  • 首先介绍Ҏ论,参考了《大道至 ?软g工程实践者的思想?
  • DP与Spring、SOAcLQ皆Z实践
  • DP的必要性和要点Q辅助代码简要说?
  • 抛砖引玉Q介l几个DPQ?
      •   Factory Pattern
      • Decorator Pattern
      •  Proxy Pattern
      • SingLeton Pattern
      • Command Pattern
      • 其它
    • 介绍点:
      • a) 使用一些可与现实结合的生动例子Q引用网上资源)
      • 介绍其本质(个h理解Q?
      • 要介l其如何使用

  •                    <一>开q?/p>

    大家好,我是crazycyQ今天我们讨论的主题是Design Pattern

    首先感谢千年一叹的安排和付出,虽然他极其的忙?/p>

    今天的话题可能不很适合初学?对于初学者的可以看我的文?http://m.tkk7.com/crazycy/archive/2006/07/07/57106.html


                     <?gt; 设计模式的v?/p>

    在谈设计模式之前Q我们想一下:Z么会有设计模式?

    Christopher Alexander_每一个模式描qC一个在我们周围不断重复发生的问题以及该问题的解x案的核心?/p>

    ׃大多是做研发的,往往有自q体会Q遇到问?-解决问题Q这个过E中会发现有一些问题及其解x案不断变换面孔重复出玎ͼ其后面的有着共同的本质。实际上q些本质是模式?br />
    从哲学角度说Q设计模式的出现是实늻验的l晶Q是产生于实践ƈ指导实践?br />


                     <?gt;联想

    说到q里Q我们现在用的、谈的比较火爆的是Spring Hibernate之类的框架。是吧?

    Struts直接革新了Web Model 2的应用?br /> Hibernate直接解决了持久层的非面向对象的尴?br /> Spring直接挑战了原先的Java EE框架?br />
    q些都是源于实践。选择一U架构、一U技术的依据是什么? Rod Johnson 认ؓQ应该是Z实践的依据、来自历史项目或亲自试验的经验,而不是Q何Ş式的偶像崇拜或者门户之见?q个是@序方法:实践的反复ȝ。(十的哲学)

    Z么说Spring是实늚ȝQ大家可以看一下我的blog的一片文?a href="http://m.tkk7.com/crazycy/archive/2006/06/05/50519.aspx">http://m.tkk7.com/crazycy/archive/2006/06/05/50519.aspx  比较略?/p>


                    <?gt; 哲学

    周爱民先生的《大道至 ?软g工程实践者的思想》对Ҏ、方法论有如下描qͼ
    ============================================================================================================
    3. Ҏ

    推动q种逻辑向前发展的,?/span> Ҏ ?/span> Ҏ?/span> 的出现。长期的~程实践Q自然的归演与ȝQ必L淀为某U?/span> ( 软g开?/span> ) ҎQ于?/span> q程 出现了,于是 对象 出现了,于是相关的方法论也就出现了?/span>

    q是实践的成果。方法不是某个h或者某个组l创造的。瓜熟而蒂落,实践U篏辑ֈ一定的E度Q微软不提出某个ҎQ?/span> IBM 也会提出q个Ҏ。即便他们都不提出,可能你自己已l在使用q个Ҏ了?/span>

    Ҏq不秘Q因为它是你今天正在做的、从事的和实现的。正?/span> 模式 是一U方法,而模式就是你昨天书写代码的那个行为。只不过Q?/span> GoF 归纳、抽取、提升了q些行ؓ的内在规律?/span>

    你看不到你做事的行ؓQ也׃能理?/span> 模式 作ؓ一U方法的价倹{所以大师们众口一词:模式需要一定的~程l验才能理解?/span>

    同理Q理解过E也需要编E经验,理解对象也需要编E经验,理解 MDA ?/span> SOA q是需要编E经验?/span>

    —?/span> q可能就发生在你d你上一行代码编写的l过Q或者上一个项目失败的l历的那一瞬息。经验来源于回顾、理解与分析Q而不是你要写的下一行代码?/span>

    有h在寺院扫了一辈子的落叶而得道,也有人因Z句话而得道?/span>

    GoF 因ؓ无数ơ的代码回顾而得道?/span>

    感觉不过瘑֏以看一下我的blog文章Q?br /> http://m.tkk7.com/crazycy/archive/2006/05/24/47799.html

    而对于方法论Q查了一下大概阐q如下:Ҏ论是一套完整的实践引导体系Q是指导实践的理论基Q是Z在长期社会实践中ȝ和摸索出来的Q符合客观发展规律的Q符合h们对研究对象q行U学分析与判断的C会实践q程或步骤。方法是实践的成果,如同瓜熟蒂落Q实늧累达C定程度,必然会提出方法?/p>

                 
                <?gt;DP产生的必要性和目标

    ׃大家对OO~程都熟悉,OO的三个基本特征:装、ѝ多态?br />
    OO解决不了的问?-JavaZ子:
    java语言要求一个类实例化时Q必调用这个类的具体类的构造子Q所以java语言l出的类的实例化Ҏ无法做到只依赖抽象类型?br /> q造成了调用者合被调用者的一U紧耦合

    设计模式q求的目标是调用者和被调用者之间的解耦?/font>

    CZQ?br /> 消费一个对象的客户端只依赖于对象的抽象cdQ而不是具体类?/p>

    Ac:
    State state = new StartState();
    工厂c:
    public class StateFactory {
    public State static createState() {
          State state  =  new. StartState();
            return state;
        } 
    }
    Ac:
    State state = StateFactory.createState();
    q样很Ҏ理解工厂cd是生产对象,也就是创建对象,把对象的创徏q程从类A中摘取出?

    q样也就辑ֈ了DP的目标,也正是因个目标的提出Q才诞生了IoC?br /> 可以参考的我blog文章Q?a href="http://m.tkk7.com/crazycy/archive/2006/03/19/36068.aspx">http://m.tkk7.com/crazycy/archive/2006/03/19/36068.aspx


    q里有朋友问抽象工厂和工厂方法两个模式的区别Q?br /> 可以参考我的blog文章Q?a href="http://m.tkk7.com/crazycy/archive/2006/10/08/73858.html">http://m.tkk7.com/crazycy/archive/2006/10/08/73858.html


                        <?gt;介绍几个模式
    Decorator模式?br /> 1. 对真实对象进行包?
    2. 使其功能扩展
    3. 表现形式Qnew 包装c?Qnew 包装c?(真实对象)Q;q样真实对象具备了本n、包装类1、包装类2的功能;
    4. 实现时只需要把真实对象做ؓ包装cȝ聚合对象Q?
    实在不行想想MLӞ如果带上TT后,原来的不影响Q而且q安全了。主要就是包装原先的对象?/p>

    可参考我的blog文章Q?a href="http://m.tkk7.com/crazycy/archive/2006/04/30/44169.html">http://m.tkk7.com/crazycy/archive/2006/04/30/44169.html



    Dynamic Proxy模式?br /> 代理模式QProxyQ这里侧重于Dynamic ProxyQ可以理解成l一个对象提供一个代理对象,q个代理对象是把原对象q行包装Q其与调用处理器相兌?br /> 因ؓ代理对象和真实对象具有相同的接口Q客戯问时Q通过接口调用代理实例的方法,q个调用会被分发到该实例对应的处理器。处理器在把客户端调用传递给真实的对象之前或者之后,可执行某个操作,也可以选择不把q个调用传递给真实的对象?br /> 1. 真实对象包?
    2. 使其与调用处理类l定
    3. 表现形式QProxy.newProxyInstance(realObject.getClass(). getClassLoader(), realObject.getClass().getInterfaces(), new InvocationHandler(realObj))Q这L实对象就与InvocationHandlerc邦定了Q对外是一个代理类的Ş式;
    4. 实现时只需要把真实对象做ؓ调用处理器的聚合对象
    实在不行想惻I垂帘听政Q犹如清朝康熙年间的四大府臣Q很多权利不在皇帝手里,必须通过辅佐大臣dQ?br /> 具体CZ参加blogQ?br /> http://m.tkk7.com/crazycy/archive/2006/04/29/44006.html


    Command模式
    本质Q将调用操作的对??知道如何实现Ҏ作的对象 解耦?br /> 1 调用?br />  发出h的对象,h被包装成h对象的Ş式(侧重于对象)
    2 接收?br />  知道如何处理某个h的对象?br /> 3 兌?br />  具备调用者传入的h对象Q?br />    q且l定接收者?/p>

    大家都用qStruts吧?
    或许Struts的Action是一个很好的说明CZQ?br />  Public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)
    Ҏ的action是兌?br /> Ҏ的req可以理解成请求?br /> Ҏ的form 是h对象
    Ҏ的mapping是接收?/p>

     



    crazycy 2006-07-05 22:34 发表评论
    ]]>
    QQ?Java技术联?W三ơ技术讨Z设计模式(大纲)http://m.tkk7.com/crazycy/archive/2006/07/04/56595.htmlcrazycycrazycyTue, 04 Jul 2006 13:52:00 GMThttp://m.tkk7.com/crazycy/archive/2006/07/04/56595.htmlhttp://m.tkk7.com/crazycy/comments/56595.htmlhttp://m.tkk7.com/crazycy/archive/2006/07/04/56595.html#Feedback0http://m.tkk7.com/crazycy/comments/commentRss/56595.htmlhttp://m.tkk7.com/crazycy/services/trackbacks/56595.html
    1.    首先介绍Ҏ论,参考了《大道至—Y件工E者实c?br />2.    DP与Spring、SOAcLQ皆Z实践
    3.    DP的必要性和要点Q辅助代码简要说?br />4.    抛砖引玉Q介l几个DPQ?br />    a)    Factory Pattern
        b)    Decorator Pattern
        c)    Proxy Pattern
        d)    SingLeton Pattern
        e)    Command Pattern
        f)    其它看情?br />介绍点:
        a)    使用一些可与现实结合的生动例子
        b)    介绍其本质(个h理解Q?br />    c)    要介l其如何使用

    具体内容:


    crazycy 2006-07-04 21:52 发表评论
    ]]>
    我对设计模式的理解:模式的目?/title><link>http://m.tkk7.com/crazycy/archive/2006/07/03/56381.html</link><dc:creator>crazycy</dc:creator><author>crazycy</author><pubDate>Mon, 03 Jul 2006 09:06:00 GMT</pubDate><guid>http://m.tkk7.com/crazycy/archive/2006/07/03/56381.html</guid><wfw:comment>http://m.tkk7.com/crazycy/comments/56381.html</wfw:comment><comments>http://m.tkk7.com/crazycy/archive/2006/07/03/56381.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/crazycy/comments/commentRss/56381.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/crazycy/services/trackbacks/56381.html</trackback:ping><description><![CDATA[ <p class="MsoNormal"> <span style="font-size: 12pt; font-family: 宋体;">设计模式QDesign PatternQ追求的目标是调用者和被调用者之间的解耦?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </p> <span style="font-size: 12pt; font-family: 宋体;"> <br /> </span> <img src ="http://m.tkk7.com/crazycy/aggbug/56381.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/crazycy/" target="_blank">crazycy</a> 2006-07-03 17:06 <a href="http://m.tkk7.com/crazycy/archive/2006/07/03/56381.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我对设计模式的理解:命o模式QCommand PatternQ?/title><link>http://m.tkk7.com/crazycy/archive/2006/07/03/56380.html</link><dc:creator>crazycy</dc:creator><author>crazycy</author><pubDate>Mon, 03 Jul 2006 09:05:00 GMT</pubDate><guid>http://m.tkk7.com/crazycy/archive/2006/07/03/56380.html</guid><wfw:comment>http://m.tkk7.com/crazycy/comments/56380.html</wfw:comment><comments>http://m.tkk7.com/crazycy/archive/2006/07/03/56380.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/crazycy/comments/commentRss/56380.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/crazycy/services/trackbacks/56380.html</trackback:ping><description><![CDATA[ <font size="3"> <span style="font-size: 12pt; font-family: 宋体;">本质Q将调用操作的对?/span> <span style="font-size: 12pt;"> </span> <span style="font-size: 12pt; font-family: 宋体;">?/span> <span style="font-size: 12pt;"> </span> <span style="font-size: 12pt; font-family: 宋体;">知道如何实现Ҏ作的对象</span> <span style="font-size: 12pt;"> </span> <span style="font-size: 12pt; font-family: 宋体;">解耦?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US">1 </span> <span style="font-size: 12pt; font-family: 宋体;">调用?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US"> <span style="">       </span> </span> <span style="font-size: 12pt; font-family: 宋体;">发出h   的对象,h被包装成h对象的Ş式(重点是封装成了请求对象)</span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US">2 </span> <span style="font-size: 12pt; font-family: 宋体;">接收?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US"> <span style="">       是  ?</span> </span> <span style="font-size: 12pt; font-family: 宋体;">知道如何处理某个h   的对象?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US">3 </span> <span style="font-size: 12pt; font-family: 宋体;">兌?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <ul> <li> <font size="3"> <span style="font-size: 12pt;" lang="EN-US"> <span style="">   </span> </span> <span style="font-size: 12pt; font-family: 宋体;">h调用者传入的h对象Q?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </li> </ul> <ul> <li> <font size="3"> <span style="font-size: 12pt;" lang="EN-US"> <span style="">   </span> </span> <span style="font-size: 12pt; font-family: 宋体;">q且l定接收者?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </li> </ul> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt; font-family: 宋体;"> <br /> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt; font-family: 宋体;">或许</span> <span style="font-size: 12pt;" lang="EN-US">Struts</span> <span style="font-size: 12pt; font-family: 宋体;">的Action是一个很好的说明CZQ?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p>Public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)<o:p></o:p></span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US"> <o:p>Ҏ?/o:p>action</span> <span style="font-size: 12pt; font-family: 宋体;">是兌?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US">Ҏ的req可以理解成请求?br /></span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US">Ҏ的form </span> <span style="font-size: 12pt; font-family: 宋体;">是h对象</span> <span style="font-size: 12pt;" lang="EN-US"> <br /> </span> </font> </p> <p class="MsoNormal"> <font size="3"> <span style="font-size: 12pt;" lang="EN-US">Ҏ的mapping</span> <span style="font-size: 12pt; font-family: 宋体;">是接收?/span> <span style="font-size: 12pt;" lang="EN-US"> <o:p> </o:p> </span> </font> </p> <img src ="http://m.tkk7.com/crazycy/aggbug/56380.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/crazycy/" target="_blank">crazycy</a> 2006-07-03 17:05 <a href="http://m.tkk7.com/crazycy/archive/2006/07/03/56380.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最q突然觉得设计模式很?/title><link>http://m.tkk7.com/crazycy/archive/2006/05/29/48689.html</link><dc:creator>crazycy</dc:creator><author>crazycy</author><pubDate>Mon, 29 May 2006 02:21:00 GMT</pubDate><guid>http://m.tkk7.com/crazycy/archive/2006/05/29/48689.html</guid><wfw:comment>http://m.tkk7.com/crazycy/comments/48689.html</wfw:comment><comments>http://m.tkk7.com/crazycy/archive/2006/05/29/48689.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/crazycy/comments/commentRss/48689.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/crazycy/services/trackbacks/48689.html</trackback:ping><description><![CDATA[    GoFQJava与模式摆在电脑桌旁已有半q_说实话就是把GoF看了不到10늚内容Q一直束之高阁?br /><br />    在大概一q半左右的开发后Q代码量累计臛_?.5wQ无意间打开GoF的Design PatternQ发现许多思想真的很美Q如同一有|发奇香的花?br />    <br />    qx大量的单例用,Singleton模式再次更新了一些认?<br /><br />    因ؓAOP的服务截?让我痴迷上动态代理(Dynamic ProxyQ;<br /><br />    因ؓ多层l承的不方便Q让我很是欢喜装饰模式(DecoratorQ;而且我还用男女“欢乐”之事给朋友Q男Q生动介l这个模式的本质?br /><br />    因ؓStruts的action机制Q以及web modle的发展历E,让我很是感慨于命令模式(CommandQ?br />    <br />    对这几个模式p的喜qQ也在脑中不断品味Q呵呵,记录一下,为日后更加成熟认识备案初涉之痕迹?br /><img src ="http://m.tkk7.com/crazycy/aggbug/48689.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/crazycy/" target="_blank">crazycy</a> 2006-05-29 10:21 <a href="http://m.tkk7.com/crazycy/archive/2006/05/29/48689.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我对设计模式的理解:观察者模式(Observer PatternQ?/title><link>http://m.tkk7.com/crazycy/archive/2006/05/29/48685.html</link><dc:creator>crazycy</dc:creator><author>crazycy</author><pubDate>Mon, 29 May 2006 02:01:00 GMT</pubDate><guid>http://m.tkk7.com/crazycy/archive/2006/05/29/48685.html</guid><wfw:comment>http://m.tkk7.com/crazycy/comments/48685.html</wfw:comment><comments>http://m.tkk7.com/crazycy/archive/2006/05/29/48685.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/crazycy/comments/commentRss/48685.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/crazycy/services/trackbacks/48685.html</trackback:ping><description><![CDATA[ <p class="MsoNormal" style="TEXT-INDENT: 21pt; LINE-HEIGHT: 150%"> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">?/span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial">GoF P194 5.7Section?/span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">动机</span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial">?/span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">中有应用的描qͼ如果对应用的</span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial">Obersers</span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">q行再一ơ抽象,其可以承一个公用的接口</span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial">Listener</span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">。这是这一步抽象,对这个模式将有更好的理解?/span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <?xml:namespace prefix = o /?> <o:p> </o:p> </span> </p> <p class="MsoNormal" style="TEXT-INDENT: 21pt; LINE-HEIGHT: 150%"> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">而最初在接触观察者模式前Q我p了大?天左右的旉Q一直在考虑Java提供的事件机Ӟ其是GUI斚w的用,因ؓ对GUI可以说是我的一个盲点,因此试从思想上有所截获。最l我归纳如下Q?/span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <o:p> </o:p> </span> </p> <p class="MsoNormal" style="MARGIN-LEFT: 39pt; TEXT-INDENT: -18pt; LINE-HEIGHT: 150%"> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <span>1.<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">      </span></span> </span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">监听者注册即调用旉源的注册ҎQƈ以监听者接口ؓ参数Q?/span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <o:p> </o:p> </span> </p> <p class="MsoNormal" style="MARGIN-LEFT: 39pt; TEXT-INDENT: -18pt; LINE-HEIGHT: 150%"> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <span>2.<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">      </span></span> </span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">事g源通知卌用监听者方法传递消息;</span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <o:p> </o:p> </span> </p> <p class="MsoNormal" style="MARGIN-LEFT: 39pt; TEXT-INDENT: -18pt; LINE-HEIGHT: 150%"> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <span>3.<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">      </span></span> </span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">通知的消息可以是一个事件对象;</span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <o:p> </o:p> </span> </p> <p class="MsoNormal" style="MARGIN-LEFT: 39pt; TEXT-INDENT: -18pt; LINE-HEIGHT: 150%"> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <span>4.<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">      </span></span> </span> <span style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体">通知的方式就是监听者接口的ҎQ且以事件对象ؓ参数?/span> <span lang="EN-US" style="LINE-HEIGHT: 150%; FONT-FAMILY: Arial"> <o:p> </o:p> </span> </p>   现在读GoF的Observer描述Q很是有醍醐灌顶之感?br />    <br />   不过如果没有那段旉的摸索,估计难有如此的体验?br /><img src ="http://m.tkk7.com/crazycy/aggbug/48685.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/crazycy/" target="_blank">crazycy</a> 2006-05-29 10:01 <a href="http://m.tkk7.com/crazycy/archive/2006/05/29/48685.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我对设计模式的理解:Decorator模式与Dynamic Proxy模式http://m.tkk7.com/crazycy/archive/2006/04/30/44169.htmlcrazycycrazycySun, 30 Apr 2006 04:51:00 GMThttp://m.tkk7.com/crazycy/archive/2006/04/30/44169.htmlhttp://m.tkk7.com/crazycy/comments/44169.htmlhttp://m.tkk7.com/crazycy/archive/2006/04/30/44169.html#Feedback0http://m.tkk7.com/crazycy/comments/commentRss/44169.htmlhttp://m.tkk7.com/crazycy/services/trackbacks/44169.htmlDecorator模式?/p>
    1. 对真实对象进行包?
    2. 使其功能扩展
    3. 表现形式Qnew 包装c?Qnew 包装c?(真实对象)Q;q样真实对象具备了本n、包装类1、包装类2的功能;实现时只需要把真实对象做ؓ包装cȝ聚合对象Q?

     

    Dynamic Proxy模式?/p>

    1. 真实对象包?
    2. 使其与调用处理类l定
    3. 表现形式QProxy.newProxyInstance(realObject.getClass().getClassLoader(), realObject.getClass().getInterfaces(), new InvocationHandler(realObj))Q这L实对象就与InvocationHandlerc邦定了Q对外是一个代理类的Ş式;实现时只需要把真实对象做ؓ调用处理器的聚合对象

     

    ==============以下是引用,2007q?5?1日增?==============

    首先我们来明一下动态代理的定义Q?font color=#000080>一个动态代理类在运行期implements一linterfaceQ得interface实现cȝҎ调用被分z至其他的类(另外的interface实现cL者Q意的c?的方?/font>。讲得更通俗一些,要了解动态代理,我们p知道什么东西动态了Q代理了什么?首先Q一个Proxy代理了一linterface的方法。注意,代理的是interfaceQ而不是ClassQ也不是abstract ClassQ其ơ,Proxyh的型别由l定的interface所军_?/font>Q动态就体现在此。也许看着q样的定义,q是会一头雾_那么我们d图来看看吧?

    dynamicProxy.gif

    从图中,我们可以看到Dynamic Proxyq没有实现Resourceq个接口Q但是包含了Resource接口实现cȝ实例Q在Dynamic Proxy的createҎ中,通过调用Proxy.newProxyInstance创徏一个ProxyQƈ该Proxy与Resource接口l定Q最后将Proxy昑ּcd转换成Resource接口cdq返回,q样调用者就可以通过Proxy调用interface定义的方法了Q由于Proxy与Resource接口l定了,对Resource接口的方法调用,都会交由Proxy的invokeҎd理。而invokeҎ会根据不同的ҎQ或l以全新的实玎ͼ或直接将Ҏ调用交给Proxy中包含的Resource接口实现cȝ实例d理。综合上面所说的Q作Z个Dynamic ProxyQ它必须满以下三个条gQ?
            1、实CInvocationHandler接口Q实现接口中定义的invokeҎQ?
            2、包含接口实现类的实例;
            3、通过Proxy.newProxyInstanceҎ实现Proxy与接口之间的l定?
            以下代码l出了一个简单的Dynamic Proxy实现Q?

    public interface Resource {
        
    public void
     operationA();
        
    public void
     operationB();
    }

                                                    

    public class ConcreteResource implements Resource {
        
    public void operationA() 
    {
            System.
    out.println("Operation A."
    );
        }

        
    public void operationB() {
            System.
    out.println("Operation B."
    );
        }

    }

                                                    

    public class DynamicProxy implements InvocationHandler {
        
    private
     Resource resource;

        
    public DynamicProxy() 
    {
            resource 
    = new
     ConcreteResource();
        }


        
    public Resource create() {
            Resource returnResource 
    = null
    ;
            returnResource 
    = (Resource) Proxy.newProxyInstance(Resource.class

                    .getClassLoader(), 
    new Class[] { Resource.class }
            this
    );
            
    return
     returnResource;
        }


        
    public Object invoke(Object obj, Method method, Object[] args) {
            Object o 
    = null
    ;
            
    try 
    {
                
    if (method.getName().equals("operationA")) 
    {
                    System.
    out.println("OperationA in Proxy"
    );
                }
     else {
                    o 
    =
     method.invoke(obj, args);
                }

            }
     catch (Exception e) {
                e.printStackTrace();        }

            
    return o;
        }

    }

                                                 

    public class Test {
        
    public static void main(String[] args) 
    {
            DynamicProxy proxy 
    = new
     DynamicProxy();
            Resource resource 
    =
     proxy.create();
            resource.operationA();
        }

    }



            目前整个开发社区对AOP(Aspect Oriented Programing)推崇备至Q也涌现出大量支持AOP的优UFramework,--Spring, JAC, Jboss AOP {等。AOPg一时之间成了潮。Java初学者不要发出感慨QOOPq没有学通呢Q又来AOP。本文不是要在理Z具体阐述何ؓAOP, Z要进行AOP . 要详l了解学习AOP可以到它老家http://aosd.netȝ瞧。这里只是意N过一个简单的例子向初学者展CZ下如何来q行AOP.

      Z单v见,例子没有没有使用MW三方的AOP Framework, 而是利用Java语言本n自带的动态代理功能来实现AOP.

      让我们先回到AOP本nQAOP主要应用于日志记录,性能l计Q安全控?事务处理{方面。它的主要意囑ְ要将日志记录Q性能l计Q安全控制等{代码从商业逻辑代码中清楚的划分出来Q我们可以把q些行ؓ一个一个单独看作系l所要解决的问题Q就是所谓的面向问题的编E?不知AOP译作面向问题的编E是否欠?。通过对这些行为的分离Q我们希望可以将它们独立地配|到商业Ҏ中,而要改变q些行ؓ也不需要媄响到商业Ҏ代码?

      假设pȝ׃pd的BusinessObject所完成业务逻辑功能Q系l要求在每一ơ业务逻辑处理时要做日志记录。这里我们略d体的业务逻辑代码?

    public interface BusinessInterface {
     public void processBusiness();
    }

    public class BusinessObject implements BusinessInterface {
     private Logger logger = Logger.getLogger(this.getClass().getName());
     public void processBusiness(){
      try {
       logger.info("start to processing...");
       //business logic here.
       System.out.println(“here is business logic”);
       logger.info("end processing...");
      } catch (Exception e){
       logger.info("exception happends...");
       //exception handling
      }
     }
    }

      q里处理商业逻辑的代码和日志记录代码混合在一Pq给日后的维护带来一定的困难Qƈ且也会造成大量的代码重复。完全相同的log代码出现在pȝ的每一个BusinessObject中?

      按照AOP的思想Q我们应该把日志记录代码分离出来。要这些代码分d涉及C个问题,我们必须知道商业逻辑代码何时被调用,q样我们好插入日志记录代码。一般来说要截获一个方法,我们可以采用回调Ҏ或者动态代理。动态代理一般要更加灉|一些,目前多数的AOP Framework也大都采用了动态代理来实现。这里我们也采用动态代理作Z子?

      JDK1.2以后提供了动态代理的支持Q程序员通过实现java.lang.reflect.InvocationHandler接口提供一个执行处理器Q然后通过java.lang.reflect.Proxy得到一个代理对象,通过q个代理对象来执行商业方?在商业方法被调用的同Ӟ执行处理器会被自动调用?

      有了JDK的这U支持,我们所要做的仅仅是提供一个日志处理器?

    public class LogHandler implements InvocationHandler {

     private Logger logger Q?Logger.getLogger(this.getClass().getName());
      private Object delegate;
      public LogHandler(Object delegate){
       this.delegate = delegate;
      }

     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      Object o = null;
      try {
       logger.info("method stats..." Q?method);
       o = method.invoke(delegate,args);
       logger.info("method ends..." + method);
      } catch (Exception e){
       logger.info("Exception happends...");
       //excetpion handling.
      }
      return o;
     }
    }

      现在我们可以把BusinessObject里面的所有日志处理代码全部去掉了?

    public class BusinessObject implements BusinessInterface {

     private Logger logger = Logger.getLogger(this.getClass().getName());
     public void processBusiness(){
      //business processing
      System.out.println(“here is business logic”);
     }
    }

      客户端调用商业方法的代码如下Q?br>
    BusinessInterface businessImp = new BusinessObject();

    InvocationHandler handler = new LogHandler(businessImp);

    BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance(
     businessImp.getClass().getClassLoader(),
     businessImp.getClass().getInterfaces(),
     handler);

    proxy.processBusiness();

      E序输出如下Q?

    INFO: method stats...
    here is business logic
    INFO: method ends...

      x我们的第一ơ小试是完成了。可以看刎ͼ采用AOP之后Q日志记录和业务逻辑代码完全分开了,以后要改变日志记录的话只需要修Ҏ志记录处理器p了,而业务对象本w(BusinessObjectQ无需做Q何修攏Vƈ且这个日志记录不会造成重复代码了,所有的商业处理对象都可以重用这个日志处理器?

      当然在实际应用中Q这个例子就昑־太粗p了。由于JDK的动态代理ƈ没有直接支持一ơ注册多个InvocationHandlerQ那么我们对业务处理Ҏ既要日志记录又要性能l计Ӟ需要自己做一些变通了。一般我们可以自己定义一个Handler接口Q然后维护一个队列存放所有Handler, 当InvocationHandler被触发的时候我们依ơ调用自qHandler。所q的是目前几乎所有的AOP Framework都对q方面提供了很好的支?q里推荐大家使用Spring?br>


    crazycy 2006-04-30 12:51 发表评论
    ]]>
    我对设计模式的理解:Dynamic Proxy模式http://m.tkk7.com/crazycy/archive/2006/04/29/44006.htmlcrazycycrazycySat, 29 Apr 2006 06:12:00 GMThttp://m.tkk7.com/crazycy/archive/2006/04/29/44006.htmlhttp://m.tkk7.com/crazycy/comments/44006.htmlhttp://m.tkk7.com/crazycy/archive/2006/04/29/44006.html#Feedback0http://m.tkk7.com/crazycy/comments/commentRss/44006.htmlhttp://m.tkk7.com/crazycy/services/trackbacks/44006.html代理模式QProxyQ这里侧重于Dynamic ProxyQ可以理解成l一个对象提供一个代理对象,q个代理对象是把原对象q行包装Q其与调用处理器相兌?br />    因ؓ代理对象和真实对象具有相同的接口Q客戯问时Q通过接口调用代理实例的方法,q个调用会被分发到该实例对应的处理器。处理器在把客户端调用传递给真实的对象之前或者之后,可执行某个操作,也可以选择不把q个调用传递给真实的对象?br />   
       产生代理对象的过E就是将原对象和调用处理器邦定的q程Q如下:
    Proxy.newProxyInstance(真实对象实例.getClass().getClassLoader(), 真实对象实例.getClass().getInterfaces(), 与真实对象关联的调用处理器实?;

       CZQ?br />public interface AnInterface {
     void say();
    }


    public class AClass implements AnInterface {
     public AClass() {
      System.out.println("AClass:AClass()");
     }
     public void say() {
      System.out.println("AClass:say()");
     }
    }


    public class MyHandler implements InvocationHandler {
     private Object realObj;
     private MyHandler() {
      }
     public Object bind(Object realObj) {
        this.realObj = realObj;
        return Proxy.newProxyInstance(realObj.getClass().getClassLoader(),
        realObj.getClass().getInterfaces(), this);
     }
     public Object invoke(Object proxy, Method md, Object[] args) throws Throwable {
      Object obj = null;
      System.out.println("invoke()");
      obj = md.invoke(realObj, args);
      return obj;
     }
    }

    public class Test {
     public static void main(String[] args) {
      MyHandler  handler = new MyHandler ();
      AnInterface proxyObj = handler.bind(new AClass());
      proxyObj.say();
     }
    }



    crazycy 2006-04-29 14:12 发表评论
    ]]>
    վ֩ģ壺 þþƷavպ| þþþ뾫Ʒպ| ۺվ| ³ʦӰԺѹۿ| 91޵ҹ| 91ɫƵ޹ۿ| ޹ղ뾫Ʒ| ҹAëƬ| av߹ۿַ| ֻˬһëƬѹۿ| Ƭѹۿ92ƵƵ| ߹ۿavÿո| ƷƵѹۿ| ձ߲| ߲ѲavƬ| óۺ| һ| þۺϹ׾Ʒ | ҹҹAһ| aƷžžŴƬѿ| ޺Сvideos| ŮƵ77777| Ʒާѡ벥Ų| ˾Ʒձר| ˾þAV| ҹ˾Ʒɫ| ޹˾þһþ| aҹëƬһ| ޹ŮƷþþþ| ÿ߹ۿapp| AAAAAٸ߳Ƭѿ| Ļ޾ƷԴ| ƷƵվ| aëƬȫ| ɫ|| Ļ뾫ƷԴ| ĻȫĻ| avһ߲ | av³˿һ| վѹۿ޹| ޵һƵ߹ۿ|