锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲愉拍一区二区三区,亚洲人成无码www久久久,国产V亚洲V天堂A无码http://m.tkk7.com/vcycyv/category/47724.htmlzh-cnTue, 22 Feb 2011 01:29:24 GMTTue, 22 Feb 2011 01:29:24 GMT60涓茶23縐嶈璁℃ā寮?/title><link>http://m.tkk7.com/vcycyv/archive/2011/02/15/344395.html</link><dc:creator>浜哄湪姹熸箹</dc:creator><author>浜哄湪姹熸箹</author><pubDate>Tue, 15 Feb 2011 14:43:00 GMT</pubDate><guid>http://m.tkk7.com/vcycyv/archive/2011/02/15/344395.html</guid><wfw:comment>http://m.tkk7.com/vcycyv/comments/344395.html</wfw:comment><comments>http://m.tkk7.com/vcycyv/archive/2011/02/15/344395.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://m.tkk7.com/vcycyv/comments/commentRss/344395.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/vcycyv/services/trackbacks/344395.html</trackback:ping><description><![CDATA[<p>闆墮浂紕庣鍦幫紝瀛︿範榪囧緢澶氳疆璁捐妯″紡銆傛繪槸瀛﹁繃灝卞繕銆傚悗鏉ュ氨鍦╣oogle docs涓婂仛浜嗕竴緋誨垪絎旇錛屾柟渚垮涔犮傛渶榪戞妸榪欎簺絎旇鎼鍒癰logjava涓婁簡錛屼竴杈規惉瀹訛紝涓杈瑰張澶嶄範涓閬嶃傚涔犺璁℃ā寮忔湁涓や釜鎰忎箟錛?/p> <p>涓銆佸涔犺璁℃ā寮忔渶閲嶈鐨勬槸浜嗚В妯″紡鐨勫簲鐢ㄥ満鏅傜紪紼嬮亣鍒扮壒瀹氬満鏅殑璇濓紝瑕佹湁鎰忚瘑鑱旀兂鍒拌璁℃ā寮忥紝鍝曠粏鑺傚繕浜嗕篃娌″叧緋伙紝緲葷炕涔﹀氨鎵懼埌浜嗐?/p> <p>浜屻佹彁楂樿璁$殑鎬濇兂銆傚涔犺璁℃ā寮忕殑鏃跺欙紝瑕佷綋浼氭ā寮忕簿濡欎箣澶勶紝褰撴兂鏄庣櫧澶у笀鎬濇兂鍜岃嚜宸辯殑宸窛鐨勬椂鍊欙紝宸窛灝辯緝鐭簡涓鐐瑰効銆?/p> <p>鏈夌殑妯″紡騫蟲貳鏃犲錛屽簲鐢ㄥ嵈騫挎硾銆傛湁鐨勬ā寮忚璁$簿宸э紝搴旂敤鍦烘櫙鍗翠笉鏄撻亣鍒般傛棤璁哄浣曪紝瑕佷箞鎺屾彙宸ュ叿錛岃涔堝鍒版濇兂錛岄兘鏄敹鑾楓?/p> <p> </p> <p>鏈閫傚悎綆鍗曟鎷璁℃ā寮忕殑鏄疷ML鍥撅紝鏀墮泦浜嗕竴涓潪甯稿ソ鐨勮祫婧愶紝鍒嗕韓鍦ㄦ垜鐨剆ky drive緗戠粶紜洏涓婏紝鍖呭惈23縐嶈璁℃ā寮忕殑UML鍥?/p> <p><a title="http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/books/designpatternscard.pdf" >http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/books/designpatternscard.pdf</a></p> <p> </p> <p>璁捐妯″紡鍙互鍒嗕負涓夌被錛歜ehavioural, structural, creational</p> <p><strong>behavioural 11 縐?/strong></p> <p>1. Memento錛堝蹇樺綍錛?/p> <p>綺懼閥紼嬪害錛?5鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>memento閫傚悎淇濆瓨/鎭㈠鐘舵佺殑鍦烘櫙銆傚垎涓哄鎺ュ彛鍜岀獎鎺ュ彛銆傚涔犲氨瑕佸紿勬帴鍙o紒</p> <p>鍏蜂綋璇瘋錛?<a title="http://m.tkk7.com/vcycyv/archive/2011/02/08/343949.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/08/343949.html">http://m.tkk7.com/vcycyv/archive/2011/02/08/343949.html</a></p> <p> </p> <p>2. Chain of Responsibility(璐d換閾?</p> <p>綺懼閥紼嬪害錛?3鏄?/p> <p>搴旂敤騫挎硾錛?3鏄?/p> <p>閫傚悎鐢ㄥ湪"涓緋誨垪handler”鐨勫満鏅笅銆傚垎涓虹函鍜屼笉綰袱縐嶏紝濂藉儚濂沖鍎夸篃鍙互榪欎箞鍒嗭紵</p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/12/344167.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/12/344167.html">http://m.tkk7.com/vcycyv/archive/2011/02/12/344167.html</a></p> <p> </p> <p>3. Observer錛堣瀵熻咃級</p> <p>鍥犱負java閲屾湁Obserable鍜孫bserver錛?鎵浠ラ氬父鍦╦ava閲屼笉浼氳嚜宸辮璁¤瀵熻呮ā寮忋?/p> <p> </p> <p>4. Command(鍛戒護妯″紡)</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>command妯″紡鐢ㄦ潵瑙hnvoker鍜宺eceiver. </p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/12/344168.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/12/344168.html">http://m.tkk7.com/vcycyv/archive/2011/02/12/344168.html</a></p> <p> </p> <p>5. State(鐘舵佹ā寮?</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>OO緙栫▼濡傛灉鍑虹幇澶氫釜鐩稿悓鐨刬f…else鏉′歡錛屽簲璇ヨ冭檻鐢╯tate妯″紡閲嶆瀯銆倃ork flow鐨勭姸鎬佸櫒涔熸槸鍩轟簬State妯″紡鐨勩?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/09/343998.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/09/343998.html">http://m.tkk7.com/vcycyv/archive/2011/02/09/343998.html</a></p> <p> </p> <p>6. Interpreter</p> <p>綺懼閥紼嬪害錛?N/A</p> <p>搴旂敤騫挎硾錛?1鏄?/p> <p>鎹鍙湁鎼滅儲寮曟搸鎵嶇敤寰椾笂榪欎釜妯″紡錛屽弽姝f垜姣忔瀛﹁璁℃ā寮忕殑鏃跺欙紝閮芥妸榪欎釜璺寵繃鍘伙紝灝辨槸涓嶅枩嬈紝鎶辨瓑銆?/p> <p> </p> <p>7. Strategy錛堢瓥鐣ユā寮忥級</p> <p>綺懼閥紼嬪害錛?2鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>鐢ㄦ潵灝佽涓嶅悓綆楁硶銆備粠uml鍥句笂鐪嬪拰state妯″紡涓鏍楓?鍥犱負榪欎釜澶畝鍗曚簡錛屾墍浠ユ病璁扮瑪璁幫紝榪欎釜蹇樹笉浜嗐?/p> <p> </p> <p>8. Iterator(榪唬瀛?</p> <p>Java Collection閮藉彲浠ヨ凱浠o紝鍦╦ava涓婁笉闇瑕佹牸澶栬璁terator銆?/p> <p> </p> <p>9. Template Method(妯℃澘鏂規硶)</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>瑙佸悕鐭ヤ箟銆傚お綆鍗曚簡錛屽榪囦竴閬嶅氨涓嶄細蹇樸備絾鏄繖涓簲鐢ㄩ潪甯稿箍娉涳紒</p> <p> </p> <p>10. Mediator(浠茶鑰?</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>鐢ㄦ潵瑙h﹀涓猵eer銆備釜浜鴻寰楄繖涓ā寮忓鍗婁細璁﹎ediator鍗佸垎鑷冭偪錛宮ediator鏈韓闇瑕佹湁寰堟竻鏅扮殑璁捐鎵嶈兘浣跨敤銆傚簲鐢ㄥ満鏅亹鎬曚篃灝戙?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/08/343951.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/08/343951.html">http://m.tkk7.com/vcycyv/archive/2011/02/08/343951.html</a></p> <p> </p> <p>11. Visitor(璁塊棶鑰?</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>collection閲屽瓨涓涓被緋葷殑entry鏃訛紝浣跨敤visitor妯″紡鍙互閬垮厤instance of鐨勪嬌鐢ㄣ傛洿閲嶈鐨勬槸鏂逛究鎵╁睍綾葷郴銆?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/13/344194.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/13/344194.html">http://m.tkk7.com/vcycyv/archive/2011/02/13/344194.html</a></p> <p> </p> <p><strong>Structure 7縐?/strong></p> <p>12. adapter(閫傞厤鍣?</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>涓漢瑙夊緱Adaptee鐢辯涓夋柟鎻愪緵鐨勬椂鍊欐墠鍙兘鐢ㄤ笂榪欎釜妯″紡[vcycyv緙栬緫錛氬悗鏉ユ剰璇嗗埌錛岃繖涓瘎璁轟笉瀹㈣銆俛daptee瀹炵幇鏍稿績鍔熻兘錛岄氳繃adapter鏆撮湶澶氱璁塊棶鏂瑰紡鐨勬儏鍐典笅錛宎daptee涔熸槸鑷繁鍐欑殑錛岃屼笉鏄涓夋柟鐨刔銆傜畝鍗曞簲鐢ㄨ寖鍥村張紿勶紝鐣?/p> <p> </p> <p>13. Proxy(浠g悊)</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>aop鏄唬鐞嗘ā寮忕殑涓縐嶅彂鎸ャ係pring鍜孒ibernate閮藉ぇ閲忎嬌鐢╬roxy銆傚彲浠ュ紩鐢沖瀛DK鐨刣ynamic proxy銆傛ā寮忕畝鍗曪紝鐣ャ?/p> <p> </p> <p>14. Bridge</p> <p>綺懼閥紼嬪害錛?5鏄?/p> <p>搴旂敤騫挎硾錛?3鏄?/p> <p>Bridge鍦ㄦ妧鏈笂鏄痵trategy鐨勮繘涓姝ュ彂鎸ワ紝浣嗕晶閲嶅湪瀹炵幇涓庢娊璞$殑瑙h︺?/p> <p>鍏蜂綋瑙侊細</p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/08/343950.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/08/343950.html">http://m.tkk7.com/vcycyv/archive/2011/02/08/343950.html</a></p> <p> </p> <p>15. Composite</p> <p>綺懼閥紼嬪害錛?4鏄?/p> <p>搴旂敤騫挎硾錛?3鏄?/p> <p>閫傜敤浜庢爲鐘剁粨鏋勩?/p> <p>鍏蜂綋瑙侊細</p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/13/344209.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/13/344209.html">http://m.tkk7.com/vcycyv/archive/2011/02/13/344209.html</a></p> <p> </p> <p>16. Decorator(瑁呴グ)</p> <p>綺懼閥紼嬪害錛?5鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>鍦╦ava I/O涓箍娉涗嬌鐢ㄣ備負浜嗚В鍐?#8220;綾葷垎鐐?#8221;鐨勯棶棰樸?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/09/343997.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/09/343997.html">http://m.tkk7.com/vcycyv/archive/2011/02/09/343997.html</a></p> <p> </p> <p>17. Facade(闂ㄩ潰)</p> <p>榪炰釜UML鍥鵑兘娌℃湁錛岀畻紲為┈妯″紡錛?/p> <p> </p> <p>18. Flyweight</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>鍒?#8220;鍗曠函”“澶嶅悎”涓ょ錛屾湰韜寘鍚簡宸ュ巶鏂規硶妯″紡銆備竴緋誨垪瀵硅薄濡傛灉浠栦滑閮芥湁鏌愰儴鍒嗗睘浜庣壒瀹氶泦鍚堬紝灝辮浠栦滑鍏變韓榪欎釜鐗瑰畾闆嗗悎浠ヨ妭鐪佽祫婧愩?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/12/344169.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/12/344169.html">http://m.tkk7.com/vcycyv/archive/2011/02/12/344169.html</a></p> <p> </p> <p><strong>Creational 6 縐?/strong></p> <p>19. Factory Method(宸ュ巶鏂規硶)</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>涓や釜鍙樼錛屼竴涓槸鏍規嵁鍙傛暟鍒涘緩瀵硅薄錛屼竴涓槸factory綾葷郴瀵瑰簲浜у搧綾葷郴銆傞潰鍚戞帴鍙g紪紼嬪湪鍒涘緩妯″紡鐨勪綋鐜般?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/13/344176.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/13/344176.html">http://m.tkk7.com/vcycyv/archive/2011/02/13/344176.html</a></p> <p> </p> <p>20. Abstract Factory錛堟娊璞″伐鍘傦級</p> <p>綺懼閥紼嬪害錛?鏄?/p> <p>搴旂敤騫挎硾錛?鏄?/p> <p>鍙湁浜у搧鏈変袱涓被緋葷殑鏃跺欐墠鐢ㄥ緱涓娿?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html">http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html</a></p> <p> </p> <p>21. Builder</p> <p>綺懼閥紼嬪害錛?5鏄?/p> <p>搴旂敤騫挎硾錛?2鏄?/p> <p>浜у搧鍒嗛儴鍒嗭紝騫朵笖build鐨勮繃紼嬩笉瀹氱殑鏃跺欒冭檻鐢ㄨ繖縐嶆ā寮忋傛槸妯℃澘妯″紡鐨勪竴縐嶅彂鎸?/p> <p>鍏蜂綋璇瘋錛?/p> <p><a title="http://m.tkk7.com/vcycyv/archive/2011/02/09/343995.html" href="http://m.tkk7.com/vcycyv/archive/2011/02/09/343995.html">http://m.tkk7.com/vcycyv/archive/2011/02/09/343995.html</a></p> <p> </p> <p>22. Prototype(鍘熷瀷)</p> <p>java鏈塩loneable鐨勬敮鎸侊紝鎵浠ラ氬父涓嶉渶瑕佽嚜宸辮璁rototype. java鐢╬rototype鐨勬椂鍊欐敞鎰忔繁澶嶅埗嫻呭鍒剁殑闂銆俻rototype涔嬪鐨勪竴涓夋嫨鏄彁渚涗竴涓猚onstructor鎺ュ彈鏈被鍨嬪璞′綔涓哄弬鏁幫紝榪欎釜鏂瑰紡鏇村姞瀹炵敤錛?/p> <p> </p> <p>23. singleton(鍗曟?</p> <p>鍙ゅ吀鐨剆ingleton鍒嗕負鎳掓眽錛岄タ姹変袱縐嶃侸DK1.5涔嬪悗鍙互鐢ㄥ敮涓entry 鐨別num浣滀負singleton鐨勫疄鐜般傚ソ澶勬湁涓ょ偣錛屼竴鏄畝鍗曘備簩鏄笉闇瑕佹牸澶栧鐞唖erializable鐨勬儏鍐?/p> <img src ="http://m.tkk7.com/vcycyv/aggbug/344395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/vcycyv/" target="_blank">浜哄湪姹熸箹</a> 2011-02-15 22:43 <a href="http://m.tkk7.com/vcycyv/archive/2011/02/15/344395.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>abstract factory錛堣漿錛?/title><link>http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html</link><dc:creator>浜哄湪姹熸箹</dc:creator><author>浜哄湪姹熸箹</author><pubDate>Tue, 15 Feb 2011 13:04:00 GMT</pubDate><guid>http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html</guid><wfw:comment>http://m.tkk7.com/vcycyv/comments/344390.html</wfw:comment><comments>http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/vcycyv/comments/commentRss/344390.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/vcycyv/services/trackbacks/344390.html</trackback:ping><description><![CDATA[     鎽樿: 杞澆鑷?http://www.javaeye.com/topic/413580#1064514 涓瀹跺叕鍙哥敓浜т袱涓郴鍒楃殑浜у搧   A緋誨垪錛?椋熷搧閲岋細 milk,meat,noodle   B緋誨垪錛?涓嶢緋誨垪鐩稿搴旂殑槨愬叿錛孲poon,Knife,Chopsticks 浜у搧鏈変袱涓郴鍒楋紝鍦ˋ緋誨垪鏌愪釜浣嶇疆鐨勪駭鍝侊紝鍦˙緋誨垪涓瀹氭湁涓涓搴旂殑浜у搧錛?鐗涘ザ--姹ゅ嫼錛?鑲?鍒錛?闈㈡潯--絳峰瓙 &n...  <a href='http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html'>闃呰鍏ㄦ枃</a><img src ="http://m.tkk7.com/vcycyv/aggbug/344390.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/vcycyv/" target="_blank">浜哄湪姹熸箹</a> 2011-02-15 21:04 <a href="http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>composite(杞?http://m.tkk7.com/vcycyv/archive/2011/02/13/344209.html浜哄湪姹熸箹浜哄湪姹熸箹Sun, 13 Feb 2011 12:30:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/13/344209.htmlhttp://m.tkk7.com/vcycyv/comments/344209.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/13/344209.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/344209.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/344209.htmlMotivation

