锘??xml version="1.0" encoding="utf-8" standalone="yes"?>一区二区激情,综合色婷婷一区二区亚洲欧美国产,日韩av有码http://www.aygfsteel.com/renyangok/category/11736.html鍥炲綊blogjavazh-cnFri, 14 May 2010 09:39:21 GMTFri, 14 May 2010 09:39:21 GMT60Interpreter錛堣В閲婂櫒錛夋ā寮?/title><link>http://www.aygfsteel.com/renyangok/articles/61157.html</link><dc:creator>淇濆皵浠?/dc:creator><author>淇濆皵浠?/author><pubDate>Tue, 01 Aug 2006 03:45:00 GMT</pubDate><guid>http://www.aygfsteel.com/renyangok/articles/61157.html</guid><wfw:comment>http://www.aygfsteel.com/renyangok/comments/61157.html</wfw:comment><comments>http://www.aygfsteel.com/renyangok/articles/61157.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/renyangok/comments/commentRss/61157.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/renyangok/services/trackbacks/61157.html</trackback:ping><description><![CDATA[ <p>Interpreter瑙i噴鍣ㄦā寮忓畾涔?<br />瀹氫箟璇█鐨勬枃娉?,騫朵笖寤虹珛涓€涓В閲婂櫒鏉ヨВ閲婅璇█涓殑鍙ュ瓙.錛堝悗闈笉鐢ㄧ湅浜嗭級</p> <p>Interpreter浼間箮浣跨敤闈笉鏄緢騫?瀹冩弿榪頒簡涓€涓璦€瑙i噴鍣ㄦ槸濡備綍鏋勬垚鐨?鍦ㄥ疄闄呭簲鐢ㄤ腑,鎴戜滑鍙兘寰堝皯鍘繪瀯閫犱竴涓璦€鐨勬枃娉?鎴戜滑榪樻槸鏉ョ畝鍗曠殑浜嗚В涓€涓?</p> <p>棣栧厛瑕佸緩绔嬩竴涓帴鍙?鐢ㄦ潵鎻忚堪鍏卞悓鐨勬搷浣?</p> <p>public interface AbstractExpression { <br />銆€銆€ void interpret( Context context ); <br />} </p> <p>鍐嶇湅鐪嬪寘鍚В閲婂櫒涔嬪鐨勪竴浜涘叏灞€淇℃伅</p> <p>public interface Context { } </p> <p>AbstractExpression鐨勫叿浣撳疄鐜板垎涓ょ:緇堢粨絎﹁〃杈懼紡鍜岄潪緇堢粨絎﹁〃杈懼紡:</p> <p>public class TerminalExpression implements AbstractExpression { <br />銆€銆€ public void interpret( Context context ) { }<br />} </p> <p>瀵逛簬鏂囨硶涓瘡涓€鏉¤鍒?闈炵粓緇撶琛ㄨ揪寮忛兘蹇呴』鐨?<br />public class NonterminalExpression implements AbstractExpression { <br />銆€銆€ private AbstractExpression successor; <br />銆€銆€<br />銆€銆€ public void setSuccessor( AbstractExpression successor ) { <br />銆€銆€銆€銆€ this.successor = successor; <br />銆€銆€ } <br /><br />銆€銆€ public AbstractExpression getSuccessor() { <br />銆€銆€銆€銆€ return successor; <br />銆€銆€ }<br /><br />銆€銆€ public void interpret( Context context ) { } <br />} </p> <img src ="http://www.aygfsteel.com/renyangok/aggbug/61157.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/renyangok/" target="_blank">淇濆皵浠?/a> 2006-08-01 11:45 <a href="http://www.aygfsteel.com/renyangok/articles/61157.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Memento錛堝鏈涘綍錛夋ā寮?/title><link>http://www.aygfsteel.com/renyangok/articles/61141.html</link><dc:creator>淇濆皵浠?/dc:creator><author>淇濆皵浠?/author><pubDate>Tue, 01 Aug 2006 02:28:00 GMT</pubDate><guid>http://www.aygfsteel.com/renyangok/articles/61141.html</guid><wfw:comment>http://www.aygfsteel.com/renyangok/comments/61141.html</wfw:comment><comments>http://www.aygfsteel.com/renyangok/articles/61141.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/renyangok/comments/commentRss/61141.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/renyangok/services/trackbacks/61141.html</trackback:ping><description><![CDATA[ <p>Memento澶囨湜褰曟ā寮忓畾涔?<br />memento鏄竴涓繚瀛樺彟澶栦竴涓璞″唴閮ㄧ姸鎬佹嫹璐濈殑瀵硅薄.榪欐牱浠ュ悗灝卞彲浠ュ皢璇ュ璞℃仮澶嶅埌鍘熷厛淇濆瓨鐨勭姸鎬?</p> <p>Memento妯″紡鐩稿涔熸瘮杈冨ソ鐞嗚В,鎴戜滑鐪嬩笅鍒椾唬鐮?</p> <table cellspacing="3" cellpadding="3" width="80%" border="0"> <tbody> <tr> <td bgcolor="#cccccc">public class Originator { <br /><br />銆€銆€ private int number; <p>銆€銆€private File file = null;</p><p>銆€銆€public Originator(){}</p><p>銆€銆€// 鍒涘緩涓€涓狹emento<br />銆€銆€public Memento getMemento(){<br />銆€銆€銆€銆€return new Memento(this);<br />銆€銆€}</p><p>銆€銆€// 鎭㈠鍒板師濮嬪€?br />銆€銆€public void setMemento(Memento m){<br />銆€銆€銆€銆€ number = m.number;<br />銆€銆€銆€銆€ file = m.file;<br />銆€銆€}</p><p>}</p><p>聽</p></td> </tr> </tbody> </table> <p>鎴戜滑鍐嶇湅鐪婱emento綾?</p> <table cellspacing="3" cellpadding="3" width="80%" border="0"> <tbody> <tr> <td bgcolor="#cccccc"> <p>private class Memento implements java.io.Serializable{</p> <p>銆€銆€private int number;</p> <p>銆€銆€private File file = null;</p> <p>銆€銆€public Memento( Originator o){</p> <p>銆€銆€銆€銆€number = o.number;<br />銆€銆€銆€銆€file = o.file;</p> <p>銆€銆€}</p> <p>}</p> <p> <br /> </p> </td> </tr> </tbody> </table> <p>鍙 Memento涓繚瀛樹簡Originator涓殑number鍜宖ile鐨勫€? 閫氳繃璋冪敤Originator涓璶umber鍜宖ile鍊兼敼鍙樼殑璇?閫氳繃璋冪敤setMemento()鏂規硶鍙互鎭㈠.</p> <p>Memento妯″紡鐨勭己鐐規槸鑰楄垂澶?濡傛灉鍐呴儴鐘舵€佸緢澶?鍐嶄繚瀛樹竴浠?鏃犳剰瑕佹氮璐瑰ぇ閲忓唴瀛?</p> <p> <b>Memento妯″紡鍦↗sp+Javabean涓殑搴旂敤</b> <br />鍦↗sp搴旂敤涓?鎴戜滑閫氬父鏈夊緢澶氳〃鍗曡姹傜敤鎴瘋緭鍏?姣斿鐢ㄦ埛娉ㄥ唽,闇€瑕佽緭鍏ュ鍚嶅拰Email絳? 濡傛灉涓€浜涜〃欏圭敤鎴鋒病鏈夊~鍐欐垨鑰呭~鍐欓敊璇?鎴戜滑甯屾湜鍦ㄧ敤鎴鋒寜"鎻愪氦Submit"鍚?閫氳繃Jsp紼嬪簭媯€鏌?鍙戠幇紜疄鏈夋湭濉啓欏圭洰,鍒欏湪璇ラ」鐩笅綰㈠瓧鏄劇ず璀﹀憡鎴栭敊璇?鍚屾椂,榪樿鏄劇ず鐢ㄦ埛鍒氭墠宸茬粡杈撳叆鐨勮〃欏?</p> <p>濡備笅鍥句腑 First Name鏄敤鎴峰凡緇忚緭鍏?Last Name娌℃湁杈撳叆,鎴戜滑鍒欐彁紺虹孩瀛楄鍛?:</p> <p> <img height="500" src="http://www.jdon.com/designpatterns/images/memento.gif" width="425" /> </p> <p>榪欑鎶€鏈殑瀹炵幇,灝辨槸鍒╃敤浜咼avabean鐨剆cope="request"鎴杝cope="session"鐗規€?涔熷氨鏄疢emento妯″紡.<br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞<br />瀹炰緥錛?br />緇忓父浣跨敤璁$畻鏈虹殑浜烘亹鎬曞緋葷粺澶囦喚錛圡emento錛変笉浼氶檶鐢燂紝褰撲綘鐨刉indows緋葷粺榪愯姝e父鏃訛紝瀵瑰畠榪涜澶囦喚錛屽綋緋葷粺榪愯鏈夐棶棰樻椂錛屽氨鍙互璋冪敤澶囦喚蹇€熺殑灝嗙郴緇熸仮澶嶏紝榪欐牱灝卞彲浠ュぇ閲忚妭鐪侀噸鏂拌緋葷粺鐨勭棝鑻︼紝鐗瑰埆鏄綋浣犵己灝戞煇涓€椹卞姩錛屾垨鍦ㄨ緋葷粺鏄嚭鐜頒竴浜涙€棶棰樻椂錛岀姽涓虹棝鑻︺€傛垜鎯蟲湁榪囪繖縐嶇粡鍘嗙殑浜哄簲璇ュ緢浜嗚В鍚э紝鍛靛懙錛?br />濂戒簡錛屼笅闈㈣鎴戜滑鐪嬬湅榪欎釜榪囩▼璇ュ浣曞疄鐜板惂錛?<br />聽<br />1銆佹垜浠厛瀹氫箟Windows緋葷粺錛圵indowsSystem錛夌被錛?br />聽<br />public class WindowsSystem {<br />聽 private String state;<br />聽 public Memento createMemento() {聽 //鍒涘緩澶囦喚錛屼繚瀛樺綋鍓嶇姸鎬?br />聽聽聽 return new Memento(state);<br />聽 }<br />聽 public void restoreMemento(Memento memento){ //浠庡浠戒腑鎭㈠緋葷粺<br />聽聽聽 this.state=memento.getState();<br />聽 }<br />聽 public String getState(){聽 //鑾峰緱鐘舵€?br />聽聽聽 return this.state;<br />聽 }<br />聽 public void setState(String state){聽 //璁劇疆鐘舵€?br />聽聽聽 this.state=state;<br />聽聽聽 System.out.println("褰撳墠緋葷粺澶勪簬"+this.state);<br />聽 }<br />}<br />2銆佸啀瀹氫箟澶囦喚錛圡emento錛夌被錛?br />public class Memento {<br />聽 private String state;<br />聽 public Memento(String state) {聽 //澶囦喚<br />聽聽聽 this.state=state;<br />聽 }<br />聽 public String getState(){ //鑾峰緱鐘舵€?br />聽聽聽 return this.state;<br />聽 }<br />聽 public void setState(String state){聽 //璁劇疆鐘舵€?br />聽聽聽 this.state=state;<br />聽 }<br />}<br />3銆佸畾涔夌敤鎴鳳紙User錛夌被錛?br />public class User {<br />聽 private Memento memento;<br />聽 public Memento retrieveMemento() {聽 //鎭㈠緋葷粺<br />聽聽聽 return this.memento;<br />聽 }<br />聽 public void saveMemento(Memento memento){聽 //淇濆瓨緋葷粺<br />聽聽聽 this.memento=memento;<br />聽 }<br />}<br />4銆佺紪鍐欐祴璇曠被錛?br />public class Test {<br />聽 public static void main(String args[]) {聽聽聽 <br />聽聽聽 WindowsSystem Winxp = new WindowsSystem(); //Winxp緋葷粺<br />聽聽聽 User user = new User();聽聽 //鏌愪竴鐢ㄦ埛<br />聽聽聽 Winxp.setState("濂界殑鐘舵€?);聽聽 //Winxp澶勪簬濂界殑榪愯鐘舵€?br />聽聽聽 user.saveMemento(Winxp.createMemento()); //鐢ㄦ埛瀵圭郴緇熻繘琛屽浠斤紝Winxp緋葷粺瑕佷駭鐢熷浠芥枃浠?br />聽聽聽 Winxp.setState("鍧忕殑鐘舵€?);聽聽 //Winxp澶勪簬涓嶅ソ鐨勮繍琛岀姸鎬?br />聽聽聽 Winxp.restoreMemento(user.retrieveMemento());聽聽 //鐢ㄦ埛鍙戞仮澶嶅懡浠わ紝緋葷粺榪涜鎭㈠<br />聽聽聽 System.out.println("褰撳墠緋葷粺澶勪簬"+Winxp.getState());<br />聽 }<br />}<br />5銆佽鏄庯細<br />A錛氬畾涔夛細Memento瀵硅薄鏄竴涓繚瀛樺彟澶栦竴涓璞″唴閮ㄧ姸鎬佹嫹璐濈殑瀵硅薄錛岃繖鏍蜂互鍚庡氨鍙互灝嗚瀵硅薄鎭㈠鍒板師鍏堜繚瀛樼殑鐘舵€併€?br />B錛歁emento妯″紡鐨勭敤鎰忔槸鍦ㄤ笉鐮村潖灝佽鐨勬潯浠朵笅錛屽皢涓€涓璞$殑鐘舵€佹崟鎹変綇錛屽茍澶栭儴鍖栵紝瀛樺偍璧鋒潵錛屼粠鑰屽彲浠ュ湪灝嗘潵鍚堥€傜殑鏃跺€欐妸榪欎釜瀵硅薄榪樺師鍒板瓨鍌ㄨ搗鏉ョ殑鐘舵€併€?br />C錛歁emento妯″紡鎵€娑夊強鐨勮鑹叉湁涓変釜錛屽蹇樺綍瑙掕壊銆佸彂璧蜂漢瑙掕壊鍜岃礋璐d漢瑙掕壊銆?br />澶囧繕褰曡鑹茬殑浣滅敤錛?br />錛?錛壜犅犅犅犅犅?灝嗗彂璧蜂漢瀵硅薄鐨勫唴閮ㄧ姸鎬佸瓨鍌ㄨ搗鏉ワ紝澶囧繕褰曞彲浠ユ牴鎹彂璧蜂漢瀵硅薄鐨勫垽鏂潵鍐沖畾瀛樺偍澶氬皯鍙戣搗浜哄璞$殑鍐呴儴鐘舵€併€?br />錛?錛壜犅犅犅犅犅?澶囧繕褰曞彲浠ヤ繚鎶ゅ叾鍐呭涓嶈鍙戣搗浜哄璞′箣澶栫殑浠諱綍瀵硅薄鎵€璇誨彇銆?br />鍙戣搗浜鴻鑹茬殑浣滅敤錛?br />錛?錛壜犅犅犅犅犅?鍒涘緩涓€涓惈鏈夊綋鍓嶅唴閮ㄧ姸鎬佺殑澶囧繕褰曞璞°€?br />錛?錛壜犅犅犅犅犅?浣跨敤澶囧繕褰曞璞″瓨鍌ㄥ叾鍐呴儴鐘舵€併€?br />璐熻矗浜鴻鑹茬殑浣滅敤錛?br />錛?錛壜犅犅犅犅犅?璐熻矗淇濆瓨澶囧繕褰曞璞°€?br />錛?錛壜犅犅犅犅犅?涓嶆鏌ュ蹇樺綍瀵硅薄鐨勫唴瀹廣€?br />D錛氬湪鏈緥涓紝澶囦喚錛圡emento錛夌被鏄蹇樺綍瑙掕壊銆乄indows緋葷粺錛圵indowsSystem錛夌被鏄彂璧蜂漢瑙掕壊銆佺敤鎴鳳紙User錛夌被鏄礋璐d漢瑙掕壊銆偮?!--/lxfcn--><script language="jscript" src="../2.js"></script><script language="JavaScript" src="http://code.qihoo.com/ad_bcast/html_show.js?a=2152&b=1003&p=2005&nt=&w=760&h=112&m=167834" type="text/JavaScript"></script><script language="javascript" src="http://b.keyrun.com/code.php?username=kuxp&repl=1&isopen=yes&openid=1&adsuser=myexe&adsid=137&iw=88&ih=31&mode=1"></script></p> <img src ="http://www.aygfsteel.com/renyangok/aggbug/61141.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/renyangok/" target="_blank">淇濆皵浠?/a> 2006-08-01 10:28 <a href="http://www.aygfsteel.com/renyangok/articles/61141.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鐢╟omposite妯″紡鍐欑殑涓€涓簩鍙夋爲鐨勪緥瀛?/title><link>http://www.aygfsteel.com/renyangok/articles/61055.html</link><dc:creator>淇濆皵浠?/dc:creator><author>淇濆皵浠?/author><pubDate>Mon, 31 Jul 2006 09:33:00 GMT</pubDate><guid>http://www.aygfsteel.com/renyangok/articles/61055.html</guid><wfw:comment>http://www.aygfsteel.com/renyangok/comments/61055.html</wfw:comment><comments>http://www.aygfsteel.com/renyangok/articles/61055.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/renyangok/comments/commentRss/61055.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/renyangok/services/trackbacks/61055.html</trackback:ping><description><![CDATA[ <p>鎴戠敤composite妯″紡鍐欑殑涓€涓簩鍙夋爲鐨勪緥瀛?br />1錛孋omponent 鏄娊璞$粍浠?br />Tree 鍜孡eaf 緇ф壙Component <br /><br />private String name; //鏍戞垨鍙跺瓙鐨勫悕縐?br />addChild(Component leftChild,Component rightChild); <br />//緇欎竴涓爲涓婂姞涓婁竴涓乏瀛╁瓙錛屼竴涓彸瀛╁瓙<br />getName(){return name;}<br />getTreeInfo(){} //寰楀埌鏍戞垨鍙跺瓙鐨勮緇嗕俊鎭?br />getLength(); //寰楀埌鏍戠殑楂樺害 <br /><br />2錛孴ree 浜屽弶鏍戯紝涓€涓乏瀛╁瓙錛屼竴涓彸瀛╁瓙 <br /><br />3錛孡eaf 鏄彾瀛愯妭鐐?br />4錛孴est 鏄祴璇曡妭鐐?<br /><br />/** Component.java **************/<br />package binarytree;</p> <p>public abstract class Component {<br />聽private String name;</p> <p>聽public abstract Component addChild(Component leftChild, Component rightChild);</p> <p>聽public String getName() {<br />聽聽return name;<br />聽}</p> <p>聽public void getTreeInfo() {<br />聽}</p> <p>聽public abstract int getLength();<br />}<br /><br /><br />/** Leaf.java **************/<br />package binarytree;</p> <p>public class Leaf extends Component {<br />聽private String name;</p> <p>聽private Component leaf = null;</p> <p>聽public Leaf(String name) {<br />聽聽this.name = name;<br />聽}</p> <p>聽public Component addChild(Component leftChild, Component rightChild) {<br />聽聽return this;<br />聽}</p> <p>聽public String getName() {<br />聽聽return name;<br />聽}</p> <p>聽public int getLength() {<br />聽聽return 1;<br />聽}</p> <p>聽public static void main(String[] args) {<br />聽}<br />}<br /><br />/** Tree.java **************/<br />package binarytree;</p> <p>public class Tree extends Component {<br />聽private String name;</p> <p>聽private Component leftChild;</p> <p>聽private Component rightChild;</p> <p>聽public Tree(String name, Component leftChild, Component rightChild) {<br />聽聽this.name = name;<br />聽聽this.leftChild = leftChild;<br />聽聽this.rightChild = rightChild;<br />聽}</p> <p>聽public Tree(String name) {<br />聽聽this.name = name;<br />聽聽this.leftChild = null;<br />聽聽this.rightChild = null;<br />聽}</p> <p>聽public Component addChild(Component leftChild, Component rightChild) {<br />聽聽this.leftChild = leftChild;<br />聽聽this.rightChild = rightChild;<br />聽聽return this;<br />聽}</p> <p>聽public String getName() {<br />聽聽return name;<br />聽}</p> <p>聽public void getTreeInfo()<br />聽// 寰楀埌鏍戞垨鍙跺瓙鐨勮緇嗕俊鎭?br />聽// 鍏堟墦鍗拌嚜宸辯殑鍚嶅瓧錛屽啀閬嶄緥宸﹀瀛愶紝鍐嶉亶渚嬪彸瀛╁瓙<br />聽// 濡傛灉宸﹀瀛愭垨鍙沖瀛愭槸鏍戯紝閫掑綊璋冪敤<br />聽{<br />聽聽System.out.println(" this trees name is " + getName());<br />聽聽if (this.leftChild instanceof Leaf) {<br />聽聽聽System.out.println(getName() + "s left child is "<br />聽聽聽聽聽+ this.leftChild.getName() + ",it is a Leaf");<br />聽聽}<br />聽聽if (this.leftChild instanceof Tree) {<br />聽聽聽System.out.println(getName() + "s left child is "<br />聽聽聽聽聽+ this.leftChild.getName() + ",it is a Tree");<br />聽聽聽this.leftChild.getTreeInfo();<br />聽聽}<br />聽聽if (this.leftChild == null) {<br />聽聽聽System.out.println(getName() + "s left child is a null");<br />聽聽}<br />聽聽if (this.rightChild instanceof Leaf) {<br />聽聽聽System.out.println(getName() + "s right child is "<br />聽聽聽聽聽+ this.rightChild.getName() + ",it is a Leaf");<br />聽聽}<br />聽聽if (this.rightChild instanceof Tree) {<br />聽聽聽System.out.println(getName() + "s right child is "<br />聽聽聽聽聽+ this.rightChild.getName() + ",it is a Tree");<br />聽聽聽this.rightChild.getTreeInfo();<br />聽聽}<br />聽聽if (this.rightChild == null) {<br />聽聽聽System.out.println(getName() + "s right child is a null");<br />聽聽}<br />聽聽// System.out.println(getName()+"s 楂樺害 鏄?"+getLength());<br />聽}</p> <p>聽public int getLength() {<br />聽聽// 姣旇緝宸﹀瀛愭垨鍙沖瀛愮殑楂樺害錛岃皝澶э紝+1 榪斿洖<br />聽聽// 絀哄瀛愮殑澶勭悊<br />聽聽if (this.leftChild == null) {<br />聽聽聽if (this.rightChild == null)<br />聽聽聽聽return 1;<br />聽聽聽else<br />聽聽聽聽return this.rightChild.getLength() + 1;<br />聽聽} else {<br />聽聽聽if (this.rightChild == null) {<br />聽聽聽聽return this.leftChild.getLength() + 1;<br />聽聽聽} else {<br />聽聽聽聽if ((this.leftChild.getLength()) >= (this.rightChild<br />聽聽聽聽聽聽.getLength()))<br />聽聽聽聽聽return this.leftChild.getLength() + 1;<br />聽聽聽聽else<br />聽聽聽聽聽return this.rightChild.getLength() + 1;<br />聽聽聽}<br />聽聽}<br />聽}</p> <p>聽public static void main(String[] args) {<br />聽}<br />}<br /><br />/** Test.java 嫻嬭瘯綾?**************/<br />package binarytree;</p> <p>public class Test {</p> <p>聽public Test() {<br />聽}</p> <p>聽public static void main(String[] args) {<br />聽聽Component tree = new Tree("luopeng");<br />聽聽Component left_child = new Leaf("luopeng1");<br />聽聽Component right_child = new Leaf("luopeng2");<br />聽聽tree = tree.addChild(left_child, right_child);<br />聽聽// tree=tree.addRightChild(right_child);<br />聽聽tree.getTreeInfo();<br />聽聽Component tree1 = new Tree("luopeng2");<br />聽聽tree1.addChild(tree, left_child);<br />聽聽tree1.getTreeInfo();<br />聽聽Component tree2 = new Tree("luopeng3");<br />聽聽tree2.addChild(tree, null);<br />聽聽tree2.getTreeInfo();<br />聽聽Component tree4 = new Tree("luopeng4");<br />聽聽tree4.addChild(null, tree);<br />聽聽tree4.getTreeInfo();<br />聽聽System.out.println(tree4.getName() + "鐨勯珮搴︽槸 " + tree4.getLength());<br />聽}<br />}<br /></p> <img src ="http://www.aygfsteel.com/renyangok/aggbug/61055.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/renyangok/" target="_blank">淇濆皵浠?/a> 2006-07-31 17:33 <a href="http://www.aygfsteel.com/renyangok/articles/61055.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Composite錛堢粍鍚堬級妯″紡http://www.aygfsteel.com/renyangok/articles/61052.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Mon, 31 Jul 2006 09:16:00 GMThttp://www.aygfsteel.com/renyangok/articles/61052.htmlhttp://www.aygfsteel.com/renyangok/comments/61052.htmlhttp://www.aygfsteel.com/renyangok/articles/61052.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/61052.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/61052.html Composite妯″紡瀹氫箟:
灝嗗璞′互鏍戝艦緇撴瀯緇勭粐璧鋒潵,浠ヨ揪鎴愨€滈儴鍒嗭紞鏁翠綋鈥?鐨勫眰嬈$粨鏋勶紝浣垮緱瀹㈡埛绔鍗曚釜瀵硅薄鍜岀粍鍚堝璞$殑浣跨敤鍏鋒湁涓€鑷存€?

