澶栬妯″紡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了澶栬妯″紡相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/des' title='des'>des
details public 瀹屾垚 姣旇緝 src 淇敼 娌′簨 缁勪欢- 澶栬妯″紡
- 妗堜緥
- 妯″紡浠嬬粛
- 妯″紡搴旂敤
- 鎬荤粨
- 澶栬妯″紡
妗堜緥
寮犱笁娌′簨鐨勬椂鍊欏氨鍠滄鍦ㄥ鐪嬬數褰憋紝鑰屼笖杩樻湁鎶曞奖浠紝鎶婄獥甯樹竴鎷夛紝鐢电伅涓€鍏筹紝鐬棿灏辨湁鐢靛奖闄㈢殑鎰熻銆備笅闈㈠氨鐢ㄤ唬鐮佹潵妯℃嫙杩欎竴杩囩▼銆?/p>
1.鏍规嵁闈㈠悜瀵硅薄鐨勬€濇兂锛岃繖閲屽垱寤轰簡鍥涗釜绫伙細
绐楀笜绫伙細
/** * 绐楀笜 */ public class Curtain { public void draw() { System.out.println("鏀句笅绐楀笜"); } }
鐢电伅绫伙細
/** * 鐢电伅 */ public class Lighting { public void close() { System.out.println("鍏抽棴鐢电伅"); } }
鎶曞奖浠被锛?/p>
/** * 鎶曞奖浠? */ public class Projector { public void open() { System.out.println("鎵撳紑鎶曞奖浠?); } }
鐢佃剳绫伙細
/** * 鐢佃剳 */ public class Computer { public void play() { System.out.println("鎾斁鐢靛奖"); } }
2.妯℃嫙鐪嬬數褰憋細
/** * 妯℃嫙鍦ㄥ鐪嬬數褰辩殑杩囩▼ */ public class Main { public static void main(String[] args) { Window window = new Window(); Curtain curtain = new Curtain(); Lighting lighting = new Lighting(); Projector projector = new Projector(); Computer computer = new Computer(); window.close(); curtain.draw(); lighting.close(); projector.open(); computer.play(); } }
3.妯℃嫙缁撴灉锛?/p>
鏀句笅绐楀笜 鍏抽棴鐢电伅 鎵撳紑鎶曞奖浠?鎾斁鐢靛奖
鐢靛奖铏界劧鐪嬩笂浜嗭紝浣嗘槸鎰熻寮€濮嬬殑涓€浜涘垪鎿嶄綔姣旇緝楹荤儲锛屽鏋滄湁璋佽兘澶熶唬鏇垮紶涓夊仛鍏崇獥甯樸€佸叧鐏€佹斁鎶曞奖浠拰寮€鐢佃剳鐨勫姩浣滃氨濂戒簡銆傛瘮濡傝鎷ユ湁涓€涓仴鎺у櫒锛屽彲浠ュ府鍔╂垜浠仛杩欎簺鎿嶄綔銆傝繖涓?璁捐妯″紡涓殑澶栬妯″紡闈炲父鐩镐技锛屼笅闈㈠氨鏉ヤ粙缁嶄竴涓嬪瑙傛ā寮忋€?/p>
妯″紡浠嬬粛
澶栬妯″紡鍙堢О涓?strong>闂ㄩ潰妯″紡锛屽畠鏄竴绉?strong>瀵硅薄缁撴瀯鍨嬫ā寮?/strong>銆傚瑙傛ā寮忥紙Facade锛夛紝鏄寚鎻愪緵涓€涓粺涓€鐨勬帴鍙e幓璁块棶澶氫釜瀛愮郴缁熺殑澶氫釜涓嶅悓鐨勬帴鍙o紝瀹冧负瀛愮郴缁熶腑鐨勪竴缁勬帴鍙f彁渚涗竴涓粺涓€鐨勯珮灞傛帴鍙c€備娇寰楀瓙绯荤粺鏇村鏄撲娇鐢ㄣ€傘€?/p>
瑙掕壊鏋勬垚
- Facade锛堝瑙傝鑹诧級锛氬湪瀹㈡埛绔彲浠ヨ皟鐢ㄥ畠鐨勬柟娉曪紝鍦ㄥ瑙傝鑹蹭腑鍙互鐭ラ亾鐩稿叧鐨勶紙涓€涓垨鑰呭涓級瀛愮郴缁熺殑鍔熻兘鍜岃矗浠伙紱鍦ㄦ甯告儏鍐典笅锛屽畠灏嗘墍鏈変粠瀹㈡埛绔彂鏉ョ殑璇锋眰濮旀淳鍒扮浉搴旂殑瀛愮郴缁熷幓锛屼紶閫掔粰鐩稿簲鐨勫瓙绯荤粺瀵硅薄澶勭悊銆?/li>
- SubSystem锛堝瓙绯荤粺瑙掕壊锛夛細鍦ㄨ蒋浠剁郴缁熶腑鍙互鏈変竴涓垨鑰呭涓瓙绯荤粺瑙掕壊锛屾瘡涓€涓瓙绯荤粺鍙互涓嶆槸涓€涓崟鐙殑绫伙紝鑰屾槸涓€涓被鐨勯泦鍚堬紝瀹冨疄鐜板瓙绯荤粺鐨勫姛鑳斤紱姣忎竴涓瓙绯荤粺閮藉彲浠ヨ瀹㈡埛绔洿鎺ヨ皟鐢紝鎴栬€呰澶栬瑙掕壊璋冪敤锛屽畠澶勭悊鐢卞瑙傜被浼犺繃鏉ョ殑璇锋眰锛涘瓙绯荤粺骞朵笉鐭ラ亾澶栬鐨勫瓨鍦紝瀵逛簬瀛愮郴缁熻€岃█锛屽瑙傝鑹蹭粎浠呮槸鍙﹀涓€涓鎴风鑰屽凡銆?/li>
UML绫诲浘
澶栬妯″紡鐨勪富瑕佺洰鐨勫湪浜庨檷浣庣郴缁熺殑澶嶆潅绋嬪害锛屽湪闈㈠悜瀵硅薄杞欢绯荤粺涓紝绫讳笌绫讳箣闂寸殑鍏崇郴瓒婂锛屼笉鑳借〃绀虹郴缁熻璁″緱瓒婂ソ锛屽弽鑰岃〃绀虹郴缁熶腑绫讳箣闂寸殑鑰﹀悎搴﹀お澶э紝杩欐牱鐨勭郴缁熷湪缁存姢鍜屼慨鏀规椂閮界己涔忕伒娲绘€э紝鍥犱负涓€涓被鐨勬敼鍔ㄤ細瀵艰嚧澶氫釜绫诲彂鐢熷彉鍖栵紝鑰屽瑙傛ā寮忕殑寮曞叆鍦ㄥ緢澶х▼搴︿笂闄嶄綆浜嗙被涓庣被涔嬮棿鐨勮€﹀悎鍏崇郴銆傚紩鍏ュ瑙傛ā寮忎箣鍚庯紝澧炲姞鏂扮殑瀛愮郴缁熸垨鑰呯Щ闄ゅ瓙绯荤粺閮介潪甯告柟渚匡紝瀹㈡埛绫绘棤椤昏繘琛屼慨鏀癸紙鎴栬€呮瀬灏戠殑淇敼锛夛紝鍙渶瑕佸湪澶栬绫讳腑澧炲姞鎴栫Щ闄ゅ瀛愮郴缁熺殑寮曠敤鍗冲彲銆?/p>
澶栬妯″紡涓墍鎸囩殑瀛愮郴缁熸槸涓€涓箍涔夌殑姒傚康锛屽畠鍙互鏄竴涓被銆佷竴涓姛鑳芥ā鍧椼€佺郴缁熺殑涓€涓粍鎴愰儴鍒嗘垨鑰呬竴涓畬鏁寸殑绯荤粺銆備笅闈㈡垜浠氨鏍规嵁杩欎竴鎬濇兂瀵规垜浠殑浠g爜杩涜鏀归€?/p>
/** * 澶栬瑙掕壊绫? */ public class Facade { private Curtain curtain; private Lighting lighting; private Projector projector; private Computer computer; public Facade() { curtain = new Curtain(); lighting = new Lighting(); projector = new Projector(); computer = new Computer(); } public void start() { curtain.draw(); lighting.close(); projector.open(); computer.play(); } }
2.鍥涗釜瀛愮郴缁熻鑹茬被锛氳繖閲屽拰涓婇潰鐨勪唬鐮佹槸涓€鏍风殑锛屽氨涓嶅崰鐢ㄧ┖闂翠簡
3.瀹㈡埛绔娇鐢細
public class Main { public static void main(String[] args) { // 鐩存帴璋冪敤澶栬瑙掕壊鐨勬柟娉曞嵆鍙? Facade facade = new Facade(); facade.start(); } }
4.娴嬭瘯缁撴灉锛?/p>
鏀句笅绐楀笜 鍏抽棴鐏厜 鎵撳紑鎶曞奖浠?鎾斁鐢靛奖
鍙互鐪嬪埌鎴戜滑閫氳繃寮曞叆浜嗗瑙傝鑹蹭娇寰楀彧闇€瑕佽皟鐢ㄥ畠鐨勬柟娉曞嵆鍙互瀹屾垚鍘熸湰闇€瑕佹垜浠嚜宸卞畬鎴愮殑鎿嶄綔锛岀灛闂存劅瑙夊儚鏄浜嗕竴涓櫤鑳藉灞呯殑閬ユ帶鍣ㄣ€傚浜庝娇鐢ㄨ€呮潵璇撮潪甯哥殑鏂逛究銆?/p>
妯″紡搴旂敤
澶栬妯″紡鏄竴绉嶄娇鐢ㄩ鐜囬潪甯搁珮鐨勮璁℃ā寮忥紝瀹冮€氳繃寮曞叆涓€涓瑙傝鑹叉潵绠€鍖栧鎴风涓庡瓙绯荤粺涔嬮棿鐨勪氦浜掞紝涓哄鏉傜殑瀛愮郴缁熻皟鐢ㄦ彁渚涗竴涓粺涓€鐨勫叆鍙o紝浣垮瓙绯荤粺涓庡鎴风鐨勮€﹀悎搴﹂檷浣庯紝涓斿鎴风璋冪敤闈炲父鏂逛究銆傚瑙傛ā寮忓苟涓嶇粰绯荤粺澧炲姞浠讳綍鏂板姛鑳斤紝瀹冧粎浠呮槸绠€鍖栬皟鐢ㄦ帴鍙c€?/p>
姣斿鎴戜滑甯稿父寮€鍙戠殑缃戦〉椤圭洰锛屽湪椤甸潰涓彁渚涚粰鐢ㄦ埛鍚勭鑿滃崟銆佸鑸爮鍜屾寜閽娇寰楃敤鎴峰彲浠ラ€氳繃鎿嶄綔缃戦〉鏉ヨ揪鍒版搷浣滀笟鍔℃暟鎹殑鐩殑銆備负浜嗚揪鍒拌繖涓€鐩殑锛屾垜浠唴閮ㄧ殑鏈嶅姟鍣ㄧ閫氳繃璋冪敤涓嶅悓鐨勬暟鎹祫婧愭潵鎻愪緵鎿嶄綔锛岃涓嶅畾鍚庡彴杩樹細鏈夊涓瓙绯荤粺鎻愪緵甯姪銆傝繖閲岀殑璁块棶鐨勫悗鍙版湇鍔″櫒灏卞彲浠ョ湅浣滄槸涓€涓瑙傝鑹诧紝瀹冪殑鍐呴儴璋冪敤浜嗗涓瓙绯荤粺鏉ユ彁渚涘姛鑳姐€?/p>
杩樻湁鎴戜滑甯哥敤鐨勬棩蹇楁鏋讹紝涓€鑸垜浠兘浼氬紩鍏LF4J锛圫imple Logging Facade for Java锛岀缉鍐橲LF4J锛夌炕璇戣繃鏉ュ氨鏄疛ava绠€鏄撴棩蹇楅棬闈紝浠庡懡鍚嶄笂鏉ョ湅灏辩煡閬撳畠涓庨棬闈㈡ā寮忕殑鍏宠仈銆傛垜浠湪浣跨敤鏃堕€氳繃SLF4J涓殑绫伙紝鐒惰€屽湪瀹冪殑鍐呴儴瀹冨彲鑳戒娇鐢ㄧ殑鏄疞og4j銆乴ogback绛夋棩蹇楁鏋躲€?/p>
鎬荤粨
1.涓昏浼樼偣
- 瀹冨瀹㈡埛绔睆钄戒簡瀛愮郴缁熺粍浠讹紝鍑忓皯浜嗗鎴风鎵€闇€澶勭悊鐨勫璞℃暟鐩紝骞朵娇寰楀瓙绯荤粺浣跨敤璧锋潵鏇村姞瀹规槗銆傞€氳繃寮曞叆澶栬妯″紡锛屽鎴风浠g爜灏嗗彉寰楀緢绠€鍗曪紝涓庝箣鍏宠仈鐨勫璞′篃寰堝皯銆?/li>
- 瀹冨疄鐜颁簡瀛愮郴缁熶笌瀹㈡埛绔箣闂寸殑鏉捐€﹀悎鍏崇郴锛岃繖浣垮緱瀛愮郴缁熺殑鍙樺寲涓嶄細褰卞搷鍒拌皟鐢ㄥ畠鐨勫鎴风锛屽彧闇€瑕佽皟鏁村瑙傜被鍗冲彲銆?/li>
- 涓€涓瓙绯荤粺鐨勪慨鏀瑰鍏朵粬瀛愮郴缁熸病鏈変换浣曞奖鍝嶏紝鑰屼笖瀛愮郴缁熷唴閮ㄥ彉鍖栦篃涓嶄細褰卞搷鍒板瑙傚璞°€?/li>
2.涓昏缂虹偣
- 涓嶈兘寰堝ソ鍦伴檺鍒跺鎴风鐩存帴浣跨敤瀛愮郴缁熺被锛屽鏋滃瀹㈡埛绔闂瓙绯荤粺绫诲仛澶鐨勯檺鍒跺垯鍑忓皯浜嗗彲鍙樻€у拰鐏垫椿 鎬с€?/li>
- 濡傛灉璁捐涓嶅綋锛屽鍔犳柊鐨勫瓙绯荤粺鍙兘闇€瑕佷慨鏀瑰瑙傜被鐨勬簮浠g爜锛岃繚鑳屼簡寮€闂師鍒欍€?/li>
3.閫傜敤鍦烘櫙
- 褰撹涓鸿闂竴绯诲垪澶嶆潅鐨勫瓙绯荤粺鎻愪緵涓€涓畝鍗曞叆鍙f椂鍙互浣跨敤澶栬妯″紡銆?/li>
- 瀹㈡埛绔▼搴忎笌澶氫釜瀛愮郴缁熶箣闂村瓨鍦ㄥ緢澶х殑渚濊禆鎬с€傚紩鍏ュ瑙傜被鍙互灏嗗瓙绯荤粺涓庡鎴风瑙h€︼紝浠庤€屾彁楂樺瓙绯荤粺鐨勭嫭绔嬫€у拰鍙Щ妞嶆€с€?/li>
- 鍦ㄥ眰娆″寲缁撴瀯涓紝鍙互浣跨敤澶栬妯″紡瀹氫箟绯荤粺涓瘡涓€灞傜殑鍏ュ彛锛屽眰涓庡眰涔嬮棿涓嶇洿鎺ヤ骇鐢熻仈绯伙紝鑰岄€氳繃澶栬绫诲缓绔嬭仈绯伙紝闄嶄綆灞備箣闂寸殑鑰﹀悎搴︺€?/li>
Log4j銆丩ogback鐨勪娇鐢ㄤ互鍙婃棩蹇楅棬闈㈡ā寮?澶栬妯″紡)
- 澶栬妯″紡
鏈瘒鏂囩珷github浠g爜鍦板潃锛?a href="https://github.com/Phoegel/design-pattern/tree/main/facade">https://github.com/Phoegel/design-pattern/tree/main/facade
杞浇璇疯鏄庡嚭澶勶紝鏈瘒鍗氬鍦板潃锛?a href="https://www.cnblogs.com/phoegel/p/13986745.html">https://www.cnblogs.com/phoegel/p/13986745.html以上是关于澶栬妯″紡的主要内容,如果未能解决你的问题,请参考以下文章