When dealing with tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a "[2]. The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a least common denominator relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a group of shapes to have the same effect (in some sense) as resizing a single shape.

   1: import java.util.ArrayList;
   2:  
   3: /** "Component" */
   4: interface Graphic {
   5:  
   6:     //Prints the graphic.
   7:     public void print();
   8: }
   9:  
  10: /** "Composite" */
  11: class CompositeGraphic implements Graphic {
  12:  
  13:     //Collection of child graphics.
  14:     private List<Graphic> mChildGraphics = new ArrayList<Graphic>();
  15:  
  16:     //Prints the graphic.
  17:     public void print() {
  18:         for (Graphic graphic : mChildGraphics) {
  19:             graphic.print();
  20:         }
  21:     }
  22:  
  23:     //Adds the graphic to the composition.
  24:     public void add(Graphic graphic) {
  25:         mChildGraphics.add(graphic);
  26:     }
  27:  
  28:     //Removes the graphic from the composition.
  29:     public void remove(Graphic graphic) {
  30:         mChildGraphics.remove(graphic);
  31:     }
  32: }
  33:  
  34: /** "Leaf" */
  35: class Ellipse implements Graphic {
  36:  
  37:     //Prints the graphic.
  38:     public void print() {
  39:         System.out.println("Ellipse");
  40:     }
  41: }
  42:  
  43: /** Client */
  44: public class Program {
  45:  
  46:     public static void main(String[] args) {
  47:         //Initialize four ellipses
  48:         Ellipse ellipse1 = new Ellipse();
  49:         Ellipse ellipse2 = new Ellipse();
  50:         Ellipse ellipse3 = new Ellipse();
  51:         Ellipse ellipse4 = new Ellipse();
  52:  
  53:         //Initialize three composite graphics
  54:         CompositeGraphic graphic = new CompositeGraphic();
  55:         CompositeGraphic graphic1 = new CompositeGraphic();
  56:         CompositeGraphic graphic2 = new CompositeGraphic();
  57:  
  58:         //Composes the graphics
  59:         graphic1.add(ellipse1);
  60:         graphic1.add(ellipse2);
  61:         graphic1.add(ellipse3);
  62:  
  63:         graphic2.add(ellipse4);
  64:  
  65:         graphic.add(graphic1);
  66:         graphic.add(graphic2);
  67:  
  68:         //Prints the complete graphic (four times the string "Ellipse").
  69:         graphic.print();
  70:     }
  71: }


]]>
visitor(杞?http://m.tkk7.com/vcycyv/archive/2011/02/13/344194.html浜哄湪姹熸箹浜哄湪姹熸箹Sun, 13 Feb 2011 06:19:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/13/344194.htmlhttp://m.tkk7.com/vcycyv/comments/344194.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/13/344194.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/344194.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/344194.html鍙傝冿細http://en.wikipedia.org/wiki/Visitor_pattern

class=thumbimage

Example
The following example is in the Java programming language, and shows how the contents of a tree of nodes (in this case describing the components of a car) can be printed. Instead of creating "print" methods for each subclass (Wheel, Engine, Body, and Car), a single class (CarElementPrintVisitor) performs the required printing action. Because different subclasses require slightly different actions to print properly, CarElementDoVisitor dispatches actions based on the class of the argument passed to it.

alt=A Diagram of the Java Code Example.        I, the copyright holder of this work, hereby release it into the public domain.  This applies worldwide. In case this is not legally possible, I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.

interface CarElementVisitor {
    void visit(Wheel wheel);
    void visit(Engine engine);
    void visit(Body body);
    void visit(Car car);
}
interface CarElement {
    void accept(CarElementVisitor visitor); // CarElements have to provide accept().
}
class Wheel implements CarElement {
    private String name;
    public Wheel(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void accept(CarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Engine implements CarElement {
    public void accept(CarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Body implements CarElement {
    public void accept(CarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Car implements CarElement{
    CarElement[] elements;
    public CarElement[] getElements() {
        return elements.clone(); // Return a copy of the array of references.
    }
    public Car() {
        this.elements = new CarElement[]
          { new Wheel("front left"), new Wheel("front right"),
            new Wheel("back left") , new Wheel("back right"),
            new Body(), new Engine() };
    }
    public void accept(CarElementVisitor visitor) {
        for(CarElement element : this.getElements()) {
            element.accept(visitor);
        }
        visitor.visit(this);
    }
}
class CarElementPrintVisitor implements CarElementVisitor {
    public void visit(Wheel wheel) {    
        System.out.println("Visiting "+ wheel.getName()
                            + " wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Visiting engine");
    }
    public void visit(Body body) {
        System.out.println("Visiting body");
    }
    public void visit(Car car) {    
        System.out.println("Visiting car");
    }
}
class CarElementDoVisitor implements CarElementVisitor {
    public void visit(Wheel wheel) {
        System.out.println("Kicking my "+ wheel.getName() + " wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Starting my engine");
    }
    public void visit(Body body) {
        System.out.println("Moving my body");
    }
    public void visit(Car car) {
        System.out.println("Starting my car");
    }
}
public class VisitorDemo {
    static public void main(String[] args){
        Car car = new Car();
        car.accept(new CarElementPrintVisitor());
        car.accept(new CarElementDoVisitor());
    }


]]>
factory method(杞?http://m.tkk7.com/vcycyv/archive/2011/02/13/344176.html浜哄湪姹熸箹浜哄湪姹熸箹Sun, 13 Feb 2011 03:33:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/13/344176.htmlhttp://m.tkk7.com/vcycyv/comments/344176.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/13/344176.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/344176.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/344176.html鍩烘湰姒傚康

FactoryMethod鏄竴縐嶅垱寤烘фā寮?瀹冨畾涔変簡涓涓垱寤哄璞$殑鎺ュ彛,浣嗘槸鍗磋瀛愮被鏉ュ喅瀹氬叿浣撳疄渚嬪寲鍝竴涓被.褰撲竴涓被鏃犳硶棰勬枡瑕佸垱寤哄摢縐嶇被鐨勫璞℃垨鏄竴涓被闇瑕佺敱瀛愮被鏉ユ寚瀹氬垱寤虹殑瀵硅薄鏃舵垜浠氨闇瑕佺敤鍒癋actory Method 妯″紡浜?綆鍗曡鏉?Factory Method鍙互鏍規嵁涓嶅悓鐨勬潯浠朵駭鐢熶笉鍚岀殑瀹炰緥,褰撶劧榪欎簺涓嶅悓鐨勫疄渚嬮氬父鏄睘浜庣浉鍚岀殑綾誨瀷,鍏鋒湁鍏卞悓鐨勭埗綾?Factory Method鎶婂垱寤鴻繖浜涘疄渚嬬殑鍏蜂綋榪囩▼灝佽璧鋒潵浜?綆鍖栦簡瀹㈡埛绔殑搴旂敤,涔熸敼鍠勪簡紼嬪簭鐨勬墿灞曟?浣垮緱灝嗘潵鍙互鍋氭渶灝忕殑鏀瑰姩灝卞彲浠ュ姞鍏ユ柊鐨勫緟鍒涘緩鐨勭被. 閫氬父鎴戜滑灝咶actory Method浣滀負涓縐嶆爣鍑嗙殑鍒涘緩瀵硅薄鐨勬柟娉?褰撳彂鐜伴渶瑕佹洿澶氱殑鐏墊椿鎬х殑鏃跺?灝卞紑濮嬭冭檻鍚戝叾瀹冨垱寤哄瀷妯″紡杞寲

綆鍗曞垎鏋?

鍥?鏄疐actory Method 妯″紡鐨勭粨鏋勫浘,榪欓噷鎻愪緵浜嗕竴浜涙湳璇?璁╂垜浠彲浠ヨ繘琛屾洿鏂逛究鐨勬弿榪?

  1. Product: 闇瑕佸垱寤虹殑浜у搧鐨勬娊璞$被.
  2. ConcreteProduct: Product鐨勫瓙綾?涓緋誨垪鍏蜂綋鐨勪駭鍝?
  3. Creator: 鎶借薄鍒涘緩鍣ㄦ帴鍙?澹版槑榪斿洖Product綾誨瀷瀵硅薄鐨凢actory Method.
  4. ConcreteCreator: 鍏蜂綋鐨勫垱寤哄櫒,閲嶅啓Creator涓殑Factory Method,榪斿洖ConcreteProduct綾誨瀷鐨勫疄渚?

鍥?: Factory Method 妯″紡緇撴瀯

 

鐢辨鍙互娓呮鐨勭湅鍑鴻繖鏍風殑騫寵瀵瑰簲鍏崇郴: Product <====> Creator ; ConreteProduct <====> ConreteCreator

鎶借薄浜у搧瀵瑰簲鎶借薄鍒涘緩鍣?鍏蜂綋浜у搧瀵瑰簲鍏蜂綋鍒涘緩鍣?榪欐牱鍋氱殑濂藉鏄粈涔堝憿?涓轟粈涔堟垜浠笉鐩存帴鐢ㄥ叿浣撶殑浜у搧鍜屽叿浣撶殑鍒涘緩鍣ㄥ畬鎴愰渶姹傚憿?瀹為檯涓婃垜浠篃鍙互榪欐牱鍋?浣嗛氳繃Factory Method妯″紡鏉ュ畬鎴?瀹㈡埛(client)鍙渶寮曠敤鎶借薄鐨凱roduct鍜孋reater,瀵瑰叿浣撶殑ConcreteProduct鍜孋oncreteCreator鍙互姣笉鍏沖績,榪欐牱鍋氭垜浠彲浠ヨ幏寰楅澶栫殑濂藉:

  • 棣栧厛瀹㈡埛绔彲浠ョ粺涓浠庢娊璞″垱寤哄櫒鑾峰彇浜х敓鐨勫疄渚?Creator鐨勪綔鐢ㄥ皢client鍜屼駭鍝佸垱寤鴻繃紼嬪垎紱誨紑鏉?瀹㈡埛涓嶇敤鎿嶅績榪斿洖鐨勬槸閭d竴涓叿浣撶殑浜у搧,涔熶笉鐢ㄥ叧蹇冭繖浜涗駭鍝佹槸濡備綍鍒涘緩鐨?鍚屾椂,ConcreteProduct涔熻闅愯棌鍦≒roduct鍚庨潰,ConreteProduct緇ф壙浜哖roduct鐨勬墍鏈夊睘鎬?騫跺疄鐜頒簡Product涓畾涔夌殑鎶借薄鏂規硶,鎸夌収Java涓殑瀵硅薄閫犲瀷(cast)鍘熷垯,閫氳繃ConcreteCreator浜х敓鐨凜oncreteProduct鍙互鑷姩鐨勪笂婧犲瀷鎴怭roduct.榪欐牱涓鏉?瀹炶川鍐呭涓嶅悓鐨凜oncreteProduct灝卞彲浠ュ湪褰㈠紡涓婄粺涓涓篜roduct,閫氳繃Creator鎻愪緵緇檆lient鏉ヨ闂?
  • 鍏舵,褰撴垜浠坊鍔犱竴涓柊鐨凜oncreteCreator鏃?鐢變簬Creator鎵鎻愪緵鐨勬帴鍙d笉鍙?瀹㈡埛绔▼搴忎笉浼氭湁涓濇鐨勬敼鍔?涓嶄細甯︽潵鍔ㄤ竴鍙戣岀壍鍏ㄨ韓鐨勭伨闅? 榪欏氨鏄壇濂藉皝瑁呮х殑浣撶幇.浣嗗鏋滅洿鎺ョ敤ConcreteProduct鍜孋oncreteCreator涓や釜綾繪槸鏃犺濡備綍涔熷仛涓嶅埌榪欑偣鐨? 浼樿壇鐨勯潰鍚戝璞¤璁¢紦鍔變嬌鐢ㄥ皝瑁?encapsulation)鍜屽鎵?delegation),鑰孎actory Method妯″紡灝辨槸浣跨敤浜嗗皝瑁呭拰濮旀墭鐨勫吀鍨嬩緥瀛?榪欓噷灝佽鏄氳繃鎶借薄鍒涘緩鍣–reator鏉ヤ綋鐜扮殑,鑰屽鎵樺垯鏄氳繃鎶借薄鍒涘緩鍣ㄦ妸鍒涘緩瀵硅薄鐨勮矗浠誨畬鍏ㄤ氦緇欏叿浣撳垱寤哄櫒ConcreteCreator鏉ヤ綋鐜扮殑.

鐜板湪,璇峰啀鍥炲ご鐪嬬湅鍩烘湰姒傚康涓殑閭f璇?寮濮嬩篃璁歌寰楃敓娑╅毦鎳?鐜板湪鏄笉鏄凡緇忔槑鏈楀寲浜嗗緢澶?

涓嬮潰璁╂垜浠湅鐪嬪湪 Java 涓浣曞疄鐜癋actory Method妯″紡,榪涗竴姝ュ姞娣卞瀹冪殑璁よ瘑.

鍏蜂綋瀹炴柦

鍏堣鏄庝竴鐐?鐢‵actory Method妯″紡鍒涘緩瀵硅薄騫朵笉涓瀹氫細璁╂垜浠殑浠g爜鏇寸煭,瀹炰簨涓婂線寰鏇撮暱,鎴戜滑涔熶嬌鐢ㄤ簡鏇村鐨勭被,鐪熸鐨勭洰鐨勫湪浜庤繖鏍峰彲浠ョ伒媧葷殑,鏈夊脊鎬х殑鍒涘緩涓嶇‘瀹氱殑瀵硅薄.鑰屼笖,浠g爜鐨勫彲閲嶇敤鎬ф彁楂樹簡,瀹㈡埛绔殑搴旂敤綆鍖栦簡,瀹㈡埛紼嬪簭鐨勪唬鐮佷細澶уぇ鍑忓皯,鍙樼殑鏇村叿鍙鎬?

  1. 鏍囧噯瀹炵幇: 榪欓噷鎴戦噰鐢˙ruce Eckel 鐢ㄦ潵鎻忚堪OO鎬濇兂鐨勭粡鍏鎬緥瀛?Shape.榪欐牱澶у浼氭瘮杈冪啛鎮変竴浜?鎴戝畬鍏ㄦ寜鐓у浘1涓墍瀹氫箟鐨勭粨鏋勫啓浜嗕笅闈㈢殑涓孌墊紨紺轟唬鐮?榪欐浠g爜鐨勪綔鐢ㄦ槸鍒涘緩涓嶅悓鐨凷hape瀹炰緥,姣忎釜瀹炰緥瀹屾垚涓や釜鎿嶄綔:draw鍜宔rase.鍏蜂綋鐨勫垱寤鴻繃紼嬪鎵橈拷oShapeFactory鏉ュ畬鎴?

    1.a 棣栧厛瀹氫箟涓涓娊璞$被Shape,瀹氫箟涓や釜鎶借薄鐨勬柟娉?

    abstract class Shape {
      // 鍕劇敾shape
      public abstract void draw();
      // 鎿﹀幓 shape
      public abstract void erase();
      public String name;
      public Shape(String aName){
        name = aName;
      }
    }

    1.b 瀹氫箟 Shape鐨勪袱涓瓙綾? Circle, Square,瀹炵幇Shape涓畾涔夌殑鎶借薄鏂規硶

    // 鍦嗗艦瀛愮被
    class Circle extends Shape {
      public void draw() {
        System.out.println("It will draw a circle.");
      }
      public void erase() {
        System.out.println("It will erase a circle."); 
      }
      // 鏋勯犲嚱鏁?
      public Circle(String aName){
        super(aName);
      }
    }
    // 鏂瑰艦瀛愮被
    class Square extends Shape {
      public void draw() { 
        System.out.println("It will draw a square."); 
      }
      public void erase() { 
        System.out.println("It will erase a square."); 
      }
      // 鏋勯犲嚱鏁?
      public Square(String aName){
        super(aName);
      }
    }
    

    1.c 瀹氫箟鎶借薄鐨勫垱寤哄櫒,anOperation璋冪敤factoryMethod鍒涘緩涓涓璞?騫跺璇ュ璞¤繘琛屼竴緋誨垪鎿嶄綔.

    abstract class ShapeFactory {  
      protected abstract Shape factoryMethod(String aName);
      // 鍦╝nOperation涓畾涔塖hape鐨勪竴緋誨垪琛屼負
    public void anOperation(String aName){
        Shape s = factoryMethod(aName);
        System.out.println("The current shape is: " + s.name);
        s.draw();
        s.erase();
      }
    }

    1.d 瀹氫箟涓巆ircle鍜宻quare鐩稿搴旂殑涓や釜鍏蜂綋鍒涘緩鍣–ircleFactory,SquareFactory,瀹炵幇鐖剁被鐨刴ethodFactory鏂規硶

    // 瀹氫箟榪斿洖 circle 瀹炰緥鐨?CircleFactory
    class CircleFactory extends ShapeFactory {
      // 閲嶈澆factoryMethod鏂規硶,榪斿洖Circle瀵硅薄
      protected Shape factoryMethod(String aName) {
        return new Circle(aName + " (created by CircleFactory)");
      }
    }
      
    // 瀹氫箟榪斿洖 Square 瀹炰緥鐨?SquareFactory
    class SquareFactory extends ShapeFactory {
      // 閲嶈澆factoryMethod鏂規硶,榪斿洖Square瀵硅薄
    protected Shape factoryMethod(String aName) {
        return new Square(aName + " (created by SquareFactory)");
      }
    }
    

    1.e 嫻嬭瘯綾?璇鋒敞鎰忚繖涓鎴風紼嬪簭澶氫箞綆媧?鏃㈡病鏈夌綏鍡︾殑鏉′歡鍒ゆ柇璇彞,涔熸棤闇鍏沖績ConcreteProduct鍜孋oncreteCreator鐨勭粏鑺?鍥犱負榪欓噷鎴戠敤anOperation灝佽浜哖roduct閲岀殑涓や釜鏂規硶,鎵浠ヨ繛Product鐨勫獎瀛愪篃娌$湅瑙?褰撶劧鎶奝roduct閲屾柟娉曠殑鍏蜂綋璋冪敤鏀懼埌瀹㈡埛紼嬪簭涓篃鏄笉閿欑殑).

    class Main {
      public static void main(String[] args){
        ShapeFactory sf1 = new SquareFactory(); 
        ShapeFactory sf2 = new CircleFactory();
        sf1.anOperation("Shape one");
        sf2.anOperation("Shape two");
      }
    }  
    

    榪愯緇撴灉濡備笅:

    The current shape is: Shape one (created by SquareFactory)

    It will draw a square.

    It will erase a square.

    The current shape is: Shape two (created by CircleFactory)

    It will draw a circle.

    It will erase a circle.

  2. 鍙傛暟鍖栫殑Factory Method: 榪欑鏂瑰紡渚濋潬鎸囧畾鐨勫弬鏁頒綔涓烘爣蹇楁潵鍒涘緩瀵瑰簲鐨勫疄渚?榪欐槸寰堝父瑙佺殑涓縐嶅姙娉?姣斿JFC涓殑BorderFactory灝辨槸涓緢涓嶉敊鐨勪緥瀛? 浠ヤ笅鐨勮繖涓緥瀛愭槸鐢ㄥ瓧絎︿覆浣滀負鏍囪鏉ヨ繘琛屽垽鏂殑,濡傛灉鍙傛暟鐨勭被鍨嬩篃涓嶄竴鏍?閭e氨鍙互鐢ㄥ埌榪囪澆鍑芥暟鏉ヨВ鍐寵繖涓棶棰?瀹氫箟涓緋誨垪鍙傛暟鍜屾柟娉曚綋涓嶅悓鐨勫悓鍚嶅嚱鏁?榪欓噷java.util.Calendar.getInstance()鍙堟槸涓瀬濂界殑渚嬪瓙.鍙傛暟鍖栫殑鍒涘緩鏂瑰紡鍏嬫湇浜咶actory Method妯″紡涓涓渶鏄捐憲鐨勭己闄?灝辨槸褰撳叿浣撲駭鍝佹瘮杈冨鏃?鎴戜滑涓嶅緱涓嶄篃寤虹珛涓緋誨垪涓庝箣瀵瑰簲鐨勫叿浣撴瀯閫犲櫒. 浣嗘槸鍦ㄥ鎴風鎴戜滑蹇呴』鎸囧畾鍙傛暟鏉ュ喅瀹氳鍒涘緩鍝竴涓被.

    2.a 鎴戜滑鍦ㄧ涓縐嶆柟娉曠殑鍩虹涓婅繘琛屼慨鏀?棣栧厛鑷畾涔変竴涓殑寮傚父,榪欐牱褰撲紶鍏ヤ笉姝g‘鐨勫弬鏁版椂鍙互寰楀埌鏇存槑鏄劇殑鎶ラ敊淇℃伅.

    class NoThisShape extends Exception {
      public NoThisShape(String aName) {
        super(aName);
      }
    }
    

    2.b鍘繪帀浜哠hapeFactory鐨勪袱涓瓙綾?鏀逛負鐢盨hapeFactory鐩存帴璐熻矗瀹炰緥鐨勫垱寤? ShapeFactory鑷繁鍙樻垚涓涓叿浣撶殑鍒涘緩鍣?鐩存帴鐢ㄥ弬鏁板寲鐨勬柟娉曞疄鐜癴actoryMethod榪斿洖澶氱瀵硅薄.

    abstract class ShapeFactory {  
      private static Shape s;
      private ShapeFactory() {}
        
      static Shape factoryMethod(String aName, String aType) throws NoThisShape{
        if (aType.compareTo("square")==0)
          return new Square(aName);
        else if (aType.compareTo("circle")==0)
          return new Circle(aName);
        else throw new NoThisShape(aType);  
      }
      
      // 鍦╝nOperation涓畾涔塖hape鐨勪竴緋誨垪琛屼負
      static void anOperation(String aName, String aType) throws NoThisShape{
        s = factoryMethod(aName, aType);
        System.out.println("The current shape is: " + s.name);
        s.draw();
        s.erase();
      }
    }
    

    2.c 嫻嬭瘯綾?榪欓噷瀹㈡埛绔繀欏繪寚瀹氬弬鏁版潵鍐沖畾鍏蜂綋鍒涘緩鍝釜綾?榪欎釜渚嬪瓙閲岀殑anOperation鏄潤鎬佸嚱鏁?鍙互鐩存帴寮曠敤.

    class Main {
      public static void main(String[] args) throws NoThisShape{
        ShapeFactory.anOperation("Shape one","circle");
        ShapeFactory.anOperation("Shape two","square");
        ShapeFactory.anOperation("Shape three", "delta");
      }
    }
    

    榪愯緇撴灉濡備笅:

    The current shape is: Shape one
    It will draw a circle.
    It will erase a circle.
    The current shape is: Shape two
    It will draw a square.
    It will erase a square.
    Exception in thread "main" NoThisShape: delta
            at ShapeFactory.factoryMethod(ShapeFactory.java:10)
            at ShapeFactory.anOperation(ShapeFactory.java:15)
            at Main.main(Main.java:5)
    
  3. 鍔ㄦ佽杞芥満鍒?

    鏈夌殑鏃跺欐垜浠細鎶奀oncreteProduct鐨勫疄渚嬩紶緇欏垱寤哄櫒浣滀負鍙傛暟,榪欑鎯呭喌涓?濡傛灉鍦ㄥ垱寤哄櫒閲屽畬鎴愬垱寤鴻繃紼?灝卞繀欏誨垽鏂弬鏁扮殑鍏蜂綋綾誨瀷(鐢╥nstanceof),鐒跺悗鎵嶈兘浜х敓鐩稿簲鐨勫疄渚?閭d箞姣旇緝濂界殑鍋氭硶鏄埄鐢↗ava鐨勫姩鎬佽杞芥満鍒舵潵瀹屾垚榪欎歡浜?姣斿:

    鎴戜滑寰楀埌涓涓猄hape鐨勫瓙綾籹,浣嗕笉鐭ラ亾鍏蜂綋鏄偅涓瓙綾?灝卞彲浠ュ埄鐢–lass綾昏嚜甯︾殑鏂規硶newInstance()寰楀埌瀹炰緥

    return (Shape)s.getClass().newInstance();

    榪欑鏂規硶鏈夊叴瓚e緱璇昏呭彲浠ヨ嚜宸卞皾璇?闄愪簬綃囧箙,涓嶅啓鍏蜂綋浠g爜鍑烘潵浜?



]]>
fly weight(杞?http://m.tkk7.com/vcycyv/archive/2011/02/12/344169.html浜哄湪姹熸箹浜哄湪姹熸箹Sat, 12 Feb 2011 15:57:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/12/344169.htmlhttp://m.tkk7.com/vcycyv/comments/344169.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/12/344169.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/344169.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/344169.html闃呰鍏ㄦ枃

]]>
command(杞?http://m.tkk7.com/vcycyv/archive/2011/02/12/344168.html浜哄湪姹熸箹浜哄湪姹熸箹Sat, 12 Feb 2011 15:25:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/12/344168.htmlhttp://m.tkk7.com/vcycyv/comments/344168.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/12/344168.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/344168.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/344168.html鍙傝?http://en.wikipedia.org/wiki/Command_pattern

Command Design Pattern Class Diagram.png

/*the Invoker class*/
public class Switch {
 
    private Command flipUpCommand;
    private Command flipDownCommand;
 
    public Switch(Command flipUpCmd, Command flipDownCmd) {
         this.flipUpCommand = flipUpCmd;
         this.flipDownCommand = flipDownCmd;
    }
 
    public void flipUp() {
         flipUpCommand.execute();
    }
 
    public void flipDown() {
         flipDownCommand.execute();
    }
}
 
/*Receiver class*/
public class Light {
 
     public Light() {  }
 
     public void turnOn() {
        System.out.println("The light is on");
     }
 
     public void turnOff() {
        System.out.println("The light is off");
     }
}
 
/*the Command interface*/
public interface Command {
    void execute();
}
 
/*the Command for turning on the light*/
public class FlipUpCommand implements Command {
 
   private Light theLight;
 
   public FlipUpCommand(Light light) {
        this.theLight=light;
   }
 
   public void execute(){
      theLight.turnOn();
   }
}
 
/*the Command for turning off the light*/
public class FlipDownCommand implements Command {
 
   private Light theLight;
 
   public FlipDownCommand(Light light) {
        this.theLight=light;
   }
 
   public void execute() {
      theLight.turnOff();
   }
}
 
/*The test class or client*/
public class PressSwitch {
 
   public static void main(String[] args) {
       Light lamp = new Light();
       Command switchUp = new FlipUpCommand(lamp);
       Command switchDown = new FlipDownCommand(lamp);
 
       // See criticism of this model above:
       // The switch itself should not be aware of lamp details (switchUp, switchDown) 
       // either directly or indirectly
       Switch s = new Switch(switchUp,switchDown);
 
       try {
           if (args[0].equalsIgnoreCase("ON")) {
                s.flipUp();
           } else if (args[0].equalsIgnoreCase("OFF")) {
               s.flipDown();
           } else {
               System.out.println("Argument \"ON\" or \"OFF\" is required.");
           }
       } catch (Exception e){
           System.out.println("Arguments required.");
       }
   }
}
=====================

java tip (http://www.javaworld.com/javaworld/javatips/jw-javatip68.html?page=3) 緇欎簡綾諱技鐨勪緥瀛愶紝澶氫簡Fan浣滀負receiver:

TestCommand.java
class Fan {
        public void startRotate() {
                System.out.println("Fan is rotating");
        }
        public void stopRotate() {
                System.out.println("Fan is not rotating");
        }
}
class Light {
        public void turnOn( ) {
                System.out.println("Light is on ");
        }
        public void turnOff( ) {
                System.out.println("Light is off");
        }
}
class Switch {
        private Command UpCommand, DownCommand;
        public Switch( Command Up, Command Down) {
                UpCommand = Up; // concrete Command registers itself with the invoker
                DownCommand = Down;
        }
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver
                        UpCommand . execute ( ) ;                          
        }
        void flipDown( ) {
                        DownCommand . execute ( );
        }
}
class LightOnCommand implements Command {
        private Light myLight;
        public LightOnCommand ( Light L) {
                myLight  =  L;
        }
        public void execute( ) {
                myLight . turnOn( );
        }
}
class LightOffCommand implements Command {
        private Light myLight;
        public LightOffCommand ( Light L) {
                myLight  =  L;
        }
        public void execute( ) {
                myLight . turnOff( );
        }
}
class FanOnCommand implements Command {
        private Fan myFan;
        public FanOnCommand ( Fan F) {
                myFan  =  F;
        }
        public void execute( ) {
                myFan . startRotate( );
        }
}
class FanOffCommand implements Command {
        private Fan myFan;
        public FanOffCommand ( Fan F) {
                myFan  =  F;
        }
        public void execute( ) {
                myFan . stopRotate( );
        }
}
public class TestCommand {
                public static void main(String[] args) {
                        Light  testLight = new Light( );
                        LightOnCommand testLOC = new LightOnCommand(testLight);
                        LightOffCommand testLFC = new LightOffCommand(testLight);
                        Switch testSwitch = new Switch( testLOC,testLFC);      
                        testSwitch.flipUp( );
                        testSwitch.flipDown( );
                        Fan testFan = new Fan( );
                        FanOnCommand foc = new FanOnCommand(testFan);
                        FanOffCommand ffc = new FanOffCommand(testFan);
                        Switch ts = new Switch( foc,ffc);
                        ts.flipUp( );
                        ts.flipDown( );
                }
}              
Command.java
public interface Command {
        public abstract void execute ( );
}

]]>
chain of responsibility(杞?http://m.tkk7.com/vcycyv/archive/2011/02/12/344167.html浜哄湪姹熸箹浜哄湪姹熸箹Sat, 12 Feb 2011 15:17:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/12/344167.htmlhttp://m.tkk7.com/vcycyv/comments/344167.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/12/344167.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/344167.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/344167.htmlThe following Java code illustrates the pattern with the example of a logging class. Each logging handler decides if any action is to be taken at this log level and then passes the message on to the next logging handler. The output is:

Writing to stdout: Entering function y. Writing to stdout: Step1 completed. Sending via e-mail: Step1 completed. Writing to stdout: An error has occurred. Sending via e-mail: An error has occurred. Writing to stderr: An error has occurred.

Note that this example should not be seen as a recommendation on how to write logging classes.

Also, note that in a 'pure' implementation of the chain of responsibility pattern, a logger would not pass responsibility further down the chain after handling a message. In this example, a message will be passed down the chain whether it is handled or not.

import java.util.*;
abstract class Logger
{
    public static int ERR = 3;
    public static int NOTICE = 5;
    public static int DEBUG = 7;
    protected int mask;
    // The next element in the chain of responsibility
    protected Logger next;
    public Logger setNext( Logger l )
    {
        next = l;
        return l;
    }
    public void message( String msg, int priority )
    {
        if ( priority <= mask )
        {
            writeMessage( msg );
        }
        if ( next != null )
        {
            next.message( msg, priority );
        }
    }
    abstract protected void writeMessage( String msg );
}
class StdoutLogger extends Logger
{
    public StdoutLogger( int mask ) { this.mask = mask; }
    protected void writeMessage( String msg )
    {
        System.out.println( "Writing to stdout: " + msg );
    }
}
class EmailLogger extends Logger
{
    public EmailLogger( int mask ) { this.mask = mask; }
    protected void writeMessage( String msg )
    {
        System.out.println( "Sending via email: " + msg );
    }
}
class StderrLogger extends Logger
{
    public StderrLogger( int mask ) { this.mask = mask; }
    protected void writeMessage( String msg )
    {
        System.err.println( "Sending to stderr: " + msg );
    }
}
public class ChainOfResponsibilityExample
{
    public static void main( String[] args )
    {
        // Build the chain of responsibility
        Logger l,l1;
        l1 = l = new StdoutLogger( Logger.DEBUG );
        l1 = l1.setNext(new EmailLogger( Logger.NOTICE ));
        l1 = l1.setNext(new StderrLogger( Logger.ERR ));
        // Handled by StdoutLogger
        l.message( "Entering function y.", Logger.DEBUG );
        // Handled by StdoutLogger and EmailLogger
        l.message( "Step1 completed.", Logger.NOTICE );
        // Handled by all three loggers
        l.message( "An error has occurred.", Logger.ERR );
    }
}



]]>
state(杞?http://m.tkk7.com/vcycyv/archive/2011/02/09/343998.html浜哄湪姹熸箹浜哄湪姹熸箹Wed, 09 Feb 2011 15:08:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/09/343998.htmlhttp://m.tkk7.com/vcycyv/comments/343998.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/09/343998.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/343998.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/343998.htmlstate妯″紡

涓銆丼tate妯″紡瀹氫箟:

銆銆鍏佽涓涓璞″湪鍏剁姸鎬佹敼鍙樻椂錛屾敼鍙樺畠鐨勮涓恒傜湅璧鋒潵瀵硅薄浼間箮淇敼浜嗗畠鐨勭被銆?

銆銆浜屻佹ā寮忚В璇?

銆銆State妯″紡涓昏瑙e喅鐨勬槸鍦ㄥ紑鍙戜腑鏃跺父閬囧埌鐨勬牴鎹笉鍚岀殑鐘舵侀渶瑕佽繘琛屼笉鍚岀殑澶勭悊鎿嶄綔鐨勯棶棰?鑰岃繖鏍風殑闂,澶ч儴鍒嗕漢鏄噰鐢╯witch-case璇彞榪涜澶勭悊鐨?榪欐牱浼氶犳垚涓涓棶棰?鍒嗘敮榪囧,鑰屼笖濡傛灉鍔犲叆涓涓柊鐨勭姸鎬佸氨闇瑕佸鍘熸潵鐨勪唬鐮佽繘琛岀紪璇戙係tate妯″紡閲囩敤浜嗗榪欎簺涓嶅悓鐨勭姸鎬佽繘琛屽皝瑁呯殑鏂瑰紡澶勭悊榪欑被闂,褰撶姸鎬佹敼鍙樼殑鏃跺欒繘琛屽鐞嗙劧鍚庡啀鍒囨崲鍒板彟涓縐嶇姸鎬?涔熷氨鏄鎶婄姸鎬佺殑鍒囨崲璐d換浜ょ粰浜嗗叿浣撶殑鐘舵佺被鍘昏礋璐?鍚屾椂,State妯″紡鍜?Strategy妯″紡鏈夊緢澶氱浉浼肩殑鍦版柟,闇瑕佽鏄庣殑鏄袱鑰呯殑鎬濇兂閮芥槸涓鑷寸殑,鍙笉榪囧皝瑁呯殑涓滆タ涓嶅悓:State妯″紡灝佽鐨勬槸涓嶅悓鐨勭姸鎬?鑰孲tategy 妯″紡灝佽鐨勬槸涓嶅悓鐨勭畻娉曘?

銆銆涓夈佺粨鏋勫浘

銆銆State妯″紡緇撴瀯鍥懼涓?

銆銆鍥涖佹庝箞浣跨敤錛?

銆銆1錛夊畾涔変竴涓猄tate鎺ュ彛錛屾帴鍙d腑鏈変竴涓粺涓鐨勬柟娉曪紝鐢ㄤ互灝佽涓涓壒瀹氱姸鎬佹墍瀵瑰簲鐨勮涓恒?

銆銆2錛夊畾涔夊叿浣撲笉鍚岀姸鎬佺被ConcreteSate瀹炵幇State鎺ュ彛銆?

銆銆3錛夋瘡涓涓姸鎬佺被閮藉疄鐜扮幆澧冿紙Context錛変竴涓姸鎬佹墍瀵瑰簲鐨勮涓恒?

銆銆4錛夊畾涔変竴涓姸鎬佺鐞嗗櫒Context.

銆銆浜斻佷竴涓緥瀛?/p>

   1: interface State{
   2: public void handle(Context ctx);
   3: }
   4: class ConcreteStateA implements State{
   5: 銆銆
   6: public void handle(Context ctx) {
   7: 銆System.out.println("handle by ConcreteStateA");
   8:if(ctx!=null){
   9: 銆ctx.ChangeState(new ConcreteStateB());
  10: 銆}
  11:
  12: }
  13: }
  14: class ConcreteStateB implements State{
  15: 銆銆
  16: public void handle(Context ctx) {
  17: 銆System.out.println("handle by ConcreteStateB");
  18:if(ctx!=null){
  19: 銆ctx.ChangeState(new ConcreteStateA());
  20: 銆}
  21:
  22: }
  23: }
  24: class Context{
  25: private State _state;
  26: public Context(State state){
  27: 銆_state=state;
  28: }
  29: public void request(){
  30:if(_state!=null){
  31: 銆_state.handle(this);
  32: 銆}
  33: }
  34: public void ChangeState(State s){
  35:if(_state!=null){
  36: 銆_state=null;
  37: 銆}
  38: 銆_state=s;
  39: }
  40: }
  41: public class StateClient {
  42: 銆銆
  43: public static void main(String[] args) {
  44: 銆State state=new ConcreteStateA();
  45: 銆Context context=new Context(state);
  46: 銆context.request();
  47: 銆context.request();
  48: 銆context.request();
  49: 銆context.request();
  50: 銆銆
  51: }
  52: 銆銆
  53: }

杈撳嚭緇撴灉:

銆銆handle by ConcreteStateA

銆銆handle by ConcreteStateB

銆銆handle by ConcreteStateA

銆銆handle by ConcreteStateB

銆銆姣忚姹備竴嬈★紝鐘舵佸氨鏇存崲涓嬈★紝灝辨墽琛屽搴旂殑琛屼負銆?

銆銆鍏侀傜敤鎬?

銆銆1錛変竴涓璞$殑琛屼負鍙栧喅浜庡畠鐨勭姸鎬侊紝騫朵笖瀹冨繀欏誨湪榪愯鏃跺埢鏍規嵁鐘舵佹敼鍙樺畠鐨勮涓恒?

銆銆2錛変竴涓璞″惈鏈夊簽澶х殑鏉′歡鍒嗘敮璇彞錛岃繖浜涘垎鏀緷璧栦簬瀹冪殑鐘舵併傝繖涓姸鎬侀氬父鐢ㄤ竴涓垨澶氫釜鏋氫婦甯擱噺琛ㄧず銆傞氬父鏈夊涓搷浣滃寘鍚繖涓鐩稿悓鐨勭粨鏋勩係tate 妯″紡灝嗘瘡涓涓垎鏀斁鍏ヤ竴涓嫭绔嬬殑綾諱腑銆傝繖浣垮緱浣犲彲浠ユ牴鎹璞¤嚜韜殑鎯呭喌灝嗗璞$殑鐘舵佷綔涓轟竴涓璞★紝榪欎竴瀵硅薄鍙互涓嶄緷璧栦簬鍏朵粬瀵硅薄鑰岀嫭绔嬪彉鍖栥?

銆銆涓冦佷紭緙虹偣

銆銆1)浼樼偣: 閬垮厤浜嗕負鍒ゆ柇鐘舵佽屼駭鐢熺殑宸ㄥぇ鐨刬f鎴朿ase璇彞銆?灝嗗璞¤涓轟氦緇欑姸鎬佺被緇存姢鍚庯紝瀵逛簬涓婂眰紼嬪簭鑰岃█錛屼粎闇瑕佺淮鎶ょ姸鎬佷箣闂寸殑杞崲瑙勫垯銆?