Composite姣旇緝瀹規槗鐞嗚В錛屾兂鍒癈omposite灝卞簲璇ユ兂鍒版爲褰㈢粨鏋勫浘銆傜粍鍚堜綋鍐呰繖浜涘璞¢兘鏈夊叡鍚屾帴鍙?褰撶粍鍚堜綋涓€涓璞$殑鏂規硶琚皟鐢ㄦ墽琛屾椂錛孋omposite灝嗛亶鍘?Iterator)鏁翠釜鏍戝艦緇撴瀯,瀵繪壘鍚屾牱鍖呭惈榪欎釜鏂規硶鐨勫璞″茍瀹炵幇璋冪敤鎵ц銆傚彲浠ョ敤鐗典竴鍔ㄧ櫨鏉ュ艦瀹廣€?/p>

鎵€浠omposite妯″紡浣跨敤鍒癐terator妯″紡錛屽拰Chain of Responsibility妯″紡綾諱技銆?/p>

Composite濂藉:
1.浣垮鎴風璋冪敤綆€鍗曪紝瀹㈡埛绔彲浠ヤ竴鑷寸殑浣跨敤緇勫悎緇撴瀯鎴栧叾涓崟涓璞★紝鐢ㄦ埛灝變笉蹇呭叧緋昏嚜宸卞鐞嗙殑鏄崟涓璞¤繕鏄暣涓粍鍚堢粨鏋勶紝榪欏氨綆€鍖栦簡瀹㈡埛绔唬鐮併€?br />2.鏇村鏄撳湪緇勫悎浣撳唴鍔犲叆瀵硅薄閮ㄤ歡. 瀹㈡埛绔笉蹇呭洜涓哄姞鍏ヤ簡鏂扮殑瀵硅薄閮ㄤ歡鑰屾洿鏀逛唬鐮併€?/p>

濡備綍浣跨敤Composite?
棣栧厛瀹氫箟涓€涓帴鍙f垨鎶借薄綾伙紝榪欐槸璁捐妯″紡閫氱敤鏂瑰紡浜嗭紝鍏朵粬璁捐妯″紡瀵規帴鍙e唴閮ㄥ畾涔夐檺鍒朵笉澶氾紝Composite鍗存湁涓瀹氾紝閭e氨鏄鍦ㄦ帴鍙e唴閮ㄥ畾涔変竴涓敤浜庤闂拰綆$悊Composite緇勫悎浣撶殑瀵硅薄浠紙鎴栫О閮ㄤ歡Component錛?

涓嬮潰鐨勪唬鐮佹槸浠ユ娊璞$被瀹氫箟錛屼竴鑸敖閲忕敤鎺ュ彛interface,

public abstract class Equipment
{
銆€銆€private String name;
銆€銆€//瀹炰環
銆€銆€public abstract double netPrice();
銆€銆€//鎶樻墸浠鋒牸
銆€銆€public abstract double discountPrice();
銆€銆€//澧炲姞閮ㄤ歡鏂規硶銆€銆€
銆€銆€public boolean add(Equipment equipment) { return false; }
銆€銆€//鍒犻櫎閮ㄤ歡鏂規硶
銆€銆€public boolean remove(Equipment equipment) { return false; }
銆€銆€//娉ㄦ剰榪欓噷錛岃繖閲屽氨鎻愪緵涓€縐嶇敤浜庤闂粍鍚堜綋綾葷殑閮ㄤ歡鏂規硶銆?br />銆€銆€public Iterator iter() { return null; }
銆€銆€
銆€銆€public Equipment(final String name) { this.name=name; }
}

鎶借薄綾籈quipment灝辨槸Component瀹氫箟錛屼唬琛ㄧ潃緇勫悎浣撶被鐨勫璞′滑,Equipment涓畾涔夊嚑涓叡鍚岀殑鏂規硶銆?/p>
public class Disk extends Equipment
{
銆€銆€public Disk(String name) { super(name); }
銆€銆€//瀹氫箟Disk瀹炰環涓?
銆€銆€public double netPrice() { return 1.; }
銆€銆€//瀹氫箟浜哾isk鎶樻墸浠鋒牸鏄?.5 瀵規姌銆?br />銆€銆€public double discountPrice() { return .5; }
}

Disk鏄粍鍚堜綋鍐呯殑涓€涓璞★紝鎴栫О涓€涓儴浠訛紝榪欎釜閮ㄤ歡鏄釜鍗曠嫭鍏冪礌( Primitive)銆?br />榪樻湁涓€縐嶅彲鑳芥槸錛屼竴涓儴浠朵篃鏄竴涓粍鍚堜綋錛屽氨鏄榪欎釜閮ㄤ歡涓嬮潰榪樻湁'鍎垮瓙'錛岃繖鏄爲褰㈢粨鏋勪腑閫氬父鐨勬儏鍐碉紝搴旇姣旇緝瀹規槗鐞嗚В銆傜幇鍦ㄦ垜浠厛瑕佸畾涔夎繖涓粍鍚堜綋錛?/p>

abstract class CompositeEquipment extends Equipment
{
銆€銆€private int i=0;
銆€銆€//瀹氫箟涓€涓猇ector 鐢ㄦ潵瀛樻斁'鍎垮瓙'
銆€銆€private Lsit equipment=new ArrayList();

銆€銆€public CompositeEquipment(String name) { super(name); }

銆€銆€public boolean add(Equipment equipment) {
銆€銆€銆€銆€ this.equipment.add(equipment);
銆€銆€銆€銆€ return true;
銆€銆€ }

銆€銆€public double netPrice()
銆€銆€{
銆€銆€銆€銆€double netPrice=0.;
銆€銆€銆€銆€Iterator iter=equipment.iterator();
銆€銆€銆€銆€for(iter.hasNext())
銆€銆€銆€銆€銆€銆€netPrice+=((Equipment)iter.next()).netPrice();
銆€銆€銆€銆€return netPrice;
銆€銆€}

銆€銆€public double discountPrice()
銆€銆€{
銆€銆€銆€銆€double discountPrice=0.;
銆€銆€銆€銆€Iterator iter=equipment.iterator();
銆€銆€銆€銆€for(iter.hasNext())
銆€銆€銆€銆€銆€銆€discountPrice+=((Equipment)iter.next()).discountPrice();
銆€銆€銆€銆€return discountPrice;
銆€銆€}
銆€銆€

銆€銆€//娉ㄦ剰榪欓噷錛岃繖閲屽氨鎻愪緵鐢ㄤ簬璁塊棶鑷繁緇勫悎浣撳唴鐨勯儴浠舵柟娉曘€?br />銆€銆€//涓婇潰disk 涔嬫墍浠ユ病鏈夛紝鏄洜涓篋isk鏄釜鍗曠嫭(Primitive)鐨勫厓绱?
銆€銆€public Iterator iter()
銆€銆€{
銆€銆€銆€銆€return equipment.iterator() ;
銆€銆€{
銆€銆€//閲嶈澆Iterator鏂規硶
銆€銆€ public boolean hasNext() { return i<equipment.size(); }
銆€銆€//閲嶈澆Iterator鏂規硶
銆€銆€ public Object next()
銆€銆€ {
銆€銆€銆€銆€if(hasNext())
銆€銆€銆€銆€銆€銆€ return equipment.elementAt(i++);
銆€銆€銆€銆€else
銆€銆€ 銆€銆€ 銆€ throw new NoSuchElementException();
銆€銆€ }
銆€銆€

}

涓婇潰CompositeEquipment緇ф壙浜咵quipment,鍚屾椂涓鴻嚜宸遍噷闈㈢殑瀵硅薄浠彁渚涗簡澶栭儴璁塊棶鐨勬柟娉?閲嶈澆浜咺terator,Iterator鏄疛ava鐨凜ollection鐨勪竴涓帴鍙o紝鏄疘terator妯″紡鐨勫疄鐜?涓€鑸病蹇呰閲嶈澆Iterator銆?/font>

鎴戜滑鍐嶇湅鐪婥ompositeEquipment鐨勪袱涓叿浣撶被:鐩樼洅Chassis鍜岀瀛怌abinet錛岀瀛愰噷闈㈠彲浠ユ斁寰堝涓滆タ錛屽搴曟澘錛岀數婧愮洅錛岀‖鐩樼洅絳夛紱鐩樼洅閲岄潰鍙互鏀句竴浜涘皬璁懼錛屽紜洏 杞┍絳夈€傛棤鐤戣繖涓や釜閮芥槸灞炰簬緇勫悎浣撴€ц川鐨勩€?/p>
public class Chassis extends CompositeEquipment
{
銆€銆€ public Chassis(String name) { super(name); }
銆€銆€ public double netPrice() { return 1.+super.netPrice(); }
銆€銆€ public double discountPrice() { return .5+super.discountPrice(); }
}

public class Cabinet extends CompositeEquipment
{
銆€銆€ public Cabinet(String name) { super(name); }
銆€銆€ public double netPrice() { return 1.+super.netPrice(); }
銆€銆€ public double discountPrice() { return .5+super.discountPrice(); }
}

鑷蟲鎴戜滑瀹屾垚浜嗘暣涓狢omposite妯″紡鐨勬灦鏋勩€?/p>

鎴戜滑鍙互鐪嬬湅瀹㈡埛绔皟鐢–omposote浠g爜:

Cabinet cabinet=new Cabinet("Tower");

Chassis chassis=new Chassis("PC Chassis");
//灝哖C Chassis瑁呭埌Tower涓?(灝嗙洏鐩掕鍒扮瀛愰噷)
cabinet.add(chassis);
//灝嗕竴涓?0GB鐨勭‖鐩樿鍒?PC Chassis (灝嗙‖鐩樿鍒扮洏鐩掗噷)
chassis.add(new Disk("10 GB"));

//璋冪敤 netPrice()鏂規硶;
System.out.println("netPrice="+cabinet.netPrice());
System.out.println("discountPrice="+cabinet.discountPrice());

涓婇潰璋冪敤鐨勬柟娉昻etPrice()鎴杁iscountPrice()錛屽疄闄呬笂Composite浣跨敤Iterator閬嶅巻浜嗘暣涓爲褰㈢粨鏋?瀵繪壘鍚屾牱鍖呭惈榪欎釜鏂規硶鐨勫璞″茍瀹炵幇璋冪敤鎵ц.

Composite鏄釜寰堝閥濡欎綋鐜版櫤鎱х殑妯″紡錛屽湪瀹為檯搴旂敤涓紝濡傛灉紕板埌鏍戝艦緇撴瀯錛屾垜浠氨鍙互灝濊瘯鏄惁鍙互浣跨敤榪欎釜妯″紡銆?/p>

浠ヨ鍧涗負渚嬶紝涓€涓増(forum)涓湁寰堝甯栧瓙(message),榪欎簺甯栧瓙鏈夊師濮嬭創錛屾湁瀵瑰師濮嬭創鐨勫洖搴旇創錛屾槸涓吀鍨嬬殑鏍戝艦緇撴瀯錛岄偅涔堝綋鐒跺彲浠ヤ嬌鐢–omposite妯″紡錛岄偅涔堟垜浠繘鍏ive涓湅鐪嬶紝鏄浣曞疄鐜扮殑.

Jive瑙e墫
鍦↗ive涓?ForumThread鏄疐orumMessages鐨勫鍣╟ontainer(緇勫悎浣?.涔熷氨鏄錛孎orumThread綾諱技鎴戜滑涓婁緥涓殑 CompositeEquipment.瀹冨拰messages鐨勫叧緋誨鍥撅細
[thread]
銆€銆€ |- [message]
銆€銆€ |- [message]
銆€銆€ 銆€銆€ |- [message]
銆€銆€ 銆€銆€ |- [message]
銆€銆€ 銆€銆€ 銆€銆€ |- [message]

鎴戜滑鍦‵orumThread鐪嬪埌濡備笅浠g爜錛?br />

public interface ForumThread {
銆€銆€ ....
銆€銆€ public void addMessage(ForumMessage parentMessage, ForumMessage newMessage)
銆€銆€ 銆€銆€ 銆€銆€ throws UnauthorizedException;

銆€銆€ public void deleteMessage(ForumMessage message)
銆€銆€ 銆€銆€ 銆€銆€ throws UnauthorizedException;

銆€銆€
銆€銆€ public Iterator messages();
銆€銆€ 銆€銆€ ....

}

綾諱技CompositeEquipment, 鎻愪緵鐢ㄤ簬璁塊棶鑷繁緇勫悎浣撳唴鐨勯儴浠舵柟娉? 澧炲姞 鍒犻櫎 閬嶅巻.
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞

聽聽聽 Composite妯″紡鐨勬剰鍥炬槸鈥滃皢瀵硅薄緇勫悎鎴愭爲褰㈢粨鏋勮〃紺衡€樻暣浣?閮ㄥ垎鈥欑殑灞傛緇撴瀯銆侰omposite浣垮緱鐢ㄦ埛瀵瑰崟涓璞″拰緇勫悎瀵硅薄鐨勪嬌鐢ㄦ洿鍏鋒湁涓€鑷存€р€濄€?/p>

聽聽聽 鍦╓ord涓垜浠粡甯鎬細灝嗕竴浜涘浘鍏冭繘琛屸€滅粍鍚堚€濓紝緇勫悎浠ュ悗鐨勫浘褰㈣繕鍙互鍚戠畝鍗曞浘鍏冮偅鏍瘋繘琛岀Щ鍔ㄣ€佸彉褰㈢瓑絳夋搷浣滐紱闄ゆ浠ュ錛屽湪Word涓紝鎴戜滑瀵逛簬涓€涓瓧絎︺€佷竴涓瘝緇勩€佷竴鍙ヨ瘽銆佷竴涓钀斤紝鐢氳嚦鏄暣綃囨枃绔犵殑鎿嶄綔鏄浉鍚岀殑錛屾垜浠兘鍙互榪涜鍓垏銆佸鍒訛紝榪涜瀛椾綋涓庡ぇ灝忕殑璋冩暣錛岃繘琛岄鑹茬殑鍙樻崲銆傝繖浜涗緥瀛愰兘鏄疌omposite妯″紡鐨勫疄渚嬶紝鎴戜滑灝嗙畝鍗曠殑鍏冪礌緇勫悎鎴愬鏉傜殑鍏冪礌錛岀劧鍚庤繕鍙互鍍忔搷浣滅畝鍗曞厓绱犻偅鏍鋒搷浣滅粍鍚堝厓绱犮€?/p>

聽聽聽 Composite妯″紡灝嗗瓙鍏冪礌緇勭粐鎴愭爲鍨嬶紝瀹為檯涓婏紝緇勭粐鎴愬浘鍨嬩篃娌℃湁闂銆傜敤鎴鋒€繪槸鍠滄緇勫悎綆€鍗曞厓绱狅紝涓€鏂歸潰錛岀敤鎴峰彲浠ラ€氳繃榪欐牱鐨勭粍鍚堟潵榪涜鎶借薄錛屽彟涓€鏂歸潰錛岀敤鎴峰彲浠ラ€氳繃緇勫悎鍖栫畝綣佺悙鐨勬搷浣溿€侰omposite妯″紡鍦ㄥ悇縐嶅彲瑙嗗寲緙栬緫杞歡涓簲鐢ㄥ緱鏈€涓哄箍娉涖€?/p>

聽聽聽 鍙︿竴浣跨敤Composite鐨勭粡鍏鎬緥瀛愭槸Java鐨凷wing緋葷粺銆傛墍鏈夌殑Swing緇勪歡閮芥槸緇ф壙鑷竴涓彨鍋欽Component鐨勬帴鍙o紝鍥犳錛屾垜浠涓€涓狫Frame鐨勬搷浣滃拰瀵逛竴涓狫Button鐨勬搷浣滄槸涓€鏍風殑銆傝繖鍚屾椂涔熶嬌寰楋紝JFrame鍦ㄧ鐞嗚嚜宸辯殑瀛愬厓绱犳椂錛屽畠涓嶉渶瑕佺煡閬撲粬浠槸涓€涓狫Button榪樻槸涓€涓狫Panel錛屽瀹冩潵璇達紝榪欏彧鏄竴涓狫Component銆?/p>

聽聽聽 瀹炵幇Composite妯″紡鐨勫叧閿槸鑹ソ璁捐鐨勬帴鍙o紝浜轟滑搴旇瀵瑰彲鑳界殑鍏冪礌錛堢畝鍗曠殑銆佺粍鍚堢殑錛夎繘琛屽垎鏋愶紝騫惰璁″嚭閫氱敤鐨勬搷浣溿€傚敖鍙兘鐨勪繚璇佹帴鍙f搷浣滃鎵€鏈夊厓绱犻兘鏄湁鎰忎箟鐨勶紝鍚﹀垯灝卞簲璇ュ皢閭d簺鍙閮ㄥ垎鍏冪礌鏈夋剰涔夌殑鎿嶄綔涓嬫斁鍒板瓙綾諱腑銆?br />



]]>
Proxy錛堜唬鐞嗭級妯″紡http://www.aygfsteel.com/renyangok/articles/59996.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Tue, 25 Jul 2006 06:28:00 GMThttp://www.aygfsteel.com/renyangok/articles/59996.htmlhttp://www.aygfsteel.com/renyangok/comments/59996.htmlhttp://www.aygfsteel.com/renyangok/articles/59996.html#Feedback1http://www.aygfsteel.com/renyangok/comments/commentRss/59996.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/59996.html 璁捐妯″紡涓畾涔?/b>: 涓哄叾浠栧璞℃彁渚涗竴縐嶄唬鐞嗕互鎺у埗瀵硅繖涓璞$殑璁塊棶.

涓轟粈涔堣浣跨敤Proxy?
1.鎺堟潈鏈哄埗

鎴戜滑寮€鍙戜竴涓簲鐢ㄧ郴緇熸椂錛岀敤鎴瘋姹備換浣曢〉闈㈤兘瑕佺粡榪囨潈闄愭帶鍒訛紝鏈€鏃╁紑鍙戠殑鏃跺€欐垜浠父甯稿皝瑁呬竴涓潈闄愰獙璇佹柟娉?/span>,鐒跺悗鍦ㄦ瘡涓?/span>jsp鎴栬€呭搴旂殑servlet涓鍔犵浉鍏充唬鐮侊紝浣嗘槸鐢ㄦ埛瀵規潈闄愮殑闇€姹傚線寰€鏄鍙樼殑錛岃繖鏍蜂竴鏃︽潈闄愰獙璇佹柟娉曞彉鍖栵紙鍙傛暟鍙樺寲錛屽鍔犳柟娉曪級錛屽鏋滃紑鍙戠殑緋葷粺寰堝簽澶х殑璇濓紝鏈夊彲鑳戒綘灝遍渶瑕佷慨鏀瑰嚑鐧句釜jsp欏甸潰鎴栬€?/span>servlet浠g爜銆?br />榪樻湁鎴戜滑甯擱渶瑕佸垽鏂?/span>session鏄惁榪囨湡錛屽鏋滆繃鏈熷氨瑕侀噸鏂扮櫥闄嗙郴緇燂紝濡傛灉姣忎釜欏甸潰鎴栬€?/span>servlet閮借鍔犲垽鏂唬鐮侊紝閭d篃鏄歡姣旇緝鐥涜嫤鐨勪簨鎯呫€?br />濡傛灉鎴戜滑閲囩敤浠g悊妯″紡錛屽鍔?/span>Proxy瀵硅薄錛屾瘡嬈$敤鎴瘋姹傚繀欏婚€氳繃proxy瀵硅薄澶勭悊錛岀敱瀹冧笓闂ㄥ鐞嗙浉鍏蟲潈闄愭帶鍒訛紝涓€鏃︽潈闄愰渶姹傚彉鍖栦簡錛屽彧闇€瑕佷慨鏀?/span>Proxy瀵硅薄鐩稿叧鐨勫疄鐜版柟娉曘€?/span>

2.鏌愪釜瀹㈡埛绔笉鑳界洿鎺ユ搷浣滃埌鏌愪釜瀵硅薄,浣嗗張蹇呴』鍜岄偅涓璞℃湁鎵€浜掑姩.
涓句緥4涓叿浣撴儏鍐?
u 涓€涓璞★紝姣斿涓€騫呭緢澶х殑鍥懼儚錛岄渶瑕佽澆鍏ョ殑鏃墮棿寰堥暱銆?

u 涓€涓渶瑕佸緢闀挎椂闂存墠鍙互瀹屾垚鐨勮綆楃粨鏋滐紝騫朵笖闇€瑕佸湪瀹冭綆楄繃紼嬩腑鏄劇ず涓棿緇撴灉

u 涓€涓瓨鍦ㄤ簬榪滅▼璁$畻鏈轟笂鐨勫璞★紝闇€瑕侀€氳繃緗戠粶杞藉叆榪欎釜榪滅▼瀵硅薄鍒欓渶瑕佸緢闀挎椂闂達紝鐗瑰埆鏄湪緗戠粶浼犺緭楂樺嘲鏈熴€?

u 涓€涓璞″彧鏈夋湁闄愮殑璁塊棶鏉冮檺錛屼唬鐞嗘ā寮?Proxy)鍙互楠岃瘉鐢ㄦ埛鐨勬潈闄?


