锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 涓銆佸涔犺璁℃ā寮忔渶閲嶈鐨勬槸浜嗚В妯″紡鐨勫簲鐢ㄥ満鏅傜紪紼嬮亣鍒扮壒瀹氬満鏅殑璇濓紝瑕佹湁鎰忚瘑鑱旀兂鍒拌璁℃ā寮忥紝鍝曠粏鑺傚繕浜嗕篃娌″叧緋伙紝緲葷炕涔﹀氨鎵懼埌浜嗐?/p>
浜屻佹彁楂樿璁$殑鎬濇兂銆傚涔犺璁℃ā寮忕殑鏃跺欙紝瑕佷綋浼氭ā寮忕簿濡欎箣澶勶紝褰撴兂鏄庣櫧澶у笀鎬濇兂鍜岃嚜宸辯殑宸窛鐨勬椂鍊欙紝宸窛灝辯緝鐭簡涓鐐瑰効銆?/p>
鏈夌殑妯″紡騫蟲貳鏃犲錛屽簲鐢ㄥ嵈騫挎硾銆傛湁鐨勬ā寮忚璁$簿宸э紝搴旂敤鍦烘櫙鍗翠笉鏄撻亣鍒般傛棤璁哄浣曪紝瑕佷箞鎺屾彙宸ュ叿錛岃涔堝鍒版濇兂錛岄兘鏄敹鑾楓?/p>
鏈閫傚悎綆鍗曟鎷璁℃ā寮忕殑鏄疷ML鍥撅紝鏀墮泦浜嗕竴涓潪甯稿ソ鐨勮祫婧愶紝鍒嗕韓鍦ㄦ垜鐨剆ky drive緗戠粶紜洏涓婏紝鍖呭惈23縐嶈璁℃ā寮忕殑UML鍥?/p>
http://cid-d8b11f9bf86fecfa.office.live.com/self.aspx/.Public/books/designpatternscard.pdf 璁捐妯″紡鍙互鍒嗕負涓夌被錛歜ehavioural, structural, creational behavioural 11 縐?/strong> 1. Memento錛堝蹇樺綍錛?/p>
綺懼閥紼嬪害錛?5鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
memento閫傚悎淇濆瓨/鎭㈠鐘舵佺殑鍦烘櫙銆傚垎涓哄鎺ュ彛鍜岀獎鎺ュ彛銆傚涔犲氨瑕佸紿勬帴鍙o紒 鍏蜂綋璇瘋錛?http://m.tkk7.com/vcycyv/archive/2011/02/08/343949.html 2. Chain of Responsibility(璐d換閾? 綺懼閥紼嬪害錛?3鏄?/p>
搴旂敤騫挎硾錛?3鏄?/p>
閫傚悎鐢ㄥ湪"涓緋誨垪handler”鐨勫満鏅笅銆傚垎涓虹函鍜屼笉綰袱縐嶏紝濂藉儚濂沖鍎夸篃鍙互榪欎箞鍒嗭紵 鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/12/344167.html 3. Observer錛堣瀵熻咃級 鍥犱負java閲屾湁Obserable鍜孫bserver錛?鎵浠ラ氬父鍦╦ava閲屼笉浼氳嚜宸辮璁¤瀵熻呮ā寮忋?/p>
4. Command(鍛戒護妯″紡) 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
command妯″紡鐢ㄦ潵瑙hnvoker鍜宺eceiver. 鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/12/344168.html 5. State(鐘舵佹ā寮? 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
OO緙栫▼濡傛灉鍑虹幇澶氫釜鐩稿悓鐨刬f…else鏉′歡錛屽簲璇ヨ冭檻鐢╯tate妯″紡閲嶆瀯銆倃ork flow鐨勭姸鎬佸櫒涔熸槸鍩轟簬State妯″紡鐨勩?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/09/343998.html 6. Interpreter 綺懼閥紼嬪害錛?N/A 搴旂敤騫挎硾錛?1鏄?/p>
鎹鍙湁鎼滅儲寮曟搸鎵嶇敤寰椾笂榪欎釜妯″紡錛屽弽姝f垜姣忔瀛﹁璁℃ā寮忕殑鏃跺欙紝閮芥妸榪欎釜璺寵繃鍘伙紝灝辨槸涓嶅枩嬈紝鎶辨瓑銆?/p>
7. Strategy錛堢瓥鐣ユā寮忥級 綺懼閥紼嬪害錛?2鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
鐢ㄦ潵灝佽涓嶅悓綆楁硶銆備粠uml鍥句笂鐪嬪拰state妯″紡涓鏍楓?鍥犱負榪欎釜澶畝鍗曚簡錛屾墍浠ユ病璁扮瑪璁幫紝榪欎釜蹇樹笉浜嗐?/p>
8. Iterator(榪唬瀛? Java Collection閮藉彲浠ヨ凱浠o紝鍦╦ava涓婁笉闇瑕佹牸澶栬璁terator銆?/p>
9. Template Method(妯℃澘鏂規硶) 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
瑙佸悕鐭ヤ箟銆傚お綆鍗曚簡錛屽榪囦竴閬嶅氨涓嶄細蹇樸備絾鏄繖涓簲鐢ㄩ潪甯稿箍娉涳紒 10. Mediator(浠茶鑰? 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
鐢ㄦ潵瑙h﹀涓猵eer銆備釜浜鴻寰楄繖涓ā寮忓鍗婁細璁﹎ediator鍗佸垎鑷冭偪錛宮ediator鏈韓闇瑕佹湁寰堟竻鏅扮殑璁捐鎵嶈兘浣跨敤銆傚簲鐢ㄥ満鏅亹鎬曚篃灝戙?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/08/343951.html 11. Visitor(璁塊棶鑰? 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
collection閲屽瓨涓涓被緋葷殑entry鏃訛紝浣跨敤visitor妯″紡鍙互閬垮厤instance of鐨勪嬌鐢ㄣ傛洿閲嶈鐨勬槸鏂逛究鎵╁睍綾葷郴銆?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/13/344194.html Structure 7縐?/strong> 12. adapter(閫傞厤鍣? 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
涓漢瑙夊緱Adaptee鐢辯涓夋柟鎻愪緵鐨勬椂鍊欐墠鍙兘鐢ㄤ笂榪欎釜妯″紡[vcycyv緙栬緫錛氬悗鏉ユ剰璇嗗埌錛岃繖涓瘎璁轟笉瀹㈣銆俛daptee瀹炵幇鏍稿績鍔熻兘錛岄氳繃adapter鏆撮湶澶氱璁塊棶鏂瑰紡鐨勬儏鍐典笅錛宎daptee涔熸槸鑷繁鍐欑殑錛岃屼笉鏄涓夋柟鐨刔銆傜畝鍗曞簲鐢ㄨ寖鍥村張紿勶紝鐣?/p>
13. Proxy(浠g悊) 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
aop鏄唬鐞嗘ā寮忕殑涓縐嶅彂鎸ャ係pring鍜孒ibernate閮藉ぇ閲忎嬌鐢╬roxy銆傚彲浠ュ紩鐢沖瀛DK鐨刣ynamic proxy銆傛ā寮忕畝鍗曪紝鐣ャ?/p>
14. Bridge 綺懼閥紼嬪害錛?5鏄?/p>
搴旂敤騫挎硾錛?3鏄?/p>
Bridge鍦ㄦ妧鏈笂鏄痵trategy鐨勮繘涓姝ュ彂鎸ワ紝浣嗕晶閲嶅湪瀹炵幇涓庢娊璞$殑瑙h︺?/p>
鍏蜂綋瑙侊細 http://m.tkk7.com/vcycyv/archive/2011/02/08/343950.html 15. Composite 綺懼閥紼嬪害錛?4鏄?/p>
搴旂敤騫挎硾錛?3鏄?/p>
閫傜敤浜庢爲鐘剁粨鏋勩?/p>
鍏蜂綋瑙侊細 http://m.tkk7.com/vcycyv/archive/2011/02/13/344209.html 16. Decorator(瑁呴グ) 綺懼閥紼嬪害錛?5鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
鍦╦ava I/O涓箍娉涗嬌鐢ㄣ備負浜嗚В鍐?#8220;綾葷垎鐐?#8221;鐨勯棶棰樸?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/09/343997.html 17. Facade(闂ㄩ潰) 榪炰釜UML鍥鵑兘娌℃湁錛岀畻紲為┈妯″紡錛?/p>
18. Flyweight 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
鍒?#8220;鍗曠函”“澶嶅悎”涓ょ錛屾湰韜寘鍚簡宸ュ巶鏂規硶妯″紡銆備竴緋誨垪瀵硅薄濡傛灉浠栦滑閮芥湁鏌愰儴鍒嗗睘浜庣壒瀹氶泦鍚堬紝灝辮浠栦滑鍏變韓榪欎釜鐗瑰畾闆嗗悎浠ヨ妭鐪佽祫婧愩?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/12/344169.html Creational 6 縐?/strong> 19. Factory Method(宸ュ巶鏂規硶) 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
涓や釜鍙樼錛屼竴涓槸鏍規嵁鍙傛暟鍒涘緩瀵硅薄錛屼竴涓槸factory綾葷郴瀵瑰簲浜у搧綾葷郴銆傞潰鍚戞帴鍙g紪紼嬪湪鍒涘緩妯″紡鐨勪綋鐜般?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/13/344176.html 20. Abstract Factory錛堟娊璞″伐鍘傦級 綺懼閥紼嬪害錛?鏄?/p>
搴旂敤騫挎硾錛?鏄?/p>
鍙湁浜у搧鏈変袱涓被緋葷殑鏃跺欐墠鐢ㄥ緱涓娿?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/15/344390.html 21. Builder 綺懼閥紼嬪害錛?5鏄?/p>
搴旂敤騫挎硾錛?2鏄?/p>
浜у搧鍒嗛儴鍒嗭紝騫朵笖build鐨勮繃紼嬩笉瀹氱殑鏃跺欒冭檻鐢ㄨ繖縐嶆ā寮忋傛槸妯℃澘妯″紡鐨勪竴縐嶅彂鎸?/p>
鍏蜂綋璇瘋錛?/p>
http://m.tkk7.com/vcycyv/archive/2011/02/09/343995.html 22. Prototype(鍘熷瀷) java鏈塩loneable鐨勬敮鎸侊紝鎵浠ラ氬父涓嶉渶瑕佽嚜宸辮璁rototype. java鐢╬rototype鐨勬椂鍊欐敞鎰忔繁澶嶅埗嫻呭鍒剁殑闂銆俻rototype涔嬪鐨勪竴涓夋嫨鏄彁渚涗竴涓猚onstructor鎺ュ彈鏈被鍨嬪璞′綔涓哄弬鏁幫紝榪欎釜鏂瑰紡鏇村姞瀹炵敤錛?/p>
23. singleton(鍗曟? 鍙ゅ吀鐨剆ingleton鍒嗕負鎳掓眽錛岄タ姹変袱縐嶃侸DK1.5涔嬪悗鍙互鐢ㄥ敮涓entry 鐨別num浣滀負singleton鐨勫疄鐜般傚ソ澶勬湁涓ょ偣錛屼竴鏄畝鍗曘備簩鏄笉闇瑕佹牸澶栧鐞唖erializable鐨勬儏鍐?/p>
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. Example interface CarElementVisitor { FactoryMethod鏄竴縐嶅垱寤烘фā寮?瀹冨畾涔変簡涓涓垱寤哄璞$殑鎺ュ彛,浣嗘槸鍗磋瀛愮被鏉ュ喅瀹氬叿浣撳疄渚嬪寲鍝竴涓被.褰撲竴涓被鏃犳硶棰勬枡瑕佸垱寤哄摢縐嶇被鐨勫璞℃垨鏄竴涓被闇瑕佺敱瀛愮被鏉ユ寚瀹氬垱寤虹殑瀵硅薄鏃舵垜浠氨闇瑕佺敤鍒癋actory Method 妯″紡浜?綆鍗曡鏉?Factory Method鍙互鏍規嵁涓嶅悓鐨勬潯浠朵駭鐢熶笉鍚岀殑瀹炰緥,褰撶劧榪欎簺涓嶅悓鐨勫疄渚嬮氬父鏄睘浜庣浉鍚岀殑綾誨瀷,鍏鋒湁鍏卞悓鐨勭埗綾?Factory Method鎶婂垱寤鴻繖浜涘疄渚嬬殑鍏蜂綋榪囩▼灝佽璧鋒潵浜?綆鍖栦簡瀹㈡埛绔殑搴旂敤,涔熸敼鍠勪簡紼嬪簭鐨勬墿灞曟?浣垮緱灝嗘潵鍙互鍋氭渶灝忕殑鏀瑰姩灝卞彲浠ュ姞鍏ユ柊鐨勫緟鍒涘緩鐨勭被. 閫氬父鎴戜滑灝咶actory Method浣滀負涓縐嶆爣鍑嗙殑鍒涘緩瀵硅薄鐨勬柟娉?褰撳彂鐜伴渶瑕佹洿澶氱殑鐏墊椿鎬х殑鏃跺?灝卞紑濮嬭冭檻鍚戝叾瀹冨垱寤哄瀷妯″紡杞寲 綆鍗曞垎鏋? 鍥?鏄疐actory Method 妯″紡鐨勭粨鏋勫浘,榪欓噷鎻愪緵浜嗕竴浜涙湳璇?璁╂垜浠彲浠ヨ繘琛屾洿鏂逛究鐨勬弿榪? 鐢辨鍙互娓呮鐨勭湅鍑鴻繖鏍風殑騫寵瀵瑰簲鍏崇郴: Product <====> Creator ; ConreteProduct <====> ConreteCreator 鎶借薄浜у搧瀵瑰簲鎶借薄鍒涘緩鍣?鍏蜂綋浜у搧瀵瑰簲鍏蜂綋鍒涘緩鍣?榪欐牱鍋氱殑濂藉鏄粈涔堝憿?涓轟粈涔堟垜浠笉鐩存帴鐢ㄥ叿浣撶殑浜у搧鍜屽叿浣撶殑鍒涘緩鍣ㄥ畬鎴愰渶姹傚憿?瀹為檯涓婃垜浠篃鍙互榪欐牱鍋?浣嗛氳繃Factory Method妯″紡鏉ュ畬鎴?瀹㈡埛(client)鍙渶寮曠敤鎶借薄鐨凱roduct鍜孋reater,瀵瑰叿浣撶殑ConcreteProduct鍜孋oncreteCreator鍙互姣笉鍏沖績,榪欐牱鍋氭垜浠彲浠ヨ幏寰楅澶栫殑濂藉: 鐜板湪,璇峰啀鍥炲ご鐪嬬湅鍩烘湰姒傚康涓殑閭f璇?寮濮嬩篃璁歌寰楃敓娑╅毦鎳?鐜板湪鏄笉鏄凡緇忔槑鏈楀寲浜嗗緢澶? 涓嬮潰璁╂垜浠湅鐪嬪湪 Java 涓浣曞疄鐜癋actory Method妯″紡,榪涗竴姝ュ姞娣卞瀹冪殑璁よ瘑. 鍏蜂綋瀹炴柦 鍏堣鏄庝竴鐐?鐢‵actory Method妯″紡鍒涘緩瀵硅薄騫朵笉涓瀹氫細璁╂垜浠殑浠g爜鏇寸煭,瀹炰簨涓婂線寰鏇撮暱,鎴戜滑涔熶嬌鐢ㄤ簡鏇村鐨勭被,鐪熸鐨勭洰鐨勫湪浜庤繖鏍峰彲浠ョ伒媧葷殑,鏈夊脊鎬х殑鍒涘緩涓嶇‘瀹氱殑瀵硅薄.鑰屼笖,浠g爜鐨勫彲閲嶇敤鎬ф彁楂樹簡,瀹㈡埛绔殑搴旂敤綆鍖栦簡,瀹㈡埛紼嬪簭鐨勪唬鐮佷細澶уぇ鍑忓皯,鍙樼殑鏇村叿鍙鎬? 1.a 棣栧厛瀹氫箟涓涓娊璞$被Shape,瀹氫箟涓や釜鎶借薄鐨勬柟娉? 1.b 瀹氫箟 Shape鐨勪袱涓瓙綾? Circle, Square,瀹炵幇Shape涓畾涔夌殑鎶借薄鏂規硶 1.c 瀹氫箟鎶借薄鐨勫垱寤哄櫒,anOperation璋冪敤factoryMethod鍒涘緩涓涓璞?騫跺璇ュ璞¤繘琛屼竴緋誨垪鎿嶄綔. 1.d 瀹氫箟涓巆ircle鍜宻quare鐩稿搴旂殑涓や釜鍏蜂綋鍒涘緩鍣–ircleFactory,SquareFactory,瀹炵幇鐖剁被鐨刴ethodFactory鏂規硶 1.e 嫻嬭瘯綾?璇鋒敞鎰忚繖涓鎴風紼嬪簭澶氫箞綆媧?鏃㈡病鏈夌綏鍡︾殑鏉′歡鍒ゆ柇璇彞,涔熸棤闇鍏沖績ConcreteProduct鍜孋oncreteCreator鐨勭粏鑺?鍥犱負榪欓噷鎴戠敤anOperation灝佽浜哖roduct閲岀殑涓や釜鏂規硶,鎵浠ヨ繛Product鐨勫獎瀛愪篃娌$湅瑙?褰撶劧鎶奝roduct閲屾柟娉曠殑鍏蜂綋璋冪敤鏀懼埌瀹㈡埛紼嬪簭涓篃鏄笉閿欑殑). 榪愯緇撴灉濡備笅:
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.a 鎴戜滑鍦ㄧ涓縐嶆柟娉曠殑鍩虹涓婅繘琛屼慨鏀?棣栧厛鑷畾涔変竴涓殑寮傚父,榪欐牱褰撲紶鍏ヤ笉姝g‘鐨勫弬鏁版椂鍙互寰楀埌鏇存槑鏄劇殑鎶ラ敊淇℃伅. 2.b鍘繪帀浜哠hapeFactory鐨勪袱涓瓙綾?鏀逛負鐢盨hapeFactory鐩存帴璐熻矗瀹炰緥鐨勫垱寤? ShapeFactory鑷繁鍙樻垚涓涓叿浣撶殑鍒涘緩鍣?鐩存帴鐢ㄥ弬鏁板寲鐨勬柟娉曞疄鐜癴actoryMethod榪斿洖澶氱瀵硅薄. 2.c 嫻嬭瘯綾?榪欓噷瀹㈡埛绔繀欏繪寚瀹氬弬鏁版潵鍐沖畾鍏蜂綋鍒涘緩鍝釜綾?榪欎釜渚嬪瓙閲岀殑anOperation鏄潤鎬佸嚱鏁?鍙互鐩存帴寮曠敤. 榪愯緇撴灉濡備笅: 鏈夌殑鏃跺欐垜浠細鎶奀oncreteProduct鐨勫疄渚嬩紶緇欏垱寤哄櫒浣滀負鍙傛暟,榪欑鎯呭喌涓?濡傛灉鍦ㄥ垱寤哄櫒閲屽畬鎴愬垱寤鴻繃紼?灝卞繀欏誨垽鏂弬鏁扮殑鍏蜂綋綾誨瀷(鐢╥nstanceof),鐒跺悗鎵嶈兘浜х敓鐩稿簲鐨勫疄渚?閭d箞姣旇緝濂界殑鍋氭硶鏄埄鐢↗ava鐨勫姩鎬佽杞芥満鍒舵潵瀹屾垚榪欎歡浜?姣斿:
鎴戜滑寰楀埌涓涓猄hape鐨勫瓙綾籹,浣嗕笉鐭ラ亾鍏蜂綋鏄偅涓瓙綾?灝卞彲浠ュ埄鐢–lass綾昏嚜甯︾殑鏂規硶newInstance()寰楀埌瀹炰緥
return (Shape)s.getClass().newInstance();
榪欑鏂規硶鏈夊叴瓚e緱璇昏呭彲浠ヨ嚜宸卞皾璇?闄愪簬綃囧箙,涓嶅啓鍏蜂綋浠g爜鍑烘潵浜? java tip (http://www.javaworld.com/javaworld/javatips/jw-javatip68.html?page=3) 緇欎簡綾諱技鐨勪緥瀛愶紝澶氫簡Fan浣滀負receiver:
TestCommand.java 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.*; 涓銆丼tate妯″紡瀹氫箟: 銆銆鍏佽涓涓璞″湪鍏剁姸鎬佹敼鍙樻椂錛屾敼鍙樺畠鐨勮涓恒傜湅璧鋒潵瀵硅薄浼間箮淇敼浜嗗畠鐨勭被銆? 銆銆浜屻佹ā寮忚В璇? 銆銆State妯″紡涓昏瑙e喅鐨勬槸鍦ㄥ紑鍙戜腑鏃跺父閬囧埌鐨勬牴鎹笉鍚岀殑鐘舵侀渶瑕佽繘琛屼笉鍚岀殑澶勭悊鎿嶄綔鐨勯棶棰?鑰岃繖鏍風殑闂,澶ч儴鍒嗕漢鏄噰鐢╯witch-case璇彞榪涜澶勭悊鐨?榪欐牱浼氶犳垚涓涓棶棰?鍒嗘敮榪囧,鑰屼笖濡傛灉鍔犲叆涓涓柊鐨勭姸鎬佸氨闇瑕佸鍘熸潵鐨勪唬鐮佽繘琛岀紪璇戙係tate妯″紡閲囩敤浜嗗榪欎簺涓嶅悓鐨勭姸鎬佽繘琛屽皝瑁呯殑鏂瑰紡澶勭悊榪欑被闂,褰撶姸鎬佹敼鍙樼殑鏃跺欒繘琛屽鐞嗙劧鍚庡啀鍒囨崲鍒板彟涓縐嶇姸鎬?涔熷氨鏄鎶婄姸鎬佺殑鍒囨崲璐d換浜ょ粰浜嗗叿浣撶殑鐘舵佺被鍘昏礋璐?鍚屾椂,State妯″紡鍜?Strategy妯″紡鏈夊緢澶氱浉浼肩殑鍦版柟,闇瑕佽鏄庣殑鏄袱鑰呯殑鎬濇兂閮芥槸涓鑷寸殑,鍙笉榪囧皝瑁呯殑涓滆タ涓嶅悓:State妯″紡灝佽鐨勬槸涓嶅悓鐨勭姸鎬?鑰孲tategy 妯″紡灝佽鐨勬槸涓嶅悓鐨勭畻娉曘? 銆銆涓夈佺粨鏋勫浘 銆銆State妯″紡緇撴瀯鍥懼涓? 銆銆鍥涖佹庝箞浣跨敤錛? 銆銆1錛夊畾涔変竴涓猄tate鎺ュ彛錛屾帴鍙d腑鏈変竴涓粺涓鐨勬柟娉曪紝鐢ㄤ互灝佽涓涓壒瀹氱姸鎬佹墍瀵瑰簲鐨勮涓恒? 銆銆2錛夊畾涔夊叿浣撲笉鍚岀姸鎬佺被ConcreteSate瀹炵幇State鎺ュ彛銆? 銆銆3錛夋瘡涓涓姸鎬佺被閮藉疄鐜扮幆澧冿紙Context錛変竴涓姸鎬佹墍瀵瑰簲鐨勮涓恒? 銆銆4錛夊畾涔変竴涓姸鎬佺鐞嗗櫒Context. 銆銆浜斻佷竴涓緥瀛?/p> 杈撳嚭緇撴灉:
銆銆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
]]> 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: }
]]>
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.
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 妯″紡緇撴瀯
abstract class Shape {
// 鍕劇敾shape
public abstract void draw();
// 鎿﹀幓 shape
public abstract void erase();
public String name;
public Shape(String aName){
name = aName;
}
}
// 鍦嗗艦瀛愮被
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);
}
}
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();
}
}
// 瀹氫箟榪斿洖 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)");
}
}
class Main {
public static void main(String[] args){
ShapeFactory sf1 = new SquareFactory();
ShapeFactory sf2 = new CircleFactory();
sf1.anOperation("Shape one");
sf2.anOperation("Shape two");
}
}
class NoThisShape extends Exception {
public NoThisShape(String aName) {
super(aName);
}
}
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();
}
}
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)
]]>
]]>/*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.");
}
}
}
=====================
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 ( );
}
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 );
}
}
]]> 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: }
]]>
]]>
]]>
鐢ㄤ竴涓腑浠嬭呭璞℃潵灝佽涓緋誨垪鐨勫璞′氦浜掋備腑浠嬭呬嬌鍚勫璞′笉闇瑕佹樉寮忕殑鐩鎬簰寮曠敤錛屼粠鑰屼嬌鍏惰﹀悎鏉炬暎錛岃屼笖鍙互鐙珛鐨勬敼鍙樹粬浠箣闂寸殑浜や簰銆?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>
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>