銆銆2)浼氬鑷存煇浜涚郴緇熸湁榪囧鐨勫叿浣撶姸鎬佺被銆?

銆銆鍏佸弬鑰?

銆銆http://www.cppblog.com/converse/archive/2006/08/07/10902.html

銆銆http://m.tkk7.com/flying/archive/2006/08/29/66472.html



]]>
decorator(杞?http://m.tkk7.com/vcycyv/archive/2011/02/09/343997.html浜哄湪姹熸箹浜哄湪姹熸箹Wed, 09 Feb 2011 14:58:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/09/343997.htmlhttp://m.tkk7.com/vcycyv/comments/343997.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/09/343997.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/343997.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/343997.html闃呰鍏ㄦ枃

]]>
builder(杞澆)http://m.tkk7.com/vcycyv/archive/2011/02/09/343995.html浜哄湪姹熸箹浜哄湪姹熸箹Wed, 09 Feb 2011 14:45:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/09/343995.htmlhttp://m.tkk7.com/vcycyv/comments/343995.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/09/343995.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/343995.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/343995.html闃呰鍏ㄦ枃

]]>
Mediator(杞澆)http://m.tkk7.com/vcycyv/archive/2011/02/08/343951.html浜哄湪姹熸箹浜哄湪姹熸箹Tue, 08 Feb 2011 09:32:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/08/343951.htmlhttp://m.tkk7.com/vcycyv/comments/343951.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/08/343951.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/343951.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/343951.html涓銆?妯″紡瀹氫箟:
鐢ㄤ竴涓腑浠嬭呭璞℃潵灝佽涓緋誨垪鐨勫璞′氦浜掋備腑浠嬭呬嬌鍚勫璞′笉闇瑕佹樉寮忕殑鐩鎬簰寮曠敤錛屼粠鑰屼嬌鍏惰﹀悎鏉炬暎錛岃屼笖鍙互鐙珛鐨勬敼鍙樹粬浠箣闂寸殑浜や簰銆?br>浜屻?緇撴瀯鍥?br> (鐣?
1) 鎶借薄涓粙鑰咃細瀹氫箟鍚屼簨錛圕olleague錛夊璞″埌涓粙鑰咃紙Mediatior錛夊璞$殑鎺ュ彛錛岄氬父鏄竴涓簨浠舵柟娉曘?br>2) 鍏蜂綋涓粙鑰咃細鍏蜂綋涓粙鑰呭疄鐜版娊璞′腑浠嬭呭0鏄庣殑鏂規硶銆傜煡鏅撴墍鏈夌殑鍏蜂綋鍚屼簨綾伙紝浠庡叿浣撳悓浜嬫帴鏀舵秷鎭悜鍙﹀鐨勫叿浣撳悓浜嬬被鍙戦佸懡浠ゃ?br>3) 鎶借薄鍚屼簨綾伙細瀹氫箟涓粙鑰呭埌鍚屼簨瀵硅薄鐨勬帴鍙o紝鍚屼簨瀵硅薄鍙煡閬撲腑浠嬭呰屼笉鐭ラ亾鍏朵粬鍚屼簨瀵硅薄銆?br>涓夈佷竴涓緥瀛?br>

   1: import java.util.ArrayList;
   2:  
   3: abstract class AbstractMediator{
   4:  public abstract void register(AbstractColleague ac);
   5:  public abstract void ColleagueChanged(AbstractColleague ac);
   6: }
   7: abstract class AbstractColleague{
   8:  private AbstractMediator med;
   9:  public AbstractColleague(AbstractMediator mediator){
  10:   this.med=mediator;
  11:  }
  12:  public abstract void action();
  13:  public void changed(){
  14:   med.ColleagueChanged(this);
  15:  }
  16: }
  17: class ConcreteMediator extends AbstractMediator{
  18:  
  19:  private ArrayList<AbstractColleague> colleagueList=new ArrayList<AbstractColleague>();
  20:  public void register(AbstractColleague ac) {
  21:   colleagueList.add(ac); 
  22:  }
  23:  
  24:  public void ColleagueChanged(AbstractColleague ac) {
  25:   for(int i=0;i<colleagueList.size();i++){
  26:    if(colleagueList.get(i)!=ac){
  27:     colleagueList.get(i).action();
  28:    }
  29:   }
  30:  }
  31: }
  32:  
  33: class ConcreteColleagueA extends AbstractColleague{
  34:     public ConcreteColleagueA(AbstractMediator mediator){
  35:      super(mediator);
  36:      mediator.register(this);
  37:     }
  38:  public void action() {
  39:   System.out.println("AAAAAAAAAAAAAAA");
  40:  }
  41: }
  42: class ConcreteColleagueC extends AbstractColleague{
  43:     public ConcreteColleagueC(AbstractMediator mediator){
  44:      super(mediator);
  45:      mediator.register(this);
  46:     }
  47:  public void action() {
  48:   System.out.println("CCCCCCCCCCCCCCC");
  49:  }
  50: }
  51: class ConcreteColleagueB extends AbstractColleague{
  52:   public ConcreteColleagueB(AbstractMediator mediator){
  53:       super(mediator);
  54:       mediator.register(this);
  55:      }
  56:   public void action() {
  57:    System.out.println("BBBBBBBBBBBBBBB");
  58:   }
  59: }
  60: class test{
  61:  public static void main(String[] args){
  62:  AbstractMediator mediator=new ConcreteMediator();
  63:  AbstractColleague colleagueA=new ConcreteColleagueA(mediator);
  64:  AbstractColleague colleagueB=new ConcreteColleagueB(mediator);
  65:  AbstractColleague colleagueC=new ConcreteColleagueC(mediator);
  66:  colleagueA.changed();
  67:  colleagueB.changed();
  68:  colleagueC.changed();
  69:  }
  70: }
  71:  