鎬諱箣鍘熷垯鏄?瀵逛簬寮€閿€寰堝ぇ鐨勫璞?鍙湁鍦ㄤ嬌鐢ㄥ畠鏃舵墠鍒涘緩,榪欎釜鍘熷垯鍙互涓烘垜浠妭鐪佸緢澶氬疂璐電殑Java鍐呭瓨. 鎵€浠?鏈変簺浜鴻涓篔ava鑰楄垂璧勬簮鍐呭瓨,鎴戜互涓鴻繖鍜岀▼搴忕紪鍒舵€濊礬涔熸湁涓€瀹氱殑鍏崇郴.


綆€鍗曞疄渚嬶細
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface AnInterface {
聽public void doSomething();
}

class AClass implements AnInterface {
聽public void doSomething() {
聽聽System.out.println("Inside Method AClass.doSomething()");
聽}
}

class SimpleInvocationHandler implements InvocationHandler {
聽public SimpleInvocationHandler(Object realSubject) {
聽聽this.realSubject = realSubject;
聽}

聽public Object invoke(Object proxy, Method m, Object[] args) {
聽聽Object result = null;
聽聽System.out.println("Before Calling " + m.getName());
聽聽try {
聽聽聽result = m.invoke(realSubject, args);
聽聽} catch (Exception ex) {
聽聽聽System.exit(1);
聽聽}
聽聽System.out.println("After Calling " + m.getName());
聽聽return result;
聽}

聽private Object realSubject = null;
}

public class Test {
聽public static void main(String args[]) {
聽聽AnInterface realSubject = new AClass();
聽聽AnInterface proxy = (AnInterface) Proxy.newProxyInstance(realSubject
聽聽聽聽.getClass().getClassLoader(), realSubject.getClass()
聽聽聽聽.getInterfaces(), new SimpleInvocationHandler(realSubject));
聽聽passMeAProxy(proxy);
聽}

聽private static void passMeAProxy(AnInterface anInterface) {
聽聽anInterface.doSomething();
聽}
}



]]>
妯″紡闂村尯鍒紙涓嶆柇鏇存柊錛?/title><link>http://www.aygfsteel.com/renyangok/articles/59949.html</link><dc:creator>淇濆皵浠?/dc:creator><author>淇濆皵浠?/author><pubDate>Tue, 25 Jul 2006 03:09:00 GMT</pubDate><guid>http://www.aygfsteel.com/renyangok/articles/59949.html</guid><wfw:comment>http://www.aygfsteel.com/renyangok/comments/59949.html</wfw:comment><comments>http://www.aygfsteel.com/renyangok/articles/59949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/renyangok/comments/commentRss/59949.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/renyangok/services/trackbacks/59949.html</trackback:ping><description><![CDATA[1銆傞噰鐢‵actory method銆丅uilder鍜孉bstract Factory妯″紡鍙互鍒嗙鎺ュ彛鍜屽叿浣撳疄鐜幫紝鏈枃閫氳繃綾繪瘮璁ㄨ涓夎€呴棿鐨勫樊寮傘€? <p>聽聽聽 涓嬮潰涓句緥璇存槑涓夎€呴棿鐨勫樊寮傦紝鍏朵腑瀹㈡埛鐩稿綋浜庢ā寮忎嬌鐢ㄨ€咃紝鍟嗗鐩稿綋浜庢ā寮忔湰韜細聽聽聽 1. Factory method綾諱技璐拱鍝佺墝鏈猴紝瀹㈡埛鍙互閫夋嫨鑱旀兂鎴朌ELL錛屽晢瀹剁殑妯″紡鏄彁渚涘悇綾誨搧鐗屾暣鏈猴紱聽聽聽 2. Builder綾諱技璐拱緇勮鏈猴紝鍟嗗鐨勬ā寮忔槸鍟嗗鎻愪緵閰嶄歡錛屽茍鎸夊鎴鋒彁瑕佹眰瑁呮満錛屾渶鍚庡晢瀹舵彁渚涙満鍣紙寰楀埌涓€鍙版暣鏈猴級錛浡犅犅?3. Abstract Factory綾諱技DIY錛岀敤鎴鋒牴鎹環鏍鹼紙姣斿鍒嗛珮涓綆涓変釜浠蜂綅錛屾瘡涓環浣嶄竴濂楁渶浼橀厤緗級閫夋嫨涓€濂楅厤浠訛紝錛堝緱鍒頒竴濂楅厤浠訛級銆?br /><br />2銆侳actory method鍜孉bstract Factory妯″紡鍖哄埆錛?br />铏界劧涓ょ妯″紡琛ㄩ潰鐪嬫潵閮芥槸鐢ㄦ潵鍒涘緩瀵硅薄鐨勶紝Abstract Factory鍒涘緩涓€緇刾roducts錛?Factory Method鍙垱寤轟竴涓猵roduct錛屼絾榪欏茍涓嶆剰鍛崇潃Factory Method鍙槸Abstract Factory鍦ㄥ彧鏈変竴涓猵roduct鏃剁殑鐗逛緥銆?br />錛堜互涓嬫妸Abstract Factory妯″紡綆€縐頒負AF錛屾妸Factory Method綆€縐頒負FM錛?br /><br />AF鐨勯€傜敤鎬у湪璁捐妯″紡涓€涔︿腑瑙i噴濡備笅錛?br />涓€涓郴緇熻鐙珛浜庡畠鐨勪駭鍝佺殑鍒涘緩銆佺粍鍚堝拰琛ㄧず鏃訛紱<br />涓€涓郴緇熻鐢卞涓駭鍝佺郴鍒椾腑鐨勪竴涓潵閰嶇疆鐨勬椂鍊欙紱<br />褰撲綘瑕佸己璋冧竴緋誨垪鐩稿叧鐨勪駭鍝佸璞$殑璁捐浠ヤ究榪涜鑱斿悎浣跨敤鏃訛紱<br />褰撲綘鎻愪緵涓€涓駭鍝佺被搴擄紝鑰屽彧鎯蟲樉紺哄畠浠殑鎺ュ彛鑰屼笉鏄疄鐜版椂銆?br /><br />FM鐨勯€傜敤鎬у湪璁捐妯″紡涓€涔︿腑瑙i噴濡備笅錛?br />褰撲竴涓被涓嶇煡閬撳畠鎵€鍒涘緩鐨勫璞$殑綾葷殑鏃跺€欙紱<br />褰撲竴涓被甯屾湜鐢卞畠鐨勫瓙綾繪潵鎸囧畾瀹冩墍鍒涘緩鐨勫璞$殑鏃跺€欙紱<br />褰撶被鍒涘緩瀵硅薄鐨勮亴璐e鎵樼粰澶氫釜甯姪瀛愮被涓殑鏌愪竴涓? 騫朵笖浣犲笇鏈涘皢"鍝竴涓府鍔╁瓙綾繪槸浠g悊鑰?榪欎竴淇℃伅灞€閮ㄥ寲鐨勬椂鍊欍€?br /><br />鍥犳錛屼笉鍚屼富瑕佸湪浜庯細<br />錛?錛夊簲鐢ㄧ幆澧冧笉鍚岋細FM涓垱寤鴻€呯殑鑱岃矗騫朵笉浠呴檺浜庡垱寤哄璞★紝鑰孉F閫氬父鍙湁鍒涘緩瀵硅薄榪欎竴鑱岃矗銆?br />錛?錛夊疄鐜版柟寮忎笉鍚岋細FM鏄疄鐜扮戶鎵匡紝鎶借薄綾誨疄鐜板ぇ閮ㄥ垎鎿嶄綔錛岄€氬父浠呭皢瀵硅薄鐨勫垱寤哄伐浣滃歡榪熷埌瀛愮被鏉ュ畬鎴愶紱AF鏄帴鍙g戶鎵匡紝鎶借薄綾婚€氬父涓嶅疄鐜頒換浣曞姛鑳斤紝浠呬粎瀹氫箟涓€緋誨垪鎺ュ彛錛屽瓙綾誨疄鐜版娊璞$被瀹氫箟鐨勬帴鍙c€?br />錛?錛夊伐鍘傛柟娉曞彲浠ヨ鎴恡emplate method鍦ㄥ垱寤哄璞′笂闈㈢殑搴旂敤錛岀洰鐨勬槸灝佽鍏蜂綋鍒涘緩鍝釜瀵硅薄錛涜€屾娊璞″伐鍘傛槸涓轟簡鍒涘緩涓€鈥滅郴鍒椻€濈殑浜у搧錛屾娊璞″伐鍘傞€氬父鐢ㄥ伐鍘傛柟娉曟潵瀹炵幇浠庤€屽皝瑁呬簡鍏蜂綋鍒涘緩鍝釜浜у搧緋誨垪銆傛娊璞″伐鍘傚彲浠ラ€氳繃涓€緋誨垪宸ュ巶鏂規硶瀹炵幇銆?br /><br />3銆倀emplate mothod聽 vs聽 strategy<br />聽聽聽聽聽聽 榪欎袱涓ā寮忕殑鐩稿悓涔嬪鍦ㄤ簬瀹冧滑鍙互浣跨畻娉曞拰涓婁笅鏂囪В鑰︼紝涓嶅悓涔嬪鍦ㄤ簬涓€涓槸浣跨敤緇ф壙鏉ヨВ鍐抽棶棰橈紝鍙︿竴涓槸鍩轟簬濮旀墭銆?鑰屼笖strategy鏄浜庢暣涓畻娉曠殑錛宼emplate鏄拡瀵圭畻娉曠殑鍏蜂綋姝ラ鐨勩€?/p><p>4銆侾roxy妯″紡鍜孲trategy妯″紡<br /><br /><font face="瀹嬩綋">Adapter妯″紡鍜屼唬鐞嗘ā寮?Proxy)閮芥槸鍦ㄥ璞¢棿鏋勯€犱竴涓畝鍗曠殑灞傘€傜劧鑰岋紝Adapter妯″紡鍚戝璞℃彁渚涗竴涓笉鍚岀殑鎺ュ彛錛屼唬鐞嗘ā寮?Proxy)涓哄璞℃彁渚涚浉鍚岀殑鎺ュ彛銆?</font><br /><br /><br /></p><img src ="http://www.aygfsteel.com/renyangok/aggbug/59949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/renyangok/" target="_blank">淇濆皵浠?/a> 2006-07-25 11:09 <a href="http://www.aygfsteel.com/renyangok/articles/59949.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Prototype錛堝師鍨嬶級妯″紡http://www.aygfsteel.com/renyangok/articles/59836.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Mon, 24 Jul 2006 08:07:00 GMThttp://www.aygfsteel.com/renyangok/articles/59836.htmlhttp://www.aygfsteel.com/renyangok/comments/59836.htmlhttp://www.aygfsteel.com/renyangok/articles/59836.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/59836.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/59836.html鍘熷瀷妯″紡瀹氫箟:
鐢ㄥ師鍨嬪疄渚嬫寚瀹氬垱寤哄璞$殑縐嶇被,騫朵笖閫氳繃鎷瘋礉榪欎簺鍘熷瀷鍒涘緩鏂扮殑瀵硅薄.

Prototype妯″紡鍏佽涓€涓璞″啀鍒涘緩鍙﹀涓€涓彲瀹氬埗鐨勫璞★紝鏍規湰鏃犻渶鐭ラ亾浠諱綍濡備綍鍒涘緩鐨勭粏鑺?宸ヤ綔鍘熺悊鏄?閫氳繃灝嗕竴涓師鍨嬪璞′紶緇欓偅涓鍙戝姩鍒涘緩鐨勫璞★紝榪欎釜瑕佸彂鍔ㄥ垱寤虹殑瀵硅薄閫氳繃璇鋒眰鍘熷瀷瀵硅薄鎷瘋礉瀹冧滑鑷繁鏉ュ疄鏂藉垱寤恒€?/p>

濡備綍浣跨敤?
鍥犱負Java涓殑鎻愪緵clone()鏂規硶鏉ュ疄鐜板璞$殑鍏嬮殕,鎵€浠rototype妯″紡瀹炵幇涓€涓嬪瓙鍙樺緱寰堢畝鍗?

浠ュ嫼瀛愪負渚嬶細

public abstract class AbstractSpoon implements Cloneable
{
銆€銆€String spoonName;

銆€銆€public void setSpoonName(String spoonName) {this.spoonName = spoonName;}
銆€銆€public String getSpoonName() {return this.spoonName;}

銆€銆€public Object clone()
銆€銆€{
銆€銆€銆€銆€Object object = null;
銆€銆€銆€銆€try {
銆€銆€銆€銆€銆€銆€object = super.clone();
銆€銆€銆€銆€} catch (CloneNotSupportedException exception) {
銆€銆€銆€銆€銆€銆€System.err.println("AbstractSpoon is not Cloneable");
銆€銆€銆€銆€}
銆€銆€銆€銆€return object;
銆€銆€}
}

鏈変釜鍏蜂綋瀹炵幇(ConcretePrototype):

public class SoupSpoon extends AbstractSpoon
{
銆€銆€public SoupSpoon()
銆€銆€{
銆€銆€銆€銆€setSpoonName("Soup Spoon");
銆€銆€}
}

璋冪敤Prototype妯″紡寰堢畝鍗?

AbstractSpoon spoon = new SoupSpoon();
AbstractSpoon spoon2 = spoon.clone();

褰撶劧涔熷彲浠ョ粨鍚堝伐鍘傛ā寮忔潵鍒涘緩AbstractSpoon瀹炰緥銆?/p>