浜斻?浼樼己鐐?br>1錛夊噺灝戜簡瀛愮被鐢熸垚Mediator灝嗗師鏈垎甯冧簬澶氫釜瀵硅薄闂寸殑琛屼負闆嗕腑鍦ㄤ竴璧鳳紝鏀瑰彉榪欎簺琛屼負鍙渶鐢熸垚Mediator鐨勫瓙綾誨嵆鍙紝榪欐牱鍚勪釜Colleague綾誨彲琚噸鐢ㄣ?br>2錛夊畠灝嗗悇Colleague瑙h︺侻ediator鏈夊埄浜庡悇Colleague闂寸殑鏉捐﹀悎錛屼綘鍙互鐙珛鐨勬敼鍙樺拰澶嶇敤鍚凜olleague綾誨拰Mediator綾匯?br>3錛夊畠綆鍖栦簡瀵硅薄鍗忚鐢∕ediator鍜屽悇Colleague闂寸殑涓瀵瑰鐨勪氦浜掓潵浠f浛澶氬澶氱殑浜や簰銆備竴瀵瑰鐨勫叧緋繪洿鏄撲簬鐞嗚В銆佺淮鎶ゅ拰鎵╁睍銆?br>4錛夊畠瀵瑰璞″浣曞崗浣滆繘琛屼簡鎶借薄灝嗕腑浠嬩綔涓轟竴涓嫭绔嬬殑姒傚康騫跺皢鍏跺皝瑁呭湪涓涓璞′腑錛屼嬌浣犲皢娉ㄦ剰鍔涗粠瀵硅薄鍚勮嚜鏈韓鐨勮涓鴻漿縐誨埌瀹冧滑涔嬮棿鐨勪氦浜掍笂鏉ャ傝繖鏈夊姪浜庡紕娓呮涓涓郴緇熶腑鐨勫璞℃槸濡備綍浜や簰鐨勩?br>5錛夊畠浣挎帶鍒墮泦涓寲錛屼腑浠嬭呮ā寮忓皢浜や簰鐨勫鏉傛у彉涓轟腑浠嬭呯殑澶嶆潅鎬с傚洜涓轟腑浠嬭呭皝瑁呬簡鍗忚錛屽畠鍙兘鍙樺緱姣斾換涓涓狢olleague閮藉鏉傘傝繖鍙兘浣垮緱涓粙鑰呰嚜韜垚涓轟竴涓毦浜庣淮鎶ょ殑搴炵劧澶х墿銆?br>鍏?閫傜敤鎬?br>1錛変竴緇勫璞′互瀹氫箟鑹ソ浣嗘槸澶嶆潅鐨勬柟寮忚繘琛岄氫俊銆備駭鐢熺殑鐩鎬簰渚濊禆鍏崇郴緇撴瀯娣蜂貢涓旈毦浠ョ悊瑙c?br>2錛変竴涓璞″紩鐢ㄥ叾浠栧緢澶氬璞″茍涓旂洿鎺ヤ笌榪欎簺瀵硅薄閫氫俊,瀵艱嚧闅句互澶嶇敤璇ュ璞°?/p>

]]>
bridge(杞澆)http://m.tkk7.com/vcycyv/archive/2011/02/08/343950.html浜哄湪姹熸箹浜哄湪姹熸箹Tue, 08 Feb 2011 09:19:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/08/343950.htmlhttp://m.tkk7.com/vcycyv/comments/343950.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/08/343950.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/343950.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/343950.html/** "Implementor" */

interface DrawingAPI {

    public void drawCircle(double x, double y, double radius);

}

/** "ConcreteImplementor" 1/2 */

class DrawingAPI1 implements DrawingAPI {

   public void drawCircle(double x, double y, double radius) {

        System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);

   }

}

/** "ConcreteImplementor" 2/2 */

class DrawingAPI2 implements DrawingAPI {

   public void drawCircle(double x, double y, double radius) { 

        System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);

   }

}

/** "Abstraction" */

interface Shape {

   public void draw();                                            // low-level

   public void resizeByPercentage(double pct);     // high-level

}

/** "Refined Abstraction" */

class CircleShape implements Shape {

   private double x, y, radius;

   private DrawingAPI drawingAPI;

   public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {

       this.x = x;  this.y = y;  this.radius = radius; 

       this.drawingAPI = drawingAPI;

   }

   // low-level i.e. Implementation specific

   public void draw() {

        drawingAPI.drawCircle(x, y, radius);

   }   

   // high-level i.e. Abstraction specific

   public void resizeByPercentage(double pct) {

        radius *= pct;

   }

}

/** "Client" */

class BridgePattern {

   public static void main(String[] args) {

       Shape[] shapes = new Shape[2];

       shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());

       shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());

       for (Shape shape : shapes) {

           shape.resizeByPercentage(2.5);

           shape.draw();

       }

   }

}

 