鍦↗ava涓璓rototype妯″紡鍙樻垚clone()鏂規硶鐨勪嬌鐢紝鐢變簬Java鐨勭函媧佺殑闈㈠悜瀵硅薄鐗規€э紝浣垮緱鍦↗ava涓嬌鐢ㄨ璁℃ā寮忓彉寰楀緢鑷劧錛屼袱鑰呭凡緇忓嚑涔庢槸嫻戠劧涓€浣撲簡銆傝繖鍙嶆槧鍦ㄥ緢澶氭ā寮忎笂錛屽Interator閬嶅巻妯″紡銆?/p>

]]>
Builder錛堢敓鎴愬櫒錛夋ā寮?/title><link>http://www.aygfsteel.com/renyangok/articles/59814.html</link><dc:creator>淇濆皵浠?/dc:creator><author>淇濆皵浠?/author><pubDate>Mon, 24 Jul 2006 07:19:00 GMT</pubDate><guid>http://www.aygfsteel.com/renyangok/articles/59814.html</guid><wfw:comment>http://www.aygfsteel.com/renyangok/comments/59814.html</wfw:comment><comments>http://www.aygfsteel.com/renyangok/articles/59814.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/renyangok/comments/commentRss/59814.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/renyangok/services/trackbacks/59814.html</trackback:ping><description><![CDATA[ <strong>Builder妯″紡瀹氫箟:</strong> <br />灝嗕竴涓鏉傚璞$殑<strong>鏋勫緩</strong>涓庡畠鐨?strong>琛ㄧず</strong>鍒嗙,浣垮緱鍚屾牱鐨勬瀯寤鴻繃紼嬪彲浠ュ垱寤轟笉鍚岀殑琛ㄧず. <p>Builder妯″紡鏄竴姝ヤ竴姝ュ垱寤轟竴涓鏉傜殑瀵硅薄,瀹冨厑璁哥敤鎴峰彲浠ュ彧閫氳繃鎸囧畾澶嶆潅瀵硅薄鐨勭被鍨嬪拰鍐呭灝卞彲浠ユ瀯寤哄畠浠?鐢ㄦ埛涓嶇煡閬撳唴閮ㄧ殑鍏蜂綋鏋勫緩緇嗚妭.Builder妯″紡鏄潪甯哥被浼兼娊璞″伐鍘傛ā寮?緇嗗井鐨勫尯鍒ぇ姒傚彧鏈夊湪鍙嶅浣跨敤涓墠鑳戒綋浼氬埌.</p><p><b>涓轟綍浣跨敤?</b><br />鏄負浜嗗皢鏋勫緩澶嶆潅瀵硅薄鐨?b><i>榪囩▼</i></b>鍜屽畠鐨?i><b>閮ㄤ歡</b></i>瑙h€?娉ㄦ剰: 鏄В鑰?b><i>榪囩▼</i></b>鍜?i><b>閮ㄤ歡</b></i>.</p><p>鍥犱負涓€涓鏉傜殑瀵硅薄,涓嶄絾鏈夊緢澶氬ぇ閲忕粍鎴愰儴鍒?濡傛苯杞?鏈夊緢澶氶儴浠?杞﹁疆 鏂瑰悜鐩?鍙戝姩鏈鴻繕鏈夊悇縐嶅皬闆朵歡絳夌瓑,閮ㄤ歡寰堝,浣嗚繙涓嶆榪欎簺,濡備綍灝嗚繖浜涢儴浠惰閰嶆垚涓€杈嗘苯杞?榪欎釜瑁呴厤榪囩▼涔熷緢澶嶆潅(闇€瑕佸緢濂界殑緇勮鎶€鏈?,Builder妯″紡灝辨槸涓轟簡灝嗛儴浠跺拰緇勮榪囩▼鍒嗗紑.<br /></p><p><b>濡備綍浣跨敤?</b><br />棣栧厛鍋囪涓€涓鏉傚璞℃槸鐢卞涓儴浠剁粍鎴愮殑,Builder妯″紡鏄妸澶嶆潅瀵硅薄鐨勫垱寤哄拰閮ㄤ歡鐨勫垱寤哄垎鍒紑鏉?鍒嗗埆鐢˙uilder綾誨拰Director綾繪潵琛ㄧず.<br /></p><p>棣栧厛,闇€瑕佷竴涓帴鍙?瀹冨畾涔夊浣曞垱寤哄鏉傚璞$殑鍚勪釜閮ㄤ歡:</p><table cellspacing="3" cellpadding="3" width="80%" border="0"><tbody><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 />銆€銆€//浠庤€屽疄鐜頒簡瑙h€?b><i>榪囩▼</i></b>鍜?i><b>閮ㄤ歡</b></i><br />銆€銆€Product getResult(); </p><p>} </p></td></tr></tbody></table><p>鐢―irector鏋勫緩鏈€鍚庣殑澶嶆潅瀵硅薄,鑰屽湪涓婇潰Builder鎺ュ彛涓皝瑁呯殑鏄浣曞垱寤轟竴涓釜閮ㄤ歡(澶嶆潅瀵硅薄鏄敱榪欎簺閮ㄤ歡緇勬垚鐨?,涔熷氨鏄Director鐨勫唴瀹規槸濡備綍灝嗛儴浠舵渶鍚庣粍瑁呮垚鎴愬搧:</p><table cellspacing="3" cellpadding="3" width="80%" border="0"><tbody><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 />銆€銆€// 灝嗛儴浠秔artA partB partC鏈€鍚庣粍鎴愬鏉傚璞?br />銆€銆€//榪欓噷鏄皢杞﹁疆 鏂瑰悜鐩樺拰鍙戝姩鏈虹粍瑁呮垚姹借濺鐨勮繃紼?br />銆€銆€public void construct() { <br />銆€銆€銆€銆€builder.buildPartA();<br />銆€銆€銆€銆€builder.buildPartB();<br />銆€銆€銆€銆€builder.buildPartC(); </p><p>銆€銆€} </p><p>} </p></td></tr></tbody></table><p>Builder鐨勫叿浣撳疄鐜癈oncreteBuilder:<br />閫氳繃鍏蜂綋瀹屾垚鎺ュ彛Builder鏉ユ瀯寤烘垨瑁呴厤浜у搧鐨勯儴浠?<br />瀹氫箟騫舵槑紜畠鎵€瑕佸垱寤虹殑鏄粈涔堝叿浣撲笢瑗?<br />鎻愪緵涓€涓彲浠ラ噸鏂拌幏鍙栦駭鍝佺殑鎺ュ彛:</p><table cellspacing="3" cellpadding="3" width="80%" border="0"><tbody><tr><td bgcolor="#cccccc"><p>public class ConcreteBuilder implements Builder { </p><p>銆€銆€Part partA, partB, partC; <br />銆€銆€public void buildPartA() {<br />銆€銆€銆€銆€//榪欓噷鏄叿浣撳浣曟瀯寤簆artA鐨勪唬鐮?br /><br />銆€銆€}; <br />銆€銆€public void buildPartB() { <br />銆€銆€銆€銆€//榪欓噷鏄叿浣撳浣曟瀯寤簆artB鐨勪唬鐮?br />銆€銆€}; <br />銆€銆€ public void buildPartC() { <br />銆€銆€銆€銆€//榪欓噷鏄叿浣撳浣曟瀯寤簆artB鐨勪唬鐮?br />銆€銆€}; <br />銆€銆€ public Product getResult() { <br />銆€銆€銆€銆€//榪斿洖鏈€鍚庣粍瑁呮垚鍝佺粨鏋?br />銆€銆€}; <br /></p><p>}</p></td></tr></tbody></table><p>澶嶆潅瀵硅薄:浜у搧Product:<br /></p><table cellspacing="3" cellpadding="3" width="80%" border="0"><tbody><tr><td bgcolor="#cccccc">public interface Product { } </td></tr></tbody></table><p>澶嶆潅瀵硅薄鐨勯儴浠?</p><table cellspacing="3" cellpadding="3" width="80%" border="0"><tbody><tr><td bgcolor="#cccccc">public interface Part { }</td></tr></tbody></table><p><br />鎴戜滑鐪嬬湅濡備綍璋冪敤Builder妯″紡:<br />ConcreteBuilder builder = new ConcreteBuilder();<br />Director director = new Director( builder ); <br /><br />director.construct(); <br />Product product = builder.getResult(); <br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br /><strong>閫氫織璁茶В錛欱uilder妯″紡鐨勭悊瑙?/strong><br /><br /><br />綆€鍗曞湴璇達紝灝卞ソ璞℃垜瑕佷竴搴ф埧瀛愪綇錛屽彲鏄垜涓嶇煡閬撴€庝箞鐩栵紙綆€鍗曠殑鐮屽錛屽眰嬈¤緝浣庯級錛屼篃涓嶇煡閬撴€庝箞鏍瘋璁★紙寤哄嚑涓埧闂達紝鍑犱釜闂ㄥソ鐪嬶紝灞傛杈冮珮錛夛紝 浜庢槸鎴戦渶瑕佹壘涓€甯皯宸ワ紝浠栦滑浼氱爩澧欙紝榪樺緱鎵句釜璁捐甯堬紝浠栫煡閬撴€庝箞璁捐錛屾垜榪樿紜繚姘戝伐鍚璁″笀鐨勯瀵鹼紝鑰岃璁″笀鏈韓涔熶笉騫叉椿錛屽厜鏄笅鍛戒護錛岃繖閲岀爩涓€鍫靛錛岃繖閲岀爩涓€鎵囬棬錛岃繖鏍鋒皯宸ュ紑濮嬪緩璁撅紝鏈€鍚庯紝鎴戝彲浠ュ悜姘戝伐瑕佹埧瀛愪簡銆傚湪榪欎釜榪囩▼涓紝璁捐甯堟槸浠€涔堜篃娌℃湁錛岄櫎浜嗕粬鍦ㄨ剳瀛愰噷鐨勮璁″拰鍛戒護錛屾墍浠ヨ鎴垮瓙涔熸槸璺熸皯宸ヨ錛岃浣忎簡錛?br /><br />灝辮薄鍥藉唴濂藉浼佷笟涓奺rp涓€鏍鳳紝涓奺rp錛岄鍏堝緱鎵捐蔣浠跺叕鍙稿憖錛屾壘鍒拌蔣浠跺叕鍙稿悗錛岃蔣浠跺叕鍙歌錛屾垜鍙煡閬撴€庝箞鍐欒蔣浠訛紝灝辯煡閬撴€庝箞瀹炵幇錛屼笉娓呮鏁翠釜erp鐨勬祦紼嬨€傚ソ錛岄偅鎴戜滑榪樺緱鎵句竴涓挩璇㈠叕鍙革紝濂斤紝鎵懼埌寰峰嫟浜嗭紝寰峰嫟璇村ソ錛屾垜瑕佽蔣浠舵€庝箞鍋氾紝杞歡鍏徃鎬庝箞鍋氾紝鎴戝氨鑳戒繚璇佽蔣浠惰兘涓轟綘浠彁渚沞rp緋葷粺浜嗐€?br /><br />姝ゆā寮忔槸涓轟簡璁╄璁″拰鏂藉伐瑙h€︼紝浜掍笉騫叉壈銆?br /><br /><br />package builder;<br /><br />public interface Builder<br />{<br />public聽 void makeWindow();<br />public聽 void makeFloor();<br />public聽 Room聽 getRoom();<br /><br />}<br /><br /><br />/*************************************************************/<br />package builder;<br /><br />public class Designer {<br /><br />聽 public Designer() {<br />聽 }<br />聽 public void order(Builder聽 builder)聽 <br />{<br />聽 builder.makeWindow();<br />聽 builder.makeFloor();<br /><br />}<br />}<br /><br />/*************************************************************/<br />package builder;<br />public class Mingong聽 implements Builder{<br />聽 private聽 String window="";<br />聽 private聽 String floor="";<br />聽 public Mingong() {<br />聽 }<br />聽 public聽 void makeWindow(){<br />聽 window=new String("window");<br />聽 }<br />聽 public聽 void makeFloor(){<br />聽 floor=new String("floor");<br />聽 }<br />聽 public聽 Room聽 getRoom()<br />聽 {<br />聽 if((!window.equals(""))&&(!floor.equals("")))<br />聽聽聽 {<br />聽聽聽 Room r = new Room();<br />聽聽聽 r.setFloor(floor);<br />聽聽聽 r.setWindow(window);<br />聽聽聽 return r;<br />聽聽聽 }<br />聽 else return null;<br />聽 }<br />}<br /><br />/*************************************************************/<br />package builder;<br />public class Room {<br />聽 private聽 String window="";<br />聽 private聽 String floor="";<br />聽 public Room() {<br />聽 }<br />聽public String getFloor() {<br />聽聽return floor;<br />聽}</p><p>聽public void setFloor(String floor) {<br />聽聽this.floor = floor;<br />聽}</p><p>聽public String getWindow() {<br />聽聽return window;<br />聽}</p><p>聽public void setWindow(String window) {<br />聽聽this.window = window;<br />聽}<br />}<br />/*************************************************************/<br /><br />package builder;<br /><br />public class Client {<br /><br />聽 public Client() {<br />聽 }<br />聽 public static void main(String[] args) {<br />聽聽聽聽 Builder mingong=new Mingong();<br />聽聽聽聽 Designer聽 designer=new聽聽聽聽聽 Designer();<br />聽聽聽聽 designer.order(mingong);<br />聽聽聽聽聽Room r聽= mingong.getRoom();<br />聽聽聽聽 System.out.println(r.getWindow);//浼氳緭鍑簑indow錛岃〃紺烘瀯寤烘垚鍔熴€?br />聽 }<br />}<br /></p><img src ="http://www.aygfsteel.com/renyangok/aggbug/59814.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/renyangok/" target="_blank">淇濆皵浠?/a> 2006-07-24 15:19 <a href="http://www.aygfsteel.com/renyangok/articles/59814.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Template Method錛堟ā鏉挎柟娉曪級妯″紡http://www.aygfsteel.com/renyangok/articles/59374.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Fri, 21 Jul 2006 02:29:00 GMThttp://www.aygfsteel.com/renyangok/articles/59374.htmlhttp://www.aygfsteel.com/renyangok/comments/59374.htmlhttp://www.aygfsteel.com/renyangok/articles/59374.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/59374.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/59374.html闇€瑕侀伒寰竴涓繃紼嬫垨涓€緋誨垪姝ラ錛屽畠浠湪鏌愪釜鍏蜂綋灞傛涓婁繚鎸佷竴鑷達紝浣嗗崟涓楠ゅ湪鏇磋緇嗙殑灞傛涓婂彲鑳芥湁涓嶅悓鐨勫疄鐜版椂錛岀敤姝ゆā寮忋€傛湁浜洪棶錛氫負浠€涔堜笉鐢ㄥ涓猻trategy妯″紡鍟婏紵鎴戠殑鐞嗚В錛歴trategy妯″紡鏄瘡涓€姝ラ兘鍙互閫夊涓疄鐜幫紝姣斿甯藉瓙鍙互閫塧,b,c錛岄瀷瀛愬彲浠ラ€塧,b,c錛屼絾teamplate method鏄寚鍙兘閫変竴濂梐錛屾垨涓€濂梑錛屼竴濂梒錛屼笉鑳藉垎寮€閫夛紝姝ゆ椂a錛宐錛宑鐩存帴鏈夊浐瀹氱殑鑱旂郴銆?br />

]]>
Observer錛堣瀵熻€咃級妯″紡http://www.aygfsteel.com/renyangok/articles/59127.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Thu, 20 Jul 2006 02:15:00 GMThttp://www.aygfsteel.com/renyangok/articles/59127.htmlhttp://www.aygfsteel.com/renyangok/comments/59127.htmlhttp://www.aygfsteel.com/renyangok/articles/59127.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/59127.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/59127.html鎸夌収鈥滃洓浜哄洟鈥濈殑璇存硶錛孫bserver妯″紡鐨勬剰鍥炬槸鈥滃畾涔夊璞¢棿鐨勪竴縐嶄竴瀵瑰鐨勪緷璧栧叧緋伙紝褰撲竴涓璞$殑鐘舵€佸彂鐢熸敼鍙樻椂錛屾墍鏈変緷璧栦簬瀹冪殑瀵硅薄閮藉緱鍒伴€氱煡騫惰鑷姩鏇存柊鈥濄€?br />闂錛氬綋鏌愪釜浜嬩歡鍙戠敓鏃訛紝浣犻渶瑕佸悜涓€緋誨垪瀵硅薄鍙戝嚭閫氱煡錛?strong>鑰岃繖涓璞$殑鍒楄〃鏄笉鏂彉鍖栫殑銆傦紙鑰屽綋渚濊禆鍏崇郴鍥哄畾鎴栧嚑涔庡浐瀹氭椂錛屽姞鍏bserver妯″紡鍙細澧炲姞澶嶆潅鎬э級

綆€鍗曟潵璇達紝Observer妯″紡璁╀竴涓璞★紙瑙傚療鑰咃紝Observer錛夊幓鐩戣鍙︿竴涓璞★紙鐩爣錛孲ubject錛夛紱瀹冧嬌寰楃洰鏍囧拰瑙傚療鑰呬箣闂村緩绔嬩竴縐?"鍙戝竷--璁㈤槄"錛坧ublish-subscribe 錛夌殑鍏崇郴銆傞€氳繃Observer妯″紡錛岃瀵熻€呭彲浠ュ悜鐩爣鐧昏錛岃〃鏄庤嚜宸辮浠庣洰鏍囨帴鏀朵簨浠躲€傜洰鏍囬渶瑕佸悜瑙傚療鑰呴€氱煡浜嬩歡鏃訛紝鍙槸綆€鍗曞湴灝嗕簨浠跺彂緇欐瘡涓€涓瀵熻€呫€?

渚嬪錛屾湁涓€涓熀浜庢煇縐嶆暟鎹ā鍨嬬殑鐢靛瓙琛ㄦ牸銆傚彧瑕佹暟鎹ā鍨嬪彂鐢熷彉鍖栵紝鐢靛瓙琛ㄦ牸灝遍渶瑕佹洿鏂拌〃鏍煎崟鍏冧互鍙婂唴宓岀殑鍥捐〃銆傝繖涓緥瀛愪腑錛岀洰鏍囨槸鏁版嵁妯″瀷錛岃瀵熻€呮槸琛ㄦ牸鍗曞厓鍜屽浘琛ㄣ€傚綋瑙傚療鑰呮帴鏀跺埌鏁版嵁妯″瀷宸茬粡鍙樺寲鐨勯€氱煡鏃訛紝瀹冧滑灝辨洿鏂拌嚜宸便€?

Observer妯″紡鐨勫ソ澶勬槸錛氬畠瑙i櫎浜嗚瀵熻€呭拰鐩爣涔嬮棿鐨勮€﹀悎鍏崇郴銆傜洰鏍囦笉闇€瑕佺煡閬撳畠鐨勮瀵熻€呯殑浠諱綍淇℃伅銆傜浉鍙嶏紝鐩爣鍙槸鍏佽瑙傚療鑰呰闃呬簨浠躲€傚綋鐩爣浜х敓涓€涓簨浠舵椂錛屽畠綆€鍗曞湴灝嗕簨浠朵紶緇欐瘡涓€涓瀵熻€呫€?

鐪嬬湅涓嬮潰鐨凧ava紺轟緥錛?

public interface Subject {
public void addObserver( Observer o );
public void removeObserver( Observer o );
}

涓婇潰鐨勪唬鐮佷腑錛孲ubject鎺ュ彛瀹氫箟浜嗕袱涓柟娉曪紙method錛夛紝姣忎釜Subject閮藉繀欏誨疄鐜板畠浠紝浠ヤ嬌Observer鍙互鍦⊿ubject涓鍔犳垨鍒犻櫎鑷韓銆?

public interface Observer {
public void update( Subject o );
}

Observer鎺ュ彛錛堝涓婏級鍒楀嚭浜嗕竴涓柟娉曪紙method錛夛紝姣忎釜Observer閮藉繀欏誨疄鐜板畠錛屼互浣縎ubject鍙互鍙戦€佹洿鏂版秷鎭粰Observer銆?

涓嬮潰鐪嬬湅Subject鐨勪竴涓畝鍗曠殑瀹炵幇--IntegerDataBag錛?

import java.util.ArrayList;
import java.util.Iterator;

public class IntegerDataBag implements Subject {

private ArrayList list = new ArrayList();
private ArrayList observers = new ArrayList();

public void add( Integer i ) {
list.add( i );
notifyObservers();
}

public Iterator iterator() {
return list.iterator();
}

public Integer remove( int index ) {
if( index < list.size() ) {
Integer i = (Integer) list.remove( index );
notifyObservers();
return i;
}
return null;
}

public void addObserver( Observer o ) {
observers.add( o );
}

public void removeObserver( Observer o ) {
observers.remove( o );
}

private void notifyObservers() {
// loop through and notify each observer
Iterator i = observers.iterator();
while( i.hasNext() ) {
Observer o = ( Observer ) i.next();
o.update( this );
}
}
}

IntegerDataBag閫傜敤浜庝嬌鐢↖nteger鐨勫満鍚堛€侷ntegerDataBag涔熷厑璁窸bserver澧炲姞鍜屽垹闄ゅ畠浠嚜韜€?

鍐嶇湅鐪嬩袱涓狾bserver鐨勫疄鐜?-IntegerAdder鍜孖ntegerPrinter錛?

import java.util.Iterator;

public class IntegerAdder implements Observer {

private IntegerDataBag bag;

public IntegerAdder( IntegerDataBag bag ) {
this.bag = bag;
bag.addObserver( this );
}

public void update( Subject o ) {
if( o == bag ) {
System.out.println( "The contents of the IntegerDataBag have changed." );
int counter = 0;
Iterator i = bag.iterator();
while( i.hasNext() ) {
Integer integer = ( Integer ) i.next();
counter+=integer.intValue();
}
System.out.println( "The new sum of the integers is: " + counter );
}
}

}

import java.util.Iterator;

public class IntegerPrinter implements Observer {

private IntegerDataBag bag;

public IntegerPrinter( IntegerDataBag bag ) {
this.bag = bag;
bag.addObserver( this );
}

public void update( Subject o ) {
if( o == bag ) {
System.out.println( "The contents of the IntegerDataBag have changed." );
System.out.println( "The new contents of the IntegerDataBag contains:" );
Iterator i = bag.iterator();
while( i.hasNext() ) {
System.out.println( i.next() );
}
}
}

}

IntegerAdder鍜孖ntegerPrinter灝嗚嚜宸變綔涓鴻瀵熻€呭鍔犲埌IntegerDataBag銆傚綋IntegerAdder鎺ユ敹鍒頒竴鏉℃洿鏂版秷鎭椂錛屽畠鍏堢粺璁ag涓殑鎬繪暟錛岀劧鍚庢樉紺虹粨鏋溿€傚悓鏍鳳紝褰揑ntegerPrinter鎺ユ敹鍒頒竴鏉℃洿鏂版秷鎭椂錛屽畠鎵撳嵃鍑篵ag涓殑Interger銆?

涓嬮潰鏄竴涓畝鍗曠殑main()錛屽畠浣跨敤浜嗕笂闈㈢殑鍑犱釜綾伙細

public class Driver {
public static void main( String [] args ) {
Integer i1 = new Integer( 1 ); Integer i2 = new Integer( 2 );
Integer i3 = new Integer( 3 ); Integer i4 = new Integer( 4 );
Integer i5 = new Integer( 5 ); Integer i6 = new Integer( 6 );
Integer i7 = new Integer( 7 ); Integer i8 = new Integer( 8 );
Integer i9 = new Integer( 9 );

IntegerDataBag bag = new IntegerDataBag();
bag.add( i1 ); bag.add( i2 ); bag.add( i3 ); bag.add( i4 );
bag.add( i5 ); bag.add( i6 ); bag.add( i7 ); bag.add( i8 );

IntegerAdder adder = new IntegerAdder( bag );
IntegerPrinter printer = new IntegerPrinter( bag );

// adder and printer add themselves to the bag

System.out.println( "About to add another integer to the bag:" );
bag.add( i9 );
System.out.println("");
System.out.println("About to remove an integer from the bag:");
bag.remove( 0 );
}
}

榪愯main錛屼綘灝嗙湅鍒幫細

c:\javaworld\java Driver
About to add another integer to the bag:
The contents of the IntegerDataBag have changed.
The new sum of the intergers is: 45
The contents of the IntegerDataBag have changed.
The new contents of the IntegerDataBag contains:
1
2
3
4
5
6
7
8
9

About to remove an integer from the bag:
The contents of the IntegerDataBag have changed.
The new sum of the intergers is: 44
The contents of the IntegerDataBag have changed.
The new contents of the IntegerDataBag contains:
2
3
4
5
6
7
8
9

IntegerDataBag/IntegerAdder/IntegerPrinter鏄簲鐢∣bserver妯″紡鐨勪竴涓緢綆€鍗曠殑渚嬪瓙銆侸ava鏈韓鏈夊ぇ閲忎嬌鐢∣bserver妯″紡鐨勪緥瀛愶細AWT/Swing浜嬩歡妯″瀷錛岃繕鏈塲ava.util.Observer鍜宩ava.util.Observable鎺ュ彛絳夛紝閮芥槸寰堝ソ鐨勪緥瀛?br />



]]>
Decorator錛堣楗幫級妯″紡http://www.aygfsteel.com/renyangok/articles/59008.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Wed, 19 Jul 2006 08:02:00 GMThttp://www.aygfsteel.com/renyangok/articles/59008.htmlhttp://www.aygfsteel.com/renyangok/comments/59008.htmlhttp://www.aygfsteel.com/renyangok/articles/59008.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/59008.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/59008.html
銆€銆€ Decorator妯″紡綆€浠嬄?br />
銆€銆€ Decorator妯″紡鍙堝悕鍖呰鍣?Wrapper)錛屽畠鐨勪富瑕佺敤閫斿湪浜?strong>緇欎竴涓璞″姩鎬佺殑娣誨姞涓€浜涢澶栫殑鑱岃矗銆備笌鐢熸垚瀛愮被鐩告瘮錛屽畠鏇村叿鏈夌伒媧繪€?/strong>銆?br />鏈夋椂鍊欙紝鎴戜滑闇€瑕佷負涓€涓璞¤€屼笉鏄暣涓被娣誨姞涓€浜涙柊鐨勫姛鑳斤紝姣斿錛岀粰涓€涓枃鏈尯娣誨姞涓€涓粴鍔ㄦ潯鐨勫姛鑳姐€傛垜浠彲浠ヤ嬌鐢ㄧ戶鎵挎満鍒舵潵瀹炵幇榪欎竴鍔熻兘錛屼絾鏄繖縐嶆柟娉曚笉澶熺伒媧伙紝鎴戜滑鏃犳硶鎺у埗鏂囨湰鍖哄姞婊氬姩鏉$殑鏂瑰紡鍜屾椂鏈恒€傝€屼笖褰撴枃鏈尯闇€瑕佹坊鍔犳洿澶氱殑鍔熻兘鏃訛紝姣斿杈規絳夛紝闇€瑕佸垱寤烘柊鐨勭被錛岃€屽綋闇€瑕佺粍鍚堜嬌鐢ㄨ繖浜涘姛鑳芥椂鏃犵枒灝嗕細寮曡搗綾葷殑鐖嗙偢銆?

銆€銆€ 鎴戜滑鍙互浣跨敤涓€縐嶆洿涓虹伒媧葷殑鏂規硶錛屽氨鏄妸鏂囨湰鍖哄祵鍏ュ埌婊氬姩鏉′腑銆傝€岃繖涓粴鍔ㄦ潯鐨勭被灝辯浉褰撲簬瀵規枃鏈尯鐨勪竴涓?strong>瑁呴グ銆?榪欎釜瑁呴グ(婊氬姩鏉?蹇呴』涓庤瑁呴グ鐨勭粍浠?鏂囨湰鍖?緇ф壙鑷悓涓€涓帴鍙o紝榪欐牱錛岀敤鎴峰氨涓嶅繀鍏沖績瑁呴グ鐨勫疄鐜幫紝鍥犱負榪欏浠栦滑鏉ヨ鏄€忔槑鐨勩€傝楗頒細灝嗙敤鎴風殑璇鋒眰杞彂 緇欑浉搴旂殑緇勪歡(鍗寵皟鐢ㄧ浉鍏崇殑鏂規硶)錛屽茍鍙兘鍦ㄨ漿鍙戠殑鍓嶅悗鍋氫竴浜涢澶栫殑鍔ㄤ綔(濡傛坊鍔犳粴鍔ㄦ潯)銆傞€氳繃榪欑鏂規硶錛屾垜浠彲浠ユ牴鎹粍鍚堝鏂囨湰鍖哄祵濂椾笉鍚岀殑瑁呴グ錛屼粠鑰?娣誨姞浠繪剰澶氱殑鍔熻兘銆傝繖縐嶅姩鎬佺殑瀵瑰璞℃坊鍔犲姛鑳界殑鏂規硶涓嶄細寮曡搗綾葷殑鐖嗙偢錛屼篃鍏鋒湁浜嗘洿澶氱殑鐏墊椿鎬с€?br />
銆€銆€ 浠ヤ笂鐨勬柟娉曞氨鏄?strong>Decorator妯″紡錛屽畠閫氳繃緇欏璞℃坊鍔犺楗版潵鍔ㄦ€佺殑娣誨姞鏂扮殑鍔熻兘銆傚涓嬫槸Decorator妯″紡鐨刄ML鍥撅細



銆€銆€ Component涓虹粍浠跺拰瑁呴グ鐨勫叕鍏辯埗綾伙紝瀹冨畾涔変簡瀛愮被蹇呴』瀹炵幇鐨勬柟娉曘€?br />
銆€銆€ ConcreteComponent鏄竴涓叿浣撶殑緇勪歡綾伙紝鍙互閫氳繃緇欏畠娣誨姞瑁呴グ鏉ュ鍔犳柊鐨勫姛鑳姐€?br />
銆€銆€ Decorator鏄墍鏈夎楗扮殑鍏叡鐖剁被錛屽畠瀹氫箟浜嗘墍鏈夎楗板繀欏誨疄鐜扮殑鏂規硶錛屽悓鏃訛紝瀹冭繕淇濆瓨浜嗕竴涓浜嶤omponent鐨勫紩鐢紝浠ヤ究灝嗙敤鎴風殑璇鋒眰杞彂緇機omponent錛屽茍鍙兘鍦ㄨ漿鍙戣姹傚墠鍚庢墽琛屼竴浜涢檮鍔犵殑鍔ㄤ綔銆?br />
銆€銆€ ConcreteDecoratorA鍜孋oncreteDecoratorB鏄叿浣撶殑瑁呴グ錛屽彲浠ヤ嬌鐢ㄥ畠浠潵瑁呴グ鍏蜂綋鐨凜omponent銆?br />
銆€銆€Java IO鍖呬腑鐨凞ecorator妯″紡

銆€銆€ JDK鎻愪緵鐨刯ava.io鍖呬腑浣跨敤浜咲ecorator妯″紡鏉ュ疄鐜板鍚勭杈撳叆杈撳嚭嫻佺殑灝佽銆備互涓嬪皢浠ava.io.OutputStream鍙婂叾瀛愮被涓轟緥錛岃璁轟竴涓婦ecorator妯″紡鍦↖O涓殑浣跨敤銆?br />
銆€銆€ 棣栧厛鏉ョ湅涓€孌電敤鏉ュ垱寤篒O嫻佺殑浠g爜錛?br />
浠ヤ笅鏄唬鐮佺墖孌碉細
try {
銆€ OutputStream out = new DataOutputStream(new FileOutputStream("test.txt"));
} catch (FileNotFoundException e) {
銆€ e.printStackTrace();
}

銆€ 銆€榪欐浠g爜瀵逛簬浣跨敤榪嘕AVA杈撳叆杈撳嚭嫻佺殑浜烘潵璇村啀鐔熸倝涓嶈繃浜嗭紝鎴戜滑浣跨敤DataOutputStream灝佽浜嗕竴涓狥ileOutputStream銆?榪欐槸涓€涓吀鍨嬬殑Decorator妯″紡鐨勪嬌鐢紝FileOutputStream鐩稿綋浜嶤omponent錛孌ataOutputStream灝辨槸涓€涓?Decorator銆傚皢浠g爜鏀規垚濡備笅錛屽皢浼氭洿瀹規槗鐞嗚В錛?br />
浠ヤ笅鏄唬鐮佺墖孌碉細
try {
銆€ OutputStream out = new FileOutputStream("test.txt");
銆€ out = new DataOutputStream(out);
} catch(FileNotFoundException e) {
銆€ e.printStatckTrace();
}

銆€銆€ 鐢變簬FileOutputStream鍜孌ataOutputStream鏈夊叕鍏辯殑鐖剁被OutputStream錛屽洜姝ゅ瀵硅薄鐨勮楗板浜庣敤鎴鋒潵璇村嚑涔庢槸閫忔槑鐨勩€備笅闈㈠氨鏉ョ湅鐪婳utputStream鍙婂叾瀛愮被鏄浣曟瀯鎴怐ecorator妯″紡鐨勶細



銆€銆€ OutputStream鏄竴涓娊璞$被錛屽畠鏄墍鏈夎緭鍑烘祦鐨勫叕鍏辯埗綾伙紝鍏舵簮浠g爜濡備笅錛?br />
浠ヤ笅鏄唬鐮佺墖孌碉細
public abstract class OutputStream implements Closeable, Flushable {
銆€ public abstract void write(int b) throws IOException;
銆€ ...
}

銆€銆€ 瀹冨畾涔変簡write(int b)鐨勬娊璞℃柟娉曘€傝繖鐩稿綋浜嶥ecorator妯″紡涓殑Component綾匯€?br />
銆€銆€ ByteArrayOutputStream錛孎ileOutputStream 鍜?PipedOutputStream 涓変釜綾婚兘鐩存帴浠嶰utputStream緇ф壙錛屼互ByteArrayOutputStream涓轟緥錛?br />
浠ヤ笅鏄唬鐮佺墖孌碉細
public class ByteArrayOutputStream extends OutputStream {
銆€ protected byte buf[];
銆€ protected int count;
銆€ public ByteArrayOutputStream() {
銆€銆€ this(32);
銆€ }
銆€ public ByteArrayOutputStream(int size) {
銆€銆€ if (size 銆?0) {
銆€銆€銆€ throw new IllegalArgumentException("Negative initial size: " + size);
銆€銆€ }
銆€銆€ buf = new byte[size];
銆€ }
銆€ public synchronized void write(int b) {
銆€銆€ int newcount = count + 1;
銆€銆€ if (newcount 銆?buf.length) {
銆€銆€銆€ byte newbuf[] = new byte[Math.max(buf.length 銆堛€?1, newcount)];
銆€銆€銆€ System.arraycopy(buf, 0, newbuf, 0, count);
銆€銆€銆€ buf = newbuf;
銆€銆€ }
銆€銆€ buf[count] = (byte)b;
銆€銆€ count = newcount;
銆€ }
銆€ ...
}

銆€銆€ 瀹冨疄鐜頒簡OutputStream涓殑write(int b)鏂規硶錛屽洜姝ゆ垜浠彲浠ョ敤鏉ュ垱寤鴻緭鍑烘祦鐨勫璞★紝騫跺畬鎴愮壒瀹氭牸寮忕殑杈撳嚭銆傚畠鐩稿綋浜嶥ecorator妯″紡涓殑ConcreteComponent綾匯€?br />
銆€銆€ 鎺ョ潃鏉ョ湅涓€涓婩ilterOutputStream錛屼唬鐮佸涓嬶細

浠ヤ笅鏄唬鐮佺墖孌碉細
public class FilterOutputStream extends OutputStream {
銆€ protected OutputStream out;
銆€ public FilterOutputStream(OutputStream out) {
銆€銆€ this.out = out;
銆€ }
銆€public void write(int b) throws IOException {
銆€銆€ out.write(b);
銆€ }
銆€...
}

銆€ 銆€鍚屾牱錛屽畠涔熸槸浠嶰utputStream緇ф壙銆備絾鏄紝瀹冪殑鏋勯€犲嚱鏁板緢鐗瑰埆錛岄渶瑕佷紶閫掍竴涓狾utputStream鐨勫紩鐢ㄧ粰瀹冿紝騫朵笖瀹冨皢淇濆瓨瀵規瀵硅薄鐨勫紩 鐢ㄣ€傝€屽鏋滄病鏈夊叿浣撶殑OutputStream瀵硅薄瀛樺湪錛屾垜浠皢鏃犳硶鍒涘緩FilterOutputStream銆傜敱浜巓ut鏃㈠彲浠ユ槸鎸囧悜 FilterOutputStream綾誨瀷鐨勫紩鐢紝涔熷彲浠ユ槸鎸囧悜ByteArrayOutputStream絳夊叿浣撹緭鍑烘祦綾葷殑寮曠敤錛屽洜姝や嬌鐢ㄥ灞傚祵濂楃殑鏂?寮忥紝鎴戜滑鍙互涓築yteArrayOutputStream娣誨姞澶氱瑁呴グ銆傝繖涓狥ilterOutputStream綾葷浉褰撲簬Decorator妯″紡涓殑 Decorator綾伙紝瀹冪殑write(int b)鏂規硶鍙槸綆€鍗曠殑璋冪敤浜嗕紶鍏ョ殑嫻佺殑write(int b)鏂規硶錛岃€屾病鏈夊仛鏇村鐨勫鐞嗭紝鍥犳瀹冩湰璐ㄤ笂娌℃湁瀵規祦榪涜瑁呴グ錛屾墍浠ョ戶鎵垮畠鐨勫瓙綾誨繀欏昏鐩栨鏂規硶錛屼互杈懼埌瑁呴グ鐨勭洰鐨勩€?br />
銆€銆€ BufferedOutputStream 鍜?DataOutputStream鏄疐ilterOutputStream鐨勪袱涓瓙綾伙紝瀹冧滑鐩稿綋浜嶥ecorator妯″紡涓殑 ConcreteDecorator錛屽茍瀵逛紶鍏ョ殑杈撳嚭嫻佸仛浜嗕笉鍚岀殑瑁呴グ銆備互BufferedOutputStream綾諱負渚嬶細

浠ヤ笅鏄唬鐮佺墖孌碉細
public class BufferedOutputStream extends FilterOutputStream {
銆€ ...
銆€ private void flushBuffer() throws IOException {
銆€銆€ if (count 銆?0) {
銆€銆€銆€ out.write(buf, 0, count);
銆€銆€銆€ count = 0;
銆€銆€ }
銆€ }
銆€ public synchronized void write(int b) throws IOException {
銆€銆€ if (count 銆? buf.length) {
銆€銆€銆€ flushBuffer();
銆€銆€ }
銆€銆€ buf[count++] = (byte)b;
銆€ }
銆€ ...
}


銆€ 銆€榪欎釜綾繪彁渚涗簡涓€涓紦瀛樻満鍒訛紝絳夊埌緙撳瓨鐨勫閲忚揪鍒頒竴瀹氱殑瀛楄妭鏁版椂鎵嶅啓鍏ヨ緭鍑烘祦銆傞鍏堝畠緇ф壙浜咶ilterOutputStream錛屽茍涓旇鐩栦簡鐖剁被鐨?write(int b)鏂規硶錛屽湪璋冪敤杈撳嚭嫻佸啓鍑烘暟鎹墠閮戒細媯€鏌ョ紦瀛樻槸鍚﹀凡婊★紝濡傛灉鏈弧錛屽垯涓嶅啓銆傝繖鏍峰氨瀹炵幇浜嗗杈撳嚭嫻佸璞″姩鎬佺殑娣誨姞鏂板姛鑳界殑鐩殑銆?br />
銆€銆€ 涓嬮潰錛屽皢浣跨敤Decorator妯″紡錛屼負IO鍐欎竴涓柊鐨勮緭鍑烘祦銆?br />
銆€銆€ 鑷繁鍐欎竴涓柊鐨勮緭鍑烘祦

銆€銆€ 浜嗚В浜哋utputStream鍙婂叾瀛愮被鐨勭粨鏋勫師鐞嗗悗錛屾垜浠彲浠ュ啓涓€涓柊鐨勮緭鍑烘祦錛屾潵娣誨姞鏂扮殑鍔熻兘銆傝繖閮ㄥ垎涓皢緇欏嚭涓€涓柊鐨勮緭鍑烘祦鐨勪緥瀛愶紝瀹冨皢榪囨護寰呰緭鍑鴻鍙ヤ腑鐨勭┖鏍肩鍙楓€傛瘮濡傞渶瑕佽緭鍑?java聽io聽OutputStream"錛屽垯榪囨護鍚庣殑杈撳嚭涓?javaioOutputStream"銆備互涓嬩負SkipSpaceOutputStream綾葷殑浠g爜錛?br />

浠ヤ笅鏄唬鐮佺墖孌碉細
import聽java.io.FilterOutputStream;聽
import聽java.io.IOException;聽
import聽java.io.OutputStream;聽
/**聽
*聽A聽new聽output聽stream,聽which聽will聽check聽the聽space聽character聽
*聽and聽won't聽write聽it聽to聽the聽output聽stream.聽
*聽@author聽Magic聽
*聽
*/聽
public聽class聽SkipSpaceOutputStream聽extends聽FilterOutputStream聽{聽
銆€ public聽SkipSpaceOutputStream(OutputStream聽out)聽{聽
銆€銆€ super(out);聽
銆€ }聽
銆€ /**聽
銆€ *聽Rewrite聽the聽method聽in聽the聽parent聽class,聽and聽
銆€ *聽skip聽the聽space聽character.聽
銆€ */聽
銆€ public聽void聽write(int聽b)聽throws聽IOException{聽
銆€銆€ if(b!='聽'){聽
銆€銆€銆€ super.write(b);聽
銆€銆€ }聽
銆€ }聽
}

銆€銆€ 瀹冧粠FilterOutputStream緇ф壙錛屽茍涓旈噸鍐欎簡瀹冪殑write(int聽b)鏂規硶銆傚湪write(int聽b)鏂規硶涓鍏堝杈撳叆瀛楃榪涜浜嗘鏌ワ紝濡傛灉涓嶆槸絀烘牸錛屽垯杈撳嚭銆?br />
銆€銆€ 浠ヤ笅鏄竴涓祴璇曠▼搴忥細


浠ヤ笅鏄唬鐮佺墖孌碉細
import聽java.io.BufferedInputStream;聽
import聽java.io.DataInputStream;聽
import聽java.io.DataOutputStream;聽
import聽java.io.IOException;聽
import聽java.io.InputStream;聽
import聽java.io.OutputStream;聽
/**聽
*聽Test聽the聽SkipSpaceOutputStream.聽
*聽@author聽Magic聽
*聽
*/聽
public聽class聽Test聽{聽
銆€ public聽static聽void聽main(String[]聽args){聽
銆€銆€ byte[]聽buffer聽=聽new聽byte[1024];聽

銆€銆€ /**聽
銆€銆€ *聽Create聽input聽stream聽from聽the聽standard聽input.聽
銆€銆€ */聽
銆€銆€ InputStream聽in聽=聽new聽BufferedInputStream(new聽DataInputStream(System.in));聽

銆€銆€ /**聽
銆€銆€ *聽write聽to聽the聽standard聽output.聽
銆€銆€ */聽
銆€銆€ OutputStream聽out聽=聽new聽SkipSpaceOutputStream(new聽DataOutputStream(System.out));聽

銆€銆€ try聽{聽
銆€銆€銆€ System.out.println("Please聽input聽your聽words:聽");聽
銆€銆€銆€ int聽n聽=聽in.read(buffer,0,buffer.length);聽
銆€銆€銆€ for(int聽i=0;i< n;i++){聽
銆€銆€銆€銆€ out.write(buffer[i]);聽
銆€銆€銆€ }聽
銆€銆€ }聽catch聽(IOException聽e)聽{聽
銆€銆€銆€ e.printStackTrace();聽
銆€銆€ }聽
銆€ }聽
}

銆€銆€ 鎵ц浠ヤ笂嫻嬭瘯紼嬪簭錛屽皢瑕佹眰鐢ㄦ埛鍦╟onsole紿楀彛涓緭鍏ヤ俊鎭紝紼嬪簭灝嗚繃婊ゆ帀淇℃伅涓殑絀烘牸錛屽茍灝嗘渶鍚庣殑緇撴灉杈撳嚭鍒癱onsole紿楀彛銆傛瘮濡傦細


浠ヤ笅鏄紩鐢ㄧ墖孌碉細
Please聽input聽your聽words:聽
a聽b聽c聽d聽e聽f聽
abcdef

銆€銆€ 鎬宦犵粨

銆€銆€ 鍦╦ava.io鍖呬腑錛屼笉浠匫utputStream鐢ㄥ埌浜咲ecorator璁捐妯″紡錛孖nputStream錛孯eader錛學riter絳夐兘鐢ㄥ埌浜嗘妯″紡銆傝€屼綔涓轟竴涓伒媧葷殑錛屽彲鎵╁睍鐨勭被搴擄紝JDK涓嬌鐢ㄤ簡澶ч噺鐨勮璁℃ā寮忥紝姣斿鍦⊿wing鍖呬腑鐨凪VC妯″紡錛孯MI涓殑Proxy妯″紡絳夌瓑銆傚浜嶫DK涓ā寮忕殑鐮旂┒涓嶄粎鑳藉姞娣卞浜庢ā寮忕殑鐞嗚В錛岃€屼笖榪樻湁鍒╀簬鏇撮€忓交鐨勪簡瑙g被搴撶殑緇撴瀯鍜岀粍鎴愩€偮?/span>



錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞




decorator聽for聽non-visualable聽class聽e.g.聽java.io.*

The聽class聽FilterInputStream聽itself聽simply聽overrides聽all聽methods聽of聽InputStream聽with聽versions聽that聽pass聽all聽requests聽to聽the聽underlying聽input聽stream.聽Subclasses聽of聽FilterInputStream聽may聽further聽override聽some聽of聽these聽methods聽as聽well聽as聽provide聽additional聽methods聽and聽fields.聽

The聽FilterInputStream聽class聽is聽thus聽a聽Decorator聽that聽can聽be聽wrapped聽around聽any聽input聽stream聽class.聽It聽is聽essentially聽an聽abstract聽class聽that聽doesn鈥檛聽do聽any聽processing,聽but聽provides聽a聽layer聽where聽the聽relevant聽methods聽have聽been聽duplicated.聽It聽normally聽forwards聽these聽method聽calls聽to聽the聽enclosed聽parent聽stream聽class.聽The聽interesting聽classes聽derived聽from聽FilterInputStream聽include:聽

BufferedInputStream聽Adds聽buffering聽to聽stream聽so聽that聽every聽call聽does

not聽cause聽I/O聽to聽occur.

CheckedInputStream聽聽Maintains聽a聽checksum聽of聽bytes聽as聽they聽are聽read

DataInputStream聽聽聽聽聽Reads聽primitive聽types聽(Long,聽Boolean,聽Float,聽etc.)

from聽the聽input聽stream.

DigestInputStream聽聽聽Computes聽a聽MessageDigest聽of聽any聽input聽stream.

InflaterInputStream聽Implements聽methods聽for聽uncompressing聽data.

PushbackInputStream聽Provides聽a聽buffer聽where聽data聽can聽be聽鈥渦nread,鈥澛爄f

during聽parsing聽you聽discover聽you聽need聽to聽back聽up.

]]>
Strategy錛堢瓥鐣ワ級妯″紡http://www.aygfsteel.com/renyangok/articles/58975.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Wed, 19 Jul 2006 06:00:00 GMThttp://www.aygfsteel.com/renyangok/articles/58975.htmlhttp://www.aygfsteel.com/renyangok/comments/58975.htmlhttp://www.aygfsteel.com/renyangok/articles/58975.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/58975.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/58975.htmlStrategy絳栫暐妯″紡鏄睘浜庤璁℃ā寮忎腑 瀵硅薄琛屼負鍨嬫ā寮?涓昏鏄畾涔変竴緋誨垪鐨勭畻娉?鎶婅繖浜涚畻娉曚竴涓釜灝佽鎴愬崟鐙殑綾?