Bridge閬靛驚鐨勬槸鎺ュ彛闅旂鍘熷垯鐨勫吀鍨嬨傛瘮濡傦紝闅忕潃鏃朵唬鐨勫彂灞曪紝鏋湪涓嶅仠鐨勫彉鍖栨敼榪涖備笌姝ゅ悓鏃舵灙鐨勫瓙寮逛篃鍦ㄦ敼榪涳紝寰堝瀛愬脊璧嬩簣浜嗘柊鍔熻兘錛氭瘮濡備俊鍙峰脊錛屾晳鎻村脊錛岀敋鑷蟲槸鐐稿脊銆傝繖鏍鳳紝鏋槸鎶借薄鐨勫悓鏃訛紝瀛愬脊涔熸槸鎶借薄鐨勩備簩鑰呯粍鍚堣搗鏉ュ彉鎹㈡棤絀楓備粩緇嗘兂涓涓嬨妀ava涓庢ā寮忋嬩腑妗ユ妯″紡鐨勪緥瀛?-椋炴満鍜岄鏈哄埗閫犲晢鏄笉鏄篃鏄繖縐嶇伒媧誨彉鎹㈢殑緇勫悎錛?/p>

]]>
memento(杞澆)http://m.tkk7.com/vcycyv/archive/2011/02/08/343949.html浜哄湪姹熸箹浜哄湪姹熸箹Tue, 08 Feb 2011 08:48:00 GMThttp://m.tkk7.com/vcycyv/archive/2011/02/08/343949.htmlhttp://m.tkk7.com/vcycyv/comments/343949.htmlhttp://m.tkk7.com/vcycyv/archive/2011/02/08/343949.html#Feedback0http://m.tkk7.com/vcycyv/comments/commentRss/343949.htmlhttp://m.tkk7.com/vcycyv/services/trackbacks/343949.html闃呰鍏ㄦ枃

]]>
主站蜘蛛池模板: 久久综合久久综合亚洲| 97se亚洲国产综合自在线| 亚洲精品老司机在线观看| 波多野结衣免费一区视频 | 亚洲成A人片在线播放器| 色天使亚洲综合在线观看| 99久久婷婷国产综合亚洲| 自拍偷区亚洲国内自拍| 亚洲欧洲日产国码久在线| 亚洲AV无码国产精品麻豆天美 | 成年私人影院免费视频网站| 精品国产一区二区三区免费| 国产精品亚洲av色欲三区| 亚洲永久永久永久永久永久精品| 日本a级片免费看| 国产免费无遮挡精品视频 | 国产精品观看在线亚洲人成网| 国产成人 亚洲欧洲| 又粗又长又爽又长黄免费视频 | 久久ww精品w免费人成| 国产成人免费网站| 国产91免费视频| 成年女人看片免费视频播放器| 国产男女性潮高清免费网站| 最新猫咪www免费人成| 国产成人免费A在线视频| 2022中文字字幕久亚洲| 亚洲va中文字幕无码久久| 亚洲日本国产乱码va在线观看| 亚洲一久久久久久久久| 亚洲成a人不卡在线观看| 亚洲熟妇AV日韩熟妇在线| 免费一区二区三区在线视频| 日本视频免费高清一本18| 在线观看AV片永久免费| 亚洲成AV人在线观看网址| 久久久亚洲精品无码| 亚洲中文字幕一二三四区 | 亚洲视频在线观看地址| 亚洲av色福利天堂| 亚洲精品第一综合99久久|