Stratrgy搴旂敤姣旇緝騫挎硾,姣斿, 鍏徃緇忚惀涓氬姟鍙樺寲鍥? 鍙兘鏈変袱縐嶅疄鐜版柟寮?涓€涓槸綰挎潯鏇茬嚎,涓€涓槸妗嗗浘(bar),榪欐槸涓ょ綆楁硶,鍙互浣跨敤Strategy瀹炵幇.

榪欓噷浠ュ瓧絎︿覆鏇夸唬涓轟緥, 鏈変竴涓枃浠?鎴戜滑闇€瑕佽鍙栧悗,甯屾湜鏇夸唬鍏朵腑鐩稿簲鐨勫彉閲?鐒跺悗杈撳嚭.鍏充簬鏇夸唬鍏朵腑鍙橀噺鐨勬柟娉曞彲鑳芥湁澶氱鏂規硶,榪欏彇鍐充簬鐢ㄦ埛鐨勮姹?鎵€浠ユ垜浠鍑嗗鍑犲鍙橀噺瀛楃鏇夸唬鏂規.

棣栧厛,鎴戜滑寤虹珛涓€涓娊璞$被RepTempRule 瀹氫箟涓€浜涘叕鐢ㄥ彉閲忓拰鏂規硶:

public abstract class RepTempRule{

protected String oldString="";
public void setOldString(String oldString){
銆€銆€this.oldString=oldString;
}

protected String newString="";
public String getNewString(){
銆€銆€return newString;
}



public abstract void replace() throws Exception;


}

鍦≧epTempRule涓?鏈変竴涓娊璞℃柟娉昦bstract闇€瑕佺戶鎵挎槑紜?榪欎釜replace閲屽叾瀹炴槸鏇夸唬鐨勫叿浣撴柟娉?
鎴戜滑鐜板湪鏈変袱涓瓧絎︽浛浠f柟妗?
1.灝嗘枃鏈腑aaa鏇夸唬鎴恇bb;
2.灝嗘枃鏈腑aaa鏇夸唬鎴恈cc;

瀵瑰簲鐨勭被鍒嗗埆鏄疪epTempRuleOne RepTempRuleTwo

public class RepTempRuleOne extends RepTempRule{


public void replace() throws Exception{

銆€銆€//replaceFirst鏄痡dk1.4鏂扮壒鎬?
銆€銆€newString=oldString.replaceFirst("aaa", "bbbb")
銆€銆€System.out.println("this is replace one");
銆€銆€
}


}

public class RepTempRuleTwo extends RepTempRule{


public void replace() throws Exception{

銆€銆€newString=oldString.replaceFirst("aaa", "ccc")
銆€銆€System.out.println("this is replace Two");
銆€銆€
}


}

絎簩姝ワ細鎴戜滑瑕佸緩绔嬩竴涓畻娉曡В鍐崇被錛岀敤鏉ユ彁渚涘鎴風鍙互鑷敱閫夋嫨綆楁硶銆?/p>
public class RepTempRuleSolve {

銆€銆€private RepTempRule strategy;

銆€銆€public RepTempRuleSolve(RepTempRule rule){
銆€銆€銆€銆€this.strategy=rule;
銆€銆€}

銆€銆€public String getNewContext(Site site,String oldString) {
銆€銆€銆€銆€return strategy.replace(site,oldString);
銆€銆€}

銆€銆€public void changeAlgorithm(RepTempRule newAlgorithm) {
銆€銆€銆€銆€strategy = newAlgorithm;
銆€銆€}

}

璋冪敤濡備笅:

public class test{

......

銆€銆€public void testReplace(){

銆€銆€//浣跨敤絎竴濂楁浛浠f柟妗?br />銆€銆€RepTempRuleSolve solver=new RepTempRuleSolve(new RepTempRuleSimple());
銆€銆€solver.getNewContext(site,context);

銆€銆€//浣跨敤絎簩濂?/p>

銆€銆€solver=new RepTempRuleSolve(new RepTempRuleTwo());
銆€銆€solver.getNewContext(site,context);

銆€銆€}

.....

}

鎴戜滑杈懼埌浜嗗湪榪愯鏈熼棿錛屽彲浠ヨ嚜鐢卞垏鎹㈢畻娉曠殑鐩殑銆?/p>

瀹為檯鏁翠釜Strategy鐨勬牳蹇冮儴鍒嗗氨鏄娊璞$被鐨勪嬌鐢?浣跨敤Strategy妯″紡鍙互鍦ㄧ敤鎴烽渶瑕佸彉鍖栨椂,淇敼閲忓緢灝?鑰屼笖蹇€?

Strategy鍜孎actory鏈変竴瀹氱殑綾諱技,Strategy鐩稿綆€鍗曞鏄撶悊瑙?騫朵笖鍙互鍦ㄨ繍琛屾椂鍒昏嚜鐢卞垏鎹€€侳actory閲嶇偣鏄敤鏉ュ垱寤哄璞°€?/p>

Strategy閫傚悎涓嬪垪鍦哄悎:

1.浠ヤ笉鍚岀殑鏍煎紡淇濆瓨鏂囦歡;

2.浠ヤ笉鍚岀殑綆楁硶鍘嬬緝鏂囦歡;

3.浠ヤ笉鍚岀殑綆楁硶鎴幏鍥捐薄;

4.浠ヤ笉鍚岀殑鏍煎紡杈撳嚭鍚屾牱鏁版嵁鐨勫浘褰?姣斿鏇茬嚎 鎴栨鍥綽ar絳?/p>

]]>
綆€鍗曞伐鍘傦紝Factory Method錛堝伐鍘傛柟娉曪級鍜孉bstract Factory錛堟娊璞″伐鍘傦級妯″紡http://www.aygfsteel.com/renyangok/articles/58714.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Tue, 18 Jul 2006 03:20:00 GMThttp://www.aygfsteel.com/renyangok/articles/58714.htmlhttp://www.aygfsteel.com/renyangok/comments/58714.htmlhttp://www.aygfsteel.com/renyangok/articles/58714.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/58714.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/58714.html瀵逛簬綆€鍗曞伐鍘傛潵璇?/strong>錛屽畠鐨勫伐鍘傚彧鑳芥槸榪欎釜鏍峰瓙鐨?br /> public class SimplyFactory {
/**
* 闈欐€佸伐鍘傛柟娉?br /> */
public static Prouct factory(String which) throw NoSuchProductExcption
{

   if(which.equalIgnoreCase("product1"))
   {
   return new Product1();
    }
   else if(which.equalsIgnoreCase("product2"))
      {   
        return new Product2(); 
       }
  else if(which.equalsIgnoreCase("product3"))
      {   
        return new Product3(); 
       }
  else throw  NoSuchProductExcption("NoSuchProduct");
   }
  }
}

鑰屽浜у搧Product1,Product2,Product3,鍙互鎵ц鎺ュ彛Product,涔熷彲浠ヤ笉鎵ц鎺ュ彛Product錛堝綋鐒惰繖鏍蜂笉濂斤級錛岃繖涓狿roduct鎺ュ彛鍙槸鐢ㄦ潵鎶借薄鍏蜂綋product鐢ㄧ殑

public interface Product
{
  void productMethod1();     //榪欎簺鍙槸
  void productMethod2();
  void productMethod3();
}
瀵瑰伐鍘傛潵璇達紝鍙鏈夎繖涔堜竴縐嶄駭鍝侊紝涓€鑸潵璇村氨瑕佸湪宸ュ巶閲屾湁瀹冪殑鐢熶駭鐨勬柟娉曪紝 鍚﹀垯鎶涘嚭寮傚父錛岃€岃宸ュ巶鐢熶駭鐨勮瘽錛屼篃蹇呴』涓嬭揪鐢熶駭浠€涔堜駭鍝佺殑鍛戒護錛岃嚦灝戣鍚戝伐鍘傚彂鍑轟俊鍙鳳紝璁╁伐鍘傝凍浠ュ尯鍒嗘槸瑕佺敓浜т粈涔堜駭鍝侊紝鍚﹀垯宸ュ巶鏄笉鐭ラ亾鐢熶駭鍝竴縐嶄駭鍝侊紝
瀵逛簬綆€鍗曞伐鍘傛潵璇達紝灝辨槸闇€瑕佸湪宸ュ巶涓灇涓炬墍鏈夌殑浜у搧錛屾墍浠ヨ綆€鍗曞伐鍘傝繕鏄潪甯哥鐨勩€?nbsp;


if(which.equalIgnoreCase("product1"))     鍙槸鐢ㄦ潵鍖哄垎鐢熶駭浠€涔堜駭鍝佺殑鏍囪鍊鹼紝錛堜篃鍙互鏍規嵁浜у搧鍏跺畠灞炴€ф潵鍒ゆ柇錛屾瘮濡備駭鍝佺被鍨嬶紝浜у搧澶у皬錛屾€諱箣鑳藉鍖哄垎鏄粈涔堜駭鍝佺殑灞炴€э紝鎴栬€呮槸涓庝駭鍝佸睘鎬х浉鍏崇殑鍙橀噺錛?nbsp;   鎴栬€呰鏍囪鍊兼槸A,鐢熶駭A浜у搧錛屾垨鑰呭伐鍘傞噷瀹氫箟涓嶆槸榪欐牱鐨勶紝鎴戝亸鍋忚鐢熶駭B浜у搧錛屾垨鑰呭啀鐗規畩涓€浜涳紝鎴戝亸鍋忚鐢熶駭A浜у搧+B浜у搧錛岄偅涔堝氨瑕乺eturn new ProductA()+new ProductB()浜嗐€?br />
榪欐牱錛屾垜浠氨鍙互鐪嬪嚭涓€涓棶棰樻潵錛屽鏋滆鑳藉琚畝鍗曞伐鍘傜敓浜у嚭鏉ワ紝灝卞繀欏誨湪綆€鍗曞伐鍘備腑鏈夎兘澶熺敓浜у嚭鐨勫畠鐨勬柟娉曞畾涔夛紝褰撶劧榪橀渶瑕佹湁榪欎釜鍏蜂綋浜у搧綾葷殑瀹氫箟錛屽氨鏄湁class瀵瑰簲錛岃繖鏍風‘淇濆湪綆€鍗曞伐鍘備腑new 瀹冪殑鏃跺€欎笉浼氭姏鍑?nbsp;NoSuchProduct鐨凟xception.


瀵逛簬宸ュ巶鏂規硶鏉ヨ
瀹炶川涓婂畠鏄宸ュ巶瀹炵幇浜嗘娊璞$殑宸ュ巶鎺ュ彛錛屽畠鎶婂叿浣撴€庝箞鐢熶駭涓€縐嶄笢瑗匡紝鏀懼湪鍏蜂綋鐨勫伐鍘傚幓瀹炵幇浜嗭紝鎵€璋?#8221;寤惰繜鍒板瓙綾諱腑瀹炵幇“
public interface Creator
{
  public Prouct factory();
}

public SubCreator1 implent Creator
{
   public Prouct factory()
  {
   return new ConcreteProduct1();
   }
}

public SubCreator2 implent Creator
{
   public Prouct factory()
  {
    return new ConcreteProduct2();
   }
}
璇鋒敞鎰忥細榪斿洖綾誨瀷鏄疨roduct鍨嬬殑錛侊紒
榪欐牱瀹㈡埛绔皟鐢ㄦ槸鐩存帴new 涓€涓叿浣撳伐鍘傜殑瀹炰緥錛岀劧鍚庡懡浠ゅ畠鍘葷敓浜э紝鑰屽浜庡叿浣撳伐鍘傜殑鐖剁被錛堟棦宸ュ巶鎺ュ彛錛屾帴鍙e畬鍏ㄥ彲浠ユ敼鎴愬瓙綾葷戶鎵跨埗綾繪潵瀹炵幇錛屽彧鏄繖鏍蜂笉濂斤紝涓嶇鍚圤O鐨勫師鍒欙級錛屽畠瀹屽叏涓嶇煡閬撲粈涔堜駭鍝佽鐢熶駭浜嗭紝鐢氳嚦瀹冭繛閭d釜鍏蜂綋宸ュ巶琚疄渚嬪寲瀹冮兘涓嶇煡閬?br />
鎶借薄宸ュ巶妯″紡 
鎶借薄宸ュ巶妯″紡鎰忓浘鏄?#8220;鎻愪緵涓€涓垱寤轟竴緋誨垪鐩稿叧鎴栫浉浜掍緷璧栧璞$殑鎺ュ彛錛岃€屾棤闇€鎸囧畾浠栦滑鍏蜂綋鐨勭被”鎴栦負鐗瑰畾鐨勫鎴鳳紙鎴栨儏鍐碉級鎻愪緵鐗瑰畾緋誨垪鐨勫璞°€?br />
public interface Creator
{
   public ProuctA factoryA();
   public ProuctB factoryB();
}

public interface ProductA     //ProductA 鎺ュ彛
{
}

public interface ProductB     //ProductB 鎺ュ彛
{
}


public class ConCreator1 implent Creator
{
   public ProuctA factoryA()
  {
   return new ConcreteProductA1();
   }

   public ProuctB factoryB()
  {
   return new ConcreteProductB1();
   }
}

public class ConCreator2 implent Creator
{
   public ProuctA factoryA()
   {
    return new ProductA2();
    }

   public ProuctB factoryB()
   {
   return new ProductB2();
    }
}


public class ProductA1 implements  ProductA
{
 public  ProductA1()
   {
    }
}
public class ProductA2 implements  ProductA
{
 public  ProductA2()
   {
    }
}
public class ProductB1 implements  ProductB
{
 public  ProductB1()
   {
    }
}
public class ProductB2 implements  ProductB
{
 public  ProductB2()
   {
    }
}
瀹為檯涓婃槸榪欐牱鐨?br /> 1錛屼袱涓伐鍘傜被ConCreator1錛孋onCreator2閮藉疄鐜頒簡Creator鎺ュ彛
2錛孭rouctA1,ProductA2閮藉疄鐜頒簡ProductA鎺ュ彛
3錛孭rouctB1,ProductB2閮藉疄鐜頒簡ProductB鎺ュ彛
4錛孋onCreator1璐熻矗鐢熶駭ProductA綾誨瀷鐨勪駭鍝?鍖呮嫭ProductA1錛孭roductB1)
5錛孋onCreator2璐熻矗鐢熶駭ProductB綾誨瀷鐨勪駭鍝?鍖呮嫭ProductA2錛孭roductB2)
6錛屽伐鍘傛柟娉曚篃鏈夎繖鏍風殑鐗瑰緛錛屼篃灝辨槸璇碈reator涓嶇煡閬撲粈涔堣鐢熶駭鍑烘潵錛岀敋鑷充笉鐭ラ亾ConCreator1榪樻槸ConCreator2琚疄渚嬪寲浜嗭紝鍥犱負client楂樺叴璋冮偅涓€涓伐鍘傦紝灝辮皟閭d竴涓伐鍘傦紝灝辨槸璇村伐鍘傝兘鐢熶駭浠€涔堬紝瀵瑰鎴風鏄彲瑙佺殑銆傜敋鑷寵繕鏈変竴縐嶆儏鍐碉紝瀹㈡埛绔珮鍏磋搗鏉ュ氨鐢熶駭浜哖roductA1,鎴戝氨涓嶇敓浜roductA2,鍥犱負涓婇潰鐨勪緥瀛愪腑瀹冧滑榪橀兘鏄澗鏁g殑錛屾病鏈夌粦瀹氬湪涓€璧?br />



浜庢槸鎻愬嚭鍙﹀涓€涓緥瀛愶紝涔熸槸鑰佹彁璧風殑鐢佃剳綾誨瀷鐨勪緥瀛?br />
1錛岀數鑴戠敓浜у晢鏄帴鍙o紝
2錛孋UP鏄帴鍙o紝
3錛岀‖鐩樻槸鎺ュ彛錛?br /> 4錛孖BM宸ュ巶鏄埗閫營BM鍝佺墝鐨勭數鑴戠殑宸ュ巶
5錛孌ELL宸ュ巶鏄埗閫燚Ell鍝佺墝鐨勭數鑴戠殑宸ュ巶
涓鴻璁烘柟渚匡紝灝辮涓虹數鑴?CUP+紜洏錛?br /> 6錛屾墍浠ュ憖CUP鏈塈BM鐨凜PU鍜孌ELL鐨凜PU
7錛屽悓鏍風‖鐩樹篃鏄湁IBM鐨勭‖鐩樺拰DELL鐨勭‖鐩?br /> 8錛孖BM宸ュ巶鐢熶駭IBM鐨凜PU鍜孖BM鐨勭‖鐩橈紝緇濆涓嶇敓浜ELL鐨凜PU錛屼篃涓嶇敓浜ELL鐨勭‖鐩?br /> 9錛屽悓鏍稤ELL宸ュ巶涔熸槸涓€鏍?br />

public interface  鐢佃剳鐢熶駭鍟?br /> {
   public CPU 鍒墮€燙PU();
   public 紜洏 鍒墮€犵‖鐩?);
}

public interface CPU
{
}

public interface 紜洏
{
}

public class IBM鐨凜PU implements CPU
{
   public     IBM鐨凜PU();
}

public class IBM鐨勭‖鐩?nbsp;implements 紜洏
{
    public     IBM鐨勭‖鐩?);
}

public class DELL鐨凜PU implements CPU
{
    public     DELL鐨凜PU();
}

public class DELL鐨勭‖鐩?nbsp;implements 紜洏
{
   public     DELL鐨勭‖鐩?);
}

//涓嬮潰鏄疘BM宸ュ巶
public class  IBM宸ュ巶
{
   private CPU IBM鐨凜PU縐佹湁鍙橀噺=null;
   private 紜洏 IBM鐨勭‖鐩樼鏈夊彉閲?null;
   private CPU 鍒墮€營BMCPU()
  {
     return  new IBM鐨凜PU();
   }
 private 紜洏 鍒墮€營BM紜洏()
 {
    return  new IBM鐨凜PU();
  }
 public 鐢佃剳  鍒墮€營BM鐢佃剳()
 {
  try{
  IBM鐨凜PU縐佹湁鍙橀噺=鍒墮€營BMCPU();
  IBM鐨勭‖鐩樼鏈夊彉閲?鍒墮€營BM紜洏();
  if(IBM鐨凜PU縐佹湁鍙橀噺!=null&&IBM鐨勭‖鐩樼鏈夊彉閲?=null)
  retrun  (IBM鐨凜PU縐佹湁鍙橀噺+IBM鐨勭‖鐩樼鏈夊彉閲?;
    //緇勮鎴怚BM鐢佃剳
   }
  catch(Exception e)
    {
    System.out.println("鍒墮€營BM鐢佃剳澶辮觸錛?);
     }
   }
  }
}

榪欐牱錛屽鎴風鏃犳硶閫氳繃鍛戒護鍗曠敓浜у嚭涓€涓狢PU鏉ワ紝榪欐牱鎶借薄鎵嶇湡姝f垚涓轟竴涓畬鏁翠駭鍝佺殑宸ュ巶錛屽彧瑕佸悜宸ュ巶鍙戝嚭鐢熶駭鐨勫懡浠わ紝涓€鍙板畬鏁寸殑鐢佃剳灝辯敓浜у嚭鏉ヤ簡錛岃€屽伐鍘傛€庝箞鐢熶駭鐨勶紝鐢熶駭浜嗗摢浜涢儴浠訛紝澶栫晫灝辯湅涓嶈浜嗭紝澶栫晫灝辯煡閬撹繖涓伐鍘傛槸鐢熶駭IBM鐢佃剳鏁存満鐨勫伐鍘傦紒

DELL鐢佃剳宸ュ巶涓€鏍?

----------------------------鎬葷粨----------------------------

宸ュ巶鏂規硶妯″紡錛?br /> 涓€涓娊璞′駭鍝佺被錛屽彲浠ユ淳鐢熷嚭澶氫釜鍏蜂綋浜у搧綾匯€?nbsp; 
涓€涓娊璞″伐鍘傜被錛屽彲浠ユ淳鐢熷嚭澶氫釜鍏蜂綋宸ュ巶綾匯€?nbsp; 
姣忎釜鍏蜂綋宸ュ巶綾誨彧鑳藉垱寤轟竴涓叿浣撲駭鍝佺被鐨勫疄渚嬨€?br />
鎶借薄宸ュ巶妯″紡錛?br /> 澶氫釜鎶借薄浜у搧綾伙紝姣忎釜鎶借薄浜у搧綾誨彲浠ユ淳鐢熷嚭澶氫釜鍏蜂綋浜у搧綾匯€?nbsp; 
涓€涓娊璞″伐鍘傜被錛屽彲浠ユ淳鐢熷嚭澶氫釜鍏蜂綋宸ュ巶綾匯€?nbsp; 
姣忎釜鍏蜂綋宸ュ巶綾誨彲浠ュ垱寤哄涓叿浣撲駭鍝佺被鐨勫疄渚嬨€?nbsp; 
   
鍖哄埆錛?br /> 宸ュ巶鏂規硶妯″紡鍙湁涓€涓娊璞′駭鍝佺被錛岃€屾娊璞″伐鍘傛ā寮忔湁澶氫釜銆?nbsp; 
宸ュ巶鏂規硶妯″紡鐨勫叿浣撳伐鍘傜被鍙兘鍒涘緩涓€涓叿浣撲駭鍝佺被鐨勫疄渚嬶紝鑰屾娊璞″伐鍘傛ā寮忓彲浠ュ垱寤哄涓€?/font>

]]>
Singleton錛堝崟鎬侊級妯″紡 鍜?Double-Checked Locking錛堝弻閲嶆鏌ラ攣瀹氾級妯″紡http://www.aygfsteel.com/renyangok/articles/58706.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Tue, 18 Jul 2006 02:56:00 GMThttp://www.aygfsteel.com/renyangok/articles/58706.htmlhttp://www.aygfsteel.com/renyangok/comments/58706.htmlhttp://www.aygfsteel.com/renyangok/articles/58706.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/58706.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/58706.html
Singleton錛氱敤浜庡崟綰跨▼搴旂敤紼嬪簭

public class Singleton {

銆€銆€private Singleton(){}

銆€銆€//鍦ㄨ嚜宸卞唴閮ㄥ畾涔夎嚜宸變竴涓疄渚嬶紝鏄笉鏄緢濂囨€紵
銆€銆€//娉ㄦ剰榪欐槸private 鍙緵鍐呴儴璋冪敤

銆€銆€private static Singleton instance = new Singleton();

銆€銆€//榪欓噷鎻愪緵浜嗕竴涓緵澶栭儴璁塊棶鏈琧lass鐨勯潤鎬佹柟娉曪紝鍙互鐩存帴璁塊棶銆€銆€
銆€銆€public static Singleton getInstance() {
銆€銆€銆€銆€return instance; 銆€銆€
銆€銆€ }
}

Double-Checked Locking錛氱敤浜庡綰跨▼搴旂敤紼嬪簭

public class Singleton {

銆€銆€private static Singleton instance = null;

銆€銆€public static synchronized Singleton getInstance() {

銆€銆€//榪欎釜鏂規硶姣斾笂闈㈡湁鎵€鏀硅繘錛屼笉鐢ㄦ瘡嬈¢兘榪涜鐢熸垚瀵硅薄錛屽彧鏄涓€嬈°€€銆€銆€ 銆€
銆€銆€//浣跨敤鏃剁敓鎴愬疄渚嬶紝鎻愰珮浜嗘晥鐜囷紒
銆€銆€if (instance==null)
銆€銆€銆€銆€instance錛漬ew Singleton();
銆€銆€return instance; 銆€銆€}

}



Double-Checked Locking鍦╦ava瀹為檯涓婃棤娉曞緱鍒伴鏈熺殑鏁堟灉錛屼笉鑳戒繚璇乻ingleton鐨勬紜€с€?br />鍙傝€冿細錛堣嫳鏂囷級
http://gee.cs.oswego.edu/dl/cpj/jmm.html
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html



]]>
璁捐妯″紡鐨勫師鍒欏拰絳栫暐http://www.aygfsteel.com/renyangok/articles/58695.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Tue, 18 Jul 2006 02:08:00 GMThttp://www.aygfsteel.com/renyangok/articles/58695.htmlhttp://www.aygfsteel.com/renyangok/comments/58695.htmlhttp://www.aygfsteel.com/renyangok/articles/58695.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/58695.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/58695.html2銆佷紭鍏堜嬌鐢ㄥ璞$粍鍚堬紝鑰屼笉鏄被緇ф壙銆?br />3銆侀拡瀵規帴鍙h璁¤€屼笉鏄疄鐜拌璁°€?br />4銆佸紑鏀撅紞灝侀棴鍘熷垯錛氭ā鍧椼€佺被鍜屾柟娉曞簲璇ュ鎵╁睍鏄紑鏀劇殑錛岃€屽鏇存敼鏄皝闂殑銆?img src ="http://www.aygfsteel.com/renyangok/aggbug/58695.html" width = "1" height = "1" />

]]>
Adapter錛堥€傞厤鍣級妯″紡http://www.aygfsteel.com/renyangok/articles/57591.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Tue, 11 Jul 2006 00:52:00 GMThttp://www.aygfsteel.com/renyangok/articles/57591.htmlhttp://www.aygfsteel.com/renyangok/comments/57591.htmlhttp://www.aygfsteel.com/renyangok/articles/57591.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/57591.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/57591.html

聽 聽 鏈夋椂錛屼負澶嶇敤鑰岃璁$殑宸ュ叿綆辯被涓嶈兘澶熻澶嶇敤鐨勫師鍥犱粎浠呮槸鍥犱負瀹冪殑鎺ュ彛涓庝笓涓氬簲鐢ㄩ鍩熸墍闇€瑕佺殑鎺ュ彛涓嶅尮閰嶃€?br />聽聽聽 閫傞厤鍣ㄦā寮忔湁綾葷殑閫傞厤鍣ㄦā寮忓拰瀵硅薄鐨勯€傞厤鍣ㄦā寮忎袱縐嶄笉鍚岀殑褰㈠紡銆?br />
涓€銆佺被鐨勯€傞厤鍣ㄦā寮?br />聽聽聽 綾葷殑閫傞厤鍣ㄦā寮忔妸閫傞厤鐨勭被鐨凙PI杞崲鎴愮洰鏍囩被鐨凙PI銆?br />


  • 鐩爣(Target)瑙掕壊錛氳繖灝辨槸鎵€鏈熷緟寰楀埌鐨勬帴鍙c€?
  • 婧?Adaptee)瑙掕壊錛氱幇鏈夐渶瑕侀€傞厤鐨勬帴鍙c€?
  • 閫傞厤鍣?Adapter)瑙掕壊錛氶€傞厤鍣ㄧ被鏄湰妯″紡鐨勬牳蹇冦€傞€傞厤鍣ㄦ妸婧愭帴鍙h漿鎹㈡垚鐩爣鎺ュ彛銆傛樉鐒惰繖涓€瑙掕壊涓嶅彲浠ユ槸鎺ュ彛錛岃€屽繀欏繪槸鍏蜂綋綾匯€?/li>
/**
聽* 瀹氫箟Client浣跨敤鐨勪笌鐗瑰畾棰嗗煙鐩稿叧鐨勬帴鍙?br />聽*/
public interface Target {
聽聽聽 void sampleOperation1();
聽聽聽 void sampleOperation2();
}
/**
聽* 瀹氫箟涓€涓凡緇忓瓨鍦ㄧ殑鎺ュ彛錛岃繖涓帴鍙i渶瑕侀€傞厤
聽*/
public class Adaptee {
聽聽聽 public void sampleOperation1() {
聽聽聽 聽聽聽 // ......
聽聽聽 }
}
/**
聽* 瀵笰daptee涓嶵arget鎺ュ彛榪涜閫傞厤
聽*/
public class Adapter extends Adaptee implements Target {
聽聽聽 public void sampleOperation2() {
聽聽聽 聽聽聽 // ......
聽聽聽 }
}

浜屻€佸璞¢€傞厤鍣?br />聽聽聽 涓庣被鐨勯€傞厤鍣ㄦā寮忎竴鏍鳳紝瀵硅薄閫傞厤鍣ㄦā寮忔妸閫傞厤鐨勭被鐨凙PI杞崲鎴愪負鐩爣綾葷殑API錛屼笌綾葷殑閫傞厤鍣ㄦā寮忎笉鍚岀殑鏄紝瀵硅薄鐨勯€傞厤鍣ㄦā寮忎笉鏄嬌鐢ㄧ戶鎵垮叧緋昏繛鎺ュ埌Adaptee綾伙紝鑰屾槸浣跨敤濮旀淳鍏崇郴榪炴帴鍒癆daptee綾匯€傜ず鎰忎唬鐮佸涓嬶細
/**
聽* 瀹氫箟Client浣跨敤鐨勪笌鐗瑰畾棰嗗煙鐩稿叧鐨勬帴鍙?br />聽*/
public interface Target {
聽聽聽 void sampleOperation1();
聽聽聽 void sampleOperation2();
}
/**
聽* 瀹氫箟涓€涓凡緇忓瓨鍦ㄧ殑鎺ュ彛錛岃繖涓帴鍙i渶瑕侀€傞厤
聽*/
public class Adaptee {
聽聽聽 public void sampleOperation1() {
聽聽聽 聽聽聽 // ......
聽聽聽 }
}
/**
聽* 瀵笰daptee涓嶵arget鎺ュ彛榪涜閫傞厤
聽*/
public class Adapter implements Target {
聽聽聽 private Adaptee adaptee;
聽聽聽 public Adapter(Adaptee adaptee) {
聽聽聽 聽聽聽 super();
聽聽聽 聽聽聽 this.adaptee = adaptee;
聽聽聽 }
聽聽聽 public void sampleOperation1() {
聽聽聽 聽聽聽 adaptee.sampleOperation1();
聽聽聽 }
聽聽聽 public void sampleOperation2() {
聽聽聽 聽聽聽 // ......
聽聽聽 }
}

綾婚€傞厤鍣ㄦā寮忓拰瀵硅薄閫傞厤鍣ㄦā寮忕殑寮傚悓錛歍arget鎺ュ彛鍜孉daptee綾婚兘鐩稿悓錛屼笉鍚岀殑鏄被閫傞厤鍣ㄧ殑Adapter緇ф壙Adaptee瀹炵幇Target錛屽璞¢€傞厤鍣ㄧ殑Adapter瀹炵幇Target鑱氶泦Adaptee銆?br />
閫傞厤鍣ㄦā寮忕殑鐢ㄦ剰鏄皢鎺ュ彛涓嶅悓鑰屽姛鑳界浉鍚屾垨鑰呯浉榪戠殑涓や釜鎺ュ彛鍔犱互杞崲銆?br />
涓夈€丣DBC椹卞姩杞歡涓庨€傞厤鍣ㄦā寮?br />聽聽聽 JDBC緇欏嚭涓€涓鎴風閫氱敤鐨勭晫闈紝姣忎釜鏁版嵁搴撳紩鎿庣殑JDBC椹卞姩杞歡閮芥槸涓€涓粙浜嶫DBC鎺ュ彛鍜屾暟鎹簱寮曟搸鎺ュ彛涔嬮棿鐨勯€傞厤鍣ㄨ蔣浠躲€?br />

]]>
Facade錛堝瑙傦級妯″紡http://www.aygfsteel.com/renyangok/articles/57541.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Mon, 10 Jul 2006 09:15:00 GMThttp://www.aygfsteel.com/renyangok/articles/57541.htmlhttp://www.aygfsteel.com/renyangok/comments/57541.htmlhttp://www.aygfsteel.com/renyangok/articles/57541.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/57541.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/57541.html Facade妯″紡鐨勫畾涔?/em>
: 涓哄瓙緋葷粺涓殑涓€緇勬帴鍙f彁渚涗竴涓竴鑷寸殑鐣岄潰.銆傜幇鍦ㄧ殑杞歡閮芥槸鎸夌収妯″潡榪涜鍒掑垎錛屽涓嶅悓鐨勬ā鍧楀垎鍒繘琛岀紪紼嬨€備絾鏄湪鐢ㄦ埛寮€鏉ワ紝涓嶅悓鐨勬ā鍧楀簲璇ュ叿鏈夌粺涓€鐨勬帴鍙o紝鎹㈠彞璇濊錛屾垜浠簲璇ュ彲浠ラ€氳繃緇熶竴鐨勬帴鍙h闂郴緇熶腑鐨勬墍鏈夊姛鑳姐€?

聽聽聽 鏈変竴涓緢鍏稿瀷鐨勪緥瀛愬氨鏄紪璇戠郴緇熴€傞€氬父鎴戜滑灝嗙紪璇戠郴緇熷垎瑙d負錛欳ompile鍜孡ink涓や釜姝ラ銆備竴涓狢ompile鍙堝彲浠ュ垎瑙d負璇嶆硶鍒嗘瀽銆佽娉曞垎鏋愩€佽涔夊垎鏋愩€佷腑闂翠唬鐮佺敓鎴愮瓑絳夋楠ゃ€傚浜庣敤鎴鋒潵璁詫紝鎴戜滑涓嶅彲鑳藉皢榪欎簺妯″潡鍒嗗埆鎻愪緵緇欎粬浠紝璁╀粬浠緷嬈¤皟鐢ㄣ€傜浉鍙嶇殑錛屾垜浠簲璇ユ彁渚涗竴涓粺涓€鐨勬帴鍙o紝浣垮緱鐢ㄦ埛鍙互鏂逛究鐨勪嬌鐢ㄥ悇涓姛鑳斤紝渚嬪IDE 銆?/p>

Facade涓€涓吀鍨嬪簲鐢ㄥ氨鏄暟鎹簱JDBC鐨勫簲鐢?濡備笅渚嬪鏁版嵁搴撶殑鎿嶄綔:

public class DBCompare {

銆€銆€Connection conn = null;
銆€銆€PreparedStatement prep = null;
銆€銆€ResultSet rset = null;
銆€銆€try {
銆€銆€ 銆€銆€Class.forName( "<driver>" ).newInstance();
銆€銆€銆€銆€ conn = DriverManager.getConnection( "<database>" );
銆€銆€銆€銆€
銆€銆€銆€銆€ String sql = "SELECT * FROM <table> WHERE <column name> = ?";
銆€銆€銆€銆€ prep = conn.prepareStatement( sql );
銆€銆€銆€銆€ prep.setString( 1, "<column value>" );
銆€銆€銆€銆€ rset = prep.executeQuery();
銆€銆€銆€銆€ if( rset.next() ) {
銆€銆€銆€銆€銆€銆€銆€銆€System.out.println( rset.getString( "<column name" ) );
銆€銆€銆€銆€銆€}
銆€銆€} catch( SException e ) {
銆€銆€銆€銆€ e.printStackTrace();
銆€銆€} finally {
銆€銆€銆€銆€ rset.close();
銆€銆€銆€銆€ prep.close();
銆€銆€銆€銆€ conn.close();
銆€銆€}
}

涓婁緥鏄疛sp涓渶閫氬父鐨勫鏁版嵁搴撴搷浣滃姙娉?

鍦ㄥ簲鐢ㄤ腑,緇忓父闇€瑕佸鏁版嵁搴撴搷浣?姣忔閮藉啓涓婅堪涓€孌典唬鐮佽偗瀹氭瘮杈冮夯鐑?闇€瑕佸皢鍏朵腑涓嶅彉鐨勯儴鍒嗘彁鐐煎嚭鏉?鍋氭垚涓€涓帴鍙?榪欏氨寮曞叆浜唂acade澶栬瀵硅薄.濡傛灉浠ュ悗鎴戜滑鏇存崲Class.forName涓殑<driver>涔熼潪甯告柟渚?姣斿浠嶮ysql鏁版嵁搴撴崲鍒癘racle鏁版嵁搴?鍙鏇存崲facade鎺ュ彛涓殑driver灝卞彲浠?

鎴戜滑鍋氭垚浜嗕竴涓?a target="_blank">Facade鎺ュ彛,浣跨敤璇ユ帴鍙?涓婁緥涓殑紼嬪簭灝卞彲浠ユ洿鏀瑰涓?

public class DBCompare {

銆€ String sql = "SELECT * FROM <table> WHERE <column name> = ?";銆€銆€

銆€銆€try {
銆€銆€ 銆€銆€Mysql msql=new mysql(sql);
銆€銆€銆€銆€ prep.setString( 1, "<column value>" );
銆€銆€銆€銆€ rset = prep.executeQuery();
銆€銆€銆€銆€ if( rset.next() ) {
銆€銆€銆€銆€銆€銆€銆€銆€System.out.println( rset.getString( "<column name" ) );
銆€銆€銆€銆€銆€}
銆€銆€} catch( SException e ) {
銆€銆€銆€銆€ e.printStackTrace();
銆€銆€} finally {
銆€銆€銆€銆€ mysql.close();
銆€銆€銆€銆€ mysql=null;
銆€銆€}
}

鍙闈炲父綆€鍗?鎵€鏈夌▼搴忓鏁版嵁搴撹闂兘鏄嬌鐢ㄦ敼鎺ュ彛,闄嶄綆緋葷粺鐨勫鏉傛€?澧炲姞浜嗙伒媧繪€?

濡傛灉鎴戜滑瑕佷嬌鐢ㄨ繛鎺ユ睜,涔熷彧瑕侀拡瀵筬acade鎺ュ彛淇敼灝卞彲浠?

鐢變笂鍥懼彲浠ョ湅鍑? facade瀹為檯涓婃槸涓悊欏虹郴緇熼棿鍏崇郴,闄嶄綆緋葷粺闂磋€﹀悎搴︾殑涓€涓父鐢ㄧ殑鍔炴硶,涔熻浣犲凡緇忎笉鐭ヤ笉瑙夊湪浣跨敤,灝界涓嶇煡閬撳畠灝辨槸facade.



]]>Bridge錛堟ˉ鎺ワ級妯″紡http://www.aygfsteel.com/renyangok/articles/57540.html淇濆皵浠?/dc:creator>淇濆皵浠?/author>Mon, 10 Jul 2006 09:13:00 GMThttp://www.aygfsteel.com/renyangok/articles/57540.htmlhttp://www.aygfsteel.com/renyangok/comments/57540.htmlhttp://www.aygfsteel.com/renyangok/articles/57540.html#Feedback0http://www.aygfsteel.com/renyangok/comments/commentRss/57540.htmlhttp://www.aygfsteel.com/renyangok/services/trackbacks/57540.htmlBridge妯″紡鐨勭洰鐨勶細鎶婃娊璞¢儴鍒嗗拰琛屼負閮ㄥ垎鍒嗙錛屼嬌瀹冧滑閮借兘鐙珛鍙樺寲銆?br />

浠諱綍浜嬬墿瀵硅薄閮芥湁鎶借薄鍜岃涓轟箣鍒嗭紝渚嬪浜猴紝浜烘槸涓€縐嶆娊璞★紝浜哄垎鐢蜂漢鍜屽コ浜虹瓑錛涗漢鏈夎涓猴紝琛屼負涔熸湁鍚勭鍏蜂綋琛ㄧ幇錛屾墍浠ワ紝鈥滀漢鈥濅笌鈥滀漢鐨勮涓衡€濅袱涓蹇典篃鍙嶆槧浜嗘娊璞″拰琛屼負涔嬪垎銆?/p>

鍦ㄩ潰鍚戝璞¤璁$殑鍩烘湰姒傚康涓紝瀵硅薄榪欎釜姒傚康瀹為檯鏄敱灞炴€у拰琛屼負涓や釜閮ㄥ垎緇勬垚鐨勶紝灞炴€ф垜浠彲浠ヨ涓烘槸涓€縐嶉潤姝㈢殑錛屾槸涓€縐嶆娊璞★紝涓€鑸儏鍐典笅錛岃涓烘槸鍖呭惈鍦ㄤ竴涓璞′腑錛屼絾鏄紝鍦ㄦ湁鐨勬儏鍐典笅錛屾垜浠渶瑕佸皢榪欎簺琛屼負涔熻繘琛屽綊綾伙紝褰㈡垚涓€涓€葷殑琛屼負鎺ュ彛錛岃繖灝辨槸妗ユā寮忕殑鐢ㄥ銆?br />

鎬諱箣錛孊ridge妯″紡灝辨槸浣挎娊璞″拰琛屼負鍒嗙錛屽仛鍒板悇鑷殑鐙珛鍙戝睍錛屽氨鏄鎶借薄鍜岃涓哄悇鎶借薄鍑轟竴涓帴鍙c€傚綋闇€瑕佹墿灞曡涓烘垨鑰呮娊璞¢儴鍒嗘椂錛?br />鍙渶鎵╁睍鐩稿簲閮ㄥ垎錛岃€屼笉鐢ㄤ慨鏀瑰師鏉ョ殑緇撴瀯銆?br />
渚嬪瓙涓€錛?/strong>
鍋囧鏈変釜綾籇rawGraph鏈変釜鐢誨渾鐨勬帴鍙o紝褰撶劧榪欎釜鍦嗗艦鍙互鐢ㄩ搮絎旂敾錛屼篃鍙互鐢ㄩ挗絎旂敾銆傚叾涓敾鍦嗘槸鎶借薄閮ㄥ垎錛?br />鐢ㄩ搮絎旂敾鍜岀敤閽㈢瑪鐢繪槸琛屼負閮ㄥ垎銆傚線寰€榪欎釜鏃跺€欐垜浠垎鍒細瀹氫箟閾呯瑪綾籔encilDraw錛岄挗絎旂被PenDraw鏉ュ疄鐜癉raw錛?br />榪欐牱灝變嬌寰楁娊璞¢儴鍒嗗拰琛屼負閮ㄥ垎鍥哄畾鐨勬崋緇戝湪涓€璧鳳紝鎵╁睍鎬т笉寮恒€傚亣濡傝鍔ㄦ€佸鍔犱竴涓敤綺夌瑪鐢葷殑琛屼負鍜屼竴涓敾鏂瑰艦鐨勬娊璞¢儴鍒嗭紝
灝卞繀鐒惰鏂板畾涔変竴涓狢halkDraw綾伙紝DrawGraph綾婚噷澧炲姞drawRectangle鎺ュ彛銆傛樉鐒惰繖鏄緢楹葷儲鐨勩€?/p>

Bridge妯″紡鏄繖鏍峰疄鐜扮殑錛?br />//鎶借薄閮ㄥ垎
public abstract class DrawGraph {

聽private ToolDraw toolDraw;
聽public void setToolDraw(){
聽聽this.toolDraw = ToolDrawSingleton.getToolDraw();//鐢⊿ingleton妯″紡鏉ラ€夊伐鍏楓€?/strong>
聽}

聽public ToolDraw getToolDraw(){
聽聽return toolDraw;
聽}

聽public abstract void draw();
}

//鐢誨渾
public class DrawCircle implements DrawGraph{
聽public DrawCircle(){
聽聽setToolDraw();
聽}
聽public void draw(){
聽聽System.out.pritnln(toolDraw.getName() + " draw circle");
聽}
}

//鐢葷煩褰?br />public class DrawRectangle implements DrawGraph{
聽public DrawRectangle(){
聽聽setToolDraw();
聽}
聽public void draw(){
聽聽System.out.pritnln(toolDraw.getName() + " draw rectangle");
聽}
}

//琛屼負閮ㄥ垎
public abstract class ToolDraw{
聽public abstract String getName();
聽public abstract void drawWithTool();
}

//閾呯瑪
public class PencilDraw implements ToolDraw{
聽private final static String name = "Pencil";

聽public static String getName(){
聽聽return name;
聽}

聽public void drawWithTool(){
聽聽System.out.println("Draw with Pencil.");
聽}
}

//閽㈢瑪
public class PenDraw implements ToolDraw{
聽private final static String name = "Pen";

聽public String getName(){
聽聽return name;
聽}

聽public void drawWithTool(){
聽聽System.out.println("Draw with Pen.");
聽}
}

//綺夌瑪
public class ChalkDraw implements ToolDraw{
聽private final static String name = "Chalk";

聽public String getName(){
聽聽return name;
聽}

聽public void drawWithTool(){
聽聽System.out.println("Draw with Chalk.");
聽}
}

鍋氫竴涓猄ingleton綾繪潵鑱旂郴鎶借薄鍜岃涓恒€傞€氳繃姝ょ被鏉ラ€夊伐鍏?br />public class ToolDrawSingleton{
聽private static ToolDraw toolDraw;

聽public ToolDrawSingleton(ToolDraw toolDraw){
聽聽this.toolDraw = toolDraw
聽}
聽public static ToolDraw getToolDraw(){
聽聽return toolDraw;
聽}
}

鍋囧鎴戜滑鐜板湪瑕佺敤綺夌瑪鏉ョ敾涓€涓柟褰?涓€涓渾褰細
//鍙栧緱宸ュ叿
ToolDrawSingleton tool = new ToolDrawSingleton(new ChalkDraw());
//鐢繪柟褰?br />DrawGraph drawRectangle = new DrawRectangle();
drawRectangle.draw();
//鐢誨渾褰?br />drawRectangle = new DrawCircle();
drawRectangle.draw();

渚嬪瓙浜岋細
渚嬪,涓€鏉挅鍟′負渚?瀛愮被瀹炵幇綾諱負鍥涗釜錛氫腑鏉姞濂躲€佸ぇ鏉姞濂躲€?涓澂涓嶅姞濂躲€佸ぇ鏉笉鍔犲ザ銆?/p>

浣嗘槸錛屾垜浠敞鎰忓埌錛氫笂闈㈠洓涓瓙綾諱腑鏈夋蹇甸噸鍙狅紝鍙粠鍙﹀涓€涓搴﹁繘琛岃€冭檻錛岃繖鍥涗釜綾誨疄闄呮槸涓や釜瑙掕壊鐨勭粍鍚堬細鎶借薄 鍜岃涓猴紝鍏朵腑鎶借薄涓猴細涓澂鍜屽ぇ鏉紱琛屼負涓猴細鍔犲ザ 涓嶅姞濂訛紙濡傚姞姍欐眮 鍔犺嫻鏋滄眮錛?

瀹炵幇鍥涗釜瀛愮被鍦ㄦ娊璞″拰琛屼負涔嬮棿鍙戠敓浜嗗浐瀹氱殑緇戝畾鍏崇郴錛屽鏋滀互鍚庡姩鎬佸鍔犲姞钁¤悇姹佺殑琛屼負錛屽氨蹇呴』鍐嶅鍔犱袱涓被錛氫腑鏉姞钁¤悇姹佸拰澶ф澂鍔犺憽钀勬眮銆傛樉鐒舵販涔?鎵╁睍鎬ф瀬宸€?/p>

閭f垜浠粠鍒嗙鎶借薄鍜岃涓虹殑瑙掑害錛屼嬌鐢˙ridge妯″紡鏉ュ疄鐜般€?/p>

濡備綍瀹炵幇?
浠ヤ笂闈㈡彁鍒扮殑鍜栧暋 涓轟緥. 鎴戜滑鍘熸潵鎵撶畻鍙璁′竴涓帴鍙?鎶借薄綾?,浣跨敤Bridge妯″紡鍚?鎴戜滑闇€瑕佸皢鎶借薄鍜岃涓哄垎寮€,鍔犲ザ鍜屼笉鍔犲ザ灞炰簬琛屼負,鎴戜滑灝嗗畠浠娊璞℃垚涓€涓笓闂ㄧ殑琛屼負鎺ュ彛.

鍏堢湅鐪嬫娊璞¢儴鍒嗙殑鎺ュ彛浠g爜:

public abstract class Coffee
{
銆€銆€CoffeeImp coffeeImp;

銆€銆€public void setCoffeeImp() {
銆€銆€銆€銆€this.CoffeeImp = CoffeeImpSingleton.getTheCoffeImp();
銆€銆€}

銆€銆€public CoffeeImp getCoffeeImp() {return this.CoffeeImp;}

銆€銆€public abstract void pourCoffee();
}

鍏朵腑CoffeeImp 鏄姞涓嶅姞濂剁殑琛屼負鎺ュ彛,鐪嬪叾浠g爜濡備笅:

public abstract class CoffeeImp
{
銆€銆€public abstract void pourCoffeeImp();
}

鐜板湪鎴戜滑鏈変簡涓や釜鎶借薄綾?涓嬮潰鎴戜滑鍒嗗埆瀵瑰叾榪涜緇ф壙,瀹炵幇concrete class:

//涓澂
public class MediumCoffee extends Coffee
{
銆€銆€public MediumCoffee() {setCoffeeImp();}

銆€銆€public void pourCoffee()
銆€銆€{
銆€銆€銆€銆€CoffeeImp coffeeImp = this.getCoffeeImp();
銆€銆€銆€銆€//鎴戜滑浠ラ噸澶嶆鏁版潵璇存槑鏄啿涓澂榪樻槸澶ф澂 ,閲嶅2嬈℃槸涓澂
銆€銆€銆€銆€for (int i = 0; i < 2; i++)
銆€銆€銆€銆€{

銆€銆€銆€銆€銆€銆€coffeeImp.pourCoffeeImp();
銆€銆€銆€銆€}
銆€銆€
銆€銆€}
}

//澶ф澂
public class SuperSizeCoffee extends Coffee
{
銆€銆€public SuperSizeCoffee() {setCoffeeImp();}

銆€銆€public void pourCoffee()
銆€銆€{
銆€銆€銆€銆€CoffeeImp coffeeImp = this.getCoffeeImp();
銆€銆€銆€銆€//鎴戜滑浠ラ噸澶嶆鏁版潵璇存槑鏄啿涓澂榪樻槸澶ф澂 ,閲嶅5嬈℃槸澶ф澂
銆€銆€銆€銆€for (int i = 0; i < 5; i++)
銆€銆€銆€銆€{

銆€銆€銆€銆€銆€銆€coffeeImp.pourCoffeeImp();
銆€銆€銆€銆€}
銆€銆€
銆€銆€}
}

涓婇潰鍒嗗埆鏄腑鏉拰澶ф澂鐨勫叿浣撳疄鐜?涓嬮潰鍐嶅琛屼負CoffeeImp榪涜緇ф壙:

//鍔犲ザ
public class MilkCoffeeImp extends CoffeeImp
{
銆€銆€MilkCoffeeImp() {}

銆€銆€public void pourCoffeeImp()
銆€銆€{
銆€銆€銆€銆€System.out.println("鍔犱簡緹庡懗鐨勭墰濂?);
銆€銆€}
}

//涓嶅姞濂?br />public class FragrantCoffeeImp extends CoffeeImp
{
銆€銆€FragrantCoffeeImp() {}

銆€銆€public void pourCoffeeImp()
銆€銆€{
銆€銆€銆€銆€System.out.println("浠€涔堜篃娌″姞,娓呴");
銆€銆€}
}

Bridge妯″紡鐨勫熀鏈鏋舵垜浠凡緇忔惌濂戒簡,鍒繕璁板畾涔変腑榪樻湁涓€鍙?鍔ㄦ€佺粨鍚?鎴戜滑鐜板湪鍙互鍠濆埌鑷沖皯鍥涚鍜栧暋:
1.涓澂鍔犲ザ
2.涓澂涓嶅姞濂?br />3.澶ф澂鍔犲ザ
4.澶ф澂涓嶅姞濂?/p>

鐪嬬湅鏄浣曞姩鎬佺粨鍚堢殑,鍦ㄤ嬌鐢ㄤ箣鍓?鎴戜滑鍋氫釜鍑嗗宸ヤ綔,璁捐涓€涓崟鎬佺被(Singleton)鐢ㄦ潵hold褰撳墠鐨凜offeeImp:

public class CoffeeImpSingleton
{
銆€銆€private static CoffeeImp coffeeImp;

銆€銆€public CoffeeImpSingleton(CoffeeImp coffeeImpIn)
銆€銆€ {this.coffeeImp = coffeeImpIn;}

銆€銆€public static CoffeeImp getTheCoffeeImp()
銆€銆€{
銆€銆€銆€銆€return coffeeImp;
銆€銆€}
}

鐪嬬湅涓澂鍔犲ザ 鍜屽ぇ鏉姞濂?鏄€庝箞鍑烘潵鐨?

//鎷垮嚭鐗涘ザ
CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton(new MilkCoffeeImp());

//涓澂鍔犲ザ
MediumCoffee mediumCoffee = new MediumCoffee();
mediumCoffee.pourCoffee();

//澶ф澂鍔犲ザ
SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee();
superSizeCoffee.pourCoffee();

娉ㄦ剰: Bridge妯″紡鐨勬墽琛岀被濡侰offeeImp鍜孋offee鏄竴瀵逛竴鐨勫叧緋? 姝g‘鍒涘緩CoffeeImp鏄妯″紡鐨勫叧閿€?br />



]]>
主站蜘蛛池模板: 闽侯县| 宣汉县| 邛崃市| 正镶白旗| 天峨县| 新余市| 交口县| 会东县| 葫芦岛市| 丹巴县| 阿拉善右旗| 河北省| 太湖县| 石首市| 长兴县| 吉首市| 海原县| 南涧| 平邑县| 思茅市| 姚安县| 天峻县| 云阳县| 凌云县| 旌德县| 泽库县| 徐闻县| 靖安县| 外汇| 昭觉县| 武隆县| 稷山县| 九龙县| 克东县| 轮台县| 杭州市| 南溪县| 乌鲁木齐市| 滨州市| 湘潭县| 三原县|