Posted java杩涢樁鏋舵瀯甯?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
澶у濂斤紝鎴戞槸楂樺喎灏辨槸鑼冨効锛屽ソ涔呬笉瑙併€傝繃浜嗕竴涓厜妫嶈妭锛屼汉鐩稿綋鎳掓暎锛屼竴鏃舵病鍟ョ伒鎰燂紝鏂囩珷鏇存柊閫熷害杈冩參锛屾姳姝夈€傪煒婁粖澶╂垜浠户缁潵鑱婅璁℃ā寮忚繖涓瘽棰樸€傚墠闈㈠凡缁忚杩囧嚑涓ā寮忥紝濡傛灉娌℃湁闃呰杩囩殑鏈嬪弸鍙互鍥為【涓€涓嬨€?/p>
鍓嶆枃鍥為【 馃憠銆愬師鍒涖€?/span> 馃憠銆愬師鍒涖€?/span> 馃憠銆愬師鍒涖€?/span> 馃憠 閭d箞锛屼粖澶╂垜浠鏉ヨ亰鐨勬槸閫傞厤鍣ㄦā寮?/strong>锛岃繖涓ā寮忓拰涓婄瘒璁茶繃鐨勪唬鐞嗘ā寮忎竴鏍凤紝涔熸槸灞炰簬GOF23鐨?span>缁撴瀯鍨嬫ā寮?/strong>涓殑涓€涓ā寮忋€?/p>
寮曞瓙
鉂?span>浣曚负閫傞厤鍣紵 寰堢粡鍏哥殑涓€涓緥瀛愶紝灏辨槸鎻掑骇锛屽涓嬪浘锛?/p>
鍋囪锛屼竴鍙扮數鑴戦渶瑕佺敤鐢碉紝浠栫殑鎻掑ご鏄袱椤规彃澶达紝浣嗘槸鍛紝鎻掑骇鍗村彧鏈変笁椤规彃瀛旂殑銆傛€庝箞鍔烇紵鎻掑骇灏卞湪閭i噷锛屾槸涓嶅彲鑳芥敼鍙樼殑銆傛垜浠€讳笉鑳戒负浜嗙數鑴戠敤涓數鎶婃暣涓數鑴戞崲浜嗗惂锛熶笉鐜板疄锛侀偅鎴戜滑甯哥敤鐨勫仛娉曞氨鏄拱涓€涓浆鎺ュご锛屼竴闈㈡槸涓ら」鎻掑瓟锛屼竴闈㈡槸涓夐」鎻掑瓟銆傝繖鏍峰氨寰堝ソ鐨勮В鍐充簡杩欎釜闂銆?/p>
杩欎釜鐢熸椿涓父瑙佺殑鍦烘櫙灏辨槸缁忓吀鐨勯€傞厤鍣ㄦā寮忕殑浣撶幇銆?/p>
閭g幇鍦ㄨ鎴戠敤浠g爜鏉ュ疄鐜颁竴涓嬶紝濡備笅锛?/p>
鐜板湪鐢佃剳杩愯闇€瑕佷娇鐢ㄧ數锛岃鐧戒簡锛?code class="mq-53">Computer鎴栬€咃紙瀹炵幇绫伙級鐨?code class="mq-54">run()闇€瑕佽皟鐢?code class="mq-55">Dominate鐨?code class="mq-56">cost()鏂规硶锛屾€庝箞鍔烇紵浣犲彲鑳戒細濡備笅瀹炵幇锛?/p>
涓婇潰浠g爜涓殑 涓婇潰杩欐牱鐨勬柟寮忓緢濂界殑瑙e喅浜嗗洜涓烘彃瀛斾笉鍏煎鑰屽鑷寸殑闂銆傝繖绉嶆柟寮忔湁浠€涔堥棶棰樺悧锛?/p>
鎴戜滑鐭ラ亾锛孞ava鏄崟缁ф壙鐨勶紝涓婇潰閲囩敤缁ф壙鏂瑰紡鏉ヨ揪鍒拌皟鐢?code class="mq-86">Dominate鐨?code class="mq-87">cost()鏂规硶鐨勭洰鐨勶紝杩欐牱涔熷氨鎰忓懗鐫€ 鏇村叧閿殑鏄紝杩樿寰楃涓€澶╄杩囩殑鍑犲ぇ璁捐鍘熷垯鍚楋紵杩欎釜鍦版柟灏卞張娑夊強鍒颁簡涓€涓璁″師鍒欙紝鍙仛缁勫悎鑱氬悎澶嶇敤鍘熷垯銆?/strong> 缁勫悎鑱氬悎澶嶇敤鍘熷垯鎻愬€★紝杞欢澶嶇敤浼樺厛浣跨敤缁勫悎鎴栬€呰仛鍚堝叧绯诲鐢紝灏戠敤缁ф壙鍏崇郴澶嶇敤銆?/p>
涓轰粈涔堝憿锛?/p>
杩欒竟鎴戜笉鐗瑰埆灞曞紑鍏充簬缁勫悎鍜岀户鎵跨殑闃愯堪銆備竴鑸潵璇达紝缁ф壙鐨勬柟寮忕敤浜庣湡鐨勬槸鍏锋湁鈥淚s锛岮鈥濈殑鐖跺瓙鍏崇郴銆傝€屾垜浠殑渚嬪瓙涓紝鎴戜滑鐨勯€傞厤鍣ㄥ彧鏄兂鍘昏皟鐢ㄦ彃搴х殑鐢ㄧ數閫昏緫锛屼絾瀹冩湰韬彲骞朵笉灞炰簬鎻掑骇鍝︼紵鑰屼笖缁ф壙鍏崇郴浼氬鑷寸被鍜岀被涔嬮棿鑰﹀悎鎬у澶э紝涓嶅埄浜庣被鐨勬墿灞曘€佸鐢ㄥ拰缁存姢銆?/p>
鍏跺疄绛変綘鐪嬪埌鍚庨潰鐨勪竴浜涙ā寮忥紝浣犱細瀵规鏇存湁浣撲細锛屽湪寰堝鍦哄悎涓嬶紝鐢ㄧ粍鍚堟柟寮忔墿灞曡璁★紝浼氭瘮缁ф壙鏂瑰紡鐏垫椿寰堝锛岀敋鑷冲ソ澶氭ā寮忓氨鏄敤缁勫悎妯″紡鏉ヨВ鍐崇户鎵挎柟寮忎笉鏄撴墿灞曠殑寮婄梾銆?/p>
鎵€浠ヤ笂闈緥瀛愶紝鎴戜滑鍙互浣跨敤缁勫悎鏂瑰紡鍔犱互鏀归€狅紝濡備笅锛?/p>
杩欐牱灏卞緢杞绘澗鐨勮В鍐充簡杩欎釜闂銆?/p>
鍋氫釜灏忔€荤粨锛?span>閫傞厤鍣ㄦā寮?/strong>灏辨槸瑙e喅杩欐牱涓€绉嶅満鏅紝瀹㈡埛绔渶瑕侀€氳繃涓€绉嶆柟寮忥紙鎴栬€呮帴鍙h鑼冿級鍘昏闂垨鑰呬娇鐢ㄥ彟涓€涓凡缁忓瓨鍦ㄧ殑鎺ュ彛锛屼絾鏄敱浜庡悇绫诲師鍥狅紝娌℃湁鍔炴硶鐩存帴鍘昏皟鐢ㄣ€傝繖涓椂鍊欏彲浠ユ彁渚涗竴涓嬩腑闂村獟浠嬩綔涓洪€傞厤鍣ㄥ璞★紝鍦ㄥ叾鍐呴儴灏嗘帴鍙e仛涓€涓嬭浆鎹紝鐒跺悗瀹㈡埛绔氨鍙互椤哄埄鐨勯€氳繃璁块棶閫傞厤鍣ㄧ殑鏂瑰紡鍘昏闂洰鏍囨帴鍙d簡銆?/p>
閫傞厤鍣ㄦā寮忕殑瀹炵幇鏈変袱绉嶏紝灏辨槸涓婇潰灞曠ず鐨勪袱绉嶏紝涓€绉嶆槸閫氳繃缁ф壙鏂瑰紡锛屽彟涓€绉嶆槸閫氳繃缁勫悎鏂瑰紡瀹炵幇鐨勩€傝€冭檻鍒癑ava鐨勫崟缁ф壙鐗圭偣锛屽姞涓婄粍鍚堣仛鍚堝鐢ㄨ璁″師鍒欙紝涓€鑸兘浼氶噰鐢ㄧ粍鍚堟柟寮忓疄鐜般€?/p>
閫傞厤鍣ㄦā寮忕殑浼樼偣涔熷緢鏄庢樉锛屽畠浣垮緱鍦ㄤ笉闇€瑕佷慨鏀瑰凡鏈変唬鐮佺殑鍓嶆彁涓嬶紝鐩存帴閲嶇敤鐜版湁鐨勭被搴撲唬鐮併€傝€屼笖瀹炵幇璧锋潵寰堢畝鍗曪紝涔熷緢瀹规槗鐞嗚В銆傚彟澶栧浜庡鎴风鑰岃█锛岄€傞厤鍣ㄨ繖绉嶅吋瀹硅浆鎹笉鍚屾帴鍙g殑鏂瑰紡鏄湪鍏跺唴閮ㄥ疄鐜扮殑锛屽浜庡鎴风鑰岃█鏄畬鍏ㄩ€忔槑鐨勶紝瀹㈡埛绔棤闇€浜嗚В鐩爣鎺ュ彛鐨勭粏鑺傦紝鍙渶瑕佹寜鐓у綋鍓嶆帴鍙g殑瑙勮寖鍘昏皟鐢ㄥ嵆鍙€?/p>
閫傞厤鍣ㄥ湪瀹為檯寮€鍙戜腑搴旂敤涔熸槸鐩稿綋骞挎硾銆備笅闈㈡垜涓惧嚑涓緢鍏稿瀷浣跨敤閫傞厤鍣ㄦā寮忕殑渚嬪瓙鏉ヨ鏄庯紝鐩镐俊澶у鐪嬪畬鍚庝細鏈夋洿鍔犳竻鏅扮殑璁よ瘑銆?/p>
棣栧厛鎴戝厛璁蹭竴涓湁鐐圭壒娈婄殑瀹為檯搴旂敤鍦烘櫙锛岀浉淇″ぇ瀹堕兘鏇剧粡閬囧埌杩囥€?/p>
鍋囪鐜板湪鏈変竴涓狝鎺ュ彛锛岃繖涓帴鍙i噷闈㈠畾涔変簡闈炲父澶氱殑鎺ュ彛鏂规硶 杩欎釜鏃跺€欙紝鏈変竴绉嶆瘮杈冪壒娈婄殑閫傞厤鍣ㄦā寮忥紝鍙?span>榛樿閫傞厤鍣ㄦā寮?/strong>鏉ヨВ鍐宠繖涓棶棰樸€?/p>
鎬庝箞瑙e喅鍛紵 鍦ㄦ帴鍙g殑鐩存帴涓嬪眰锛屽畾涔変竴涓娊璞″眰浣滀负閫傞厤鍣紝灏嗘帴鍙g殑鎵€鏈夋柟娉曞叏閮ㄧ┖瀹炵幇銆傝€屾墍鏈夌殑鍏蜂綋瀹炵幇绫讳笉浼氬啀鐩存帴瀹炵幇椤跺眰鎺ュ彛浜嗭紝鑰屼笖鍘昏窡杩欎釜鎶借薄灞傛墦浜ら亾锛岃繖鏍风殑璇濓紝姣忎釜鍏蜂綋瀹炵幇绫诲彧闇€瑕佸疄鐜拌嚜宸卞叧娉ㄧ殑鏂规硶鍗冲彲銆傚涓嬶紝 JDK IO杞崲娴佺殑瀹炵幇
JDK鐨処O API涓浆鎹㈡祦 鍙﹀锛屽紑婧愭鏋?code class="mq-153">Spring銆?code class="mq-154">Mybatis涓€傞厤鍣ㄦā寮忕殑搴旂敤涓嶈鍏舵暟銆?/p>
SpringMVC涓紝 杩欏叾涓殑鍏抽敭灏辨槸鍦ㄤ簬 鍙﹀锛屽湪SpringMVC鍦ㄥ疄鐜板叾 鍚屾牱浣滀负浼樼鐨勫紑婧愭鏋禡ybatis涓€傞厤鍣ㄦā寮忎篃搴旂敤鐨勯潪甯稿箍锛屾渶鍏稿瀷鐨勫氨鏄疢ybatis鐨勬棩璁版ā鍧椾簡銆備紬鎵€鍛ㄧ煡锛孧ybatis鏀寔log4j绛夊悇澶у紑婧愭棩蹇楁鏋讹紝閭d箞鍚勫ぇ寮€婧愭棩蹇楁鏋跺簳灞傚疄鐜颁簲鑺卞叓闂紝浠栧張鏄€庝箞鍋氬埌缁熶竴鎶借薄鐨勫憿锛熷叾瀹炲拰涓婇潰SpringgMVC鐨?code class="mq-185">HandlerAdaptor鐨勬€濊矾鏄竴鏍风殑銆傜涓€鏂戣€岀煡鍏ㄨ惫鍚э紝鎶€鏈渶閲嶈鐨勫氨鏄妇涓€鍙嶄笁锛岀伒娲昏繍鐢ㄤ簡銆傞檺浜庣瘒骞咃紝杩欎袱涓殑鍘熺悊鎴戣繖杈瑰氨涓嶅搴熻瘽浜嗭紝澶у濡傛灉鏈夊叴瓒e彲浠ュ幓闃呰涓€涓嬪叾婧愮爜瀹炵幇銆?/p>
濂戒簡锛屼粖澶╁叧浜?span>閫傞厤鍣ㄦā寮?/strong>鐨勬妧鏈垎浜氨鍒版缁撴潫锛屼笅涓€绡囨垜浼氬垎浜彟涓€涓璁℃ā寮忊€斺€?span>瑁呴グ鑰呮ā寮?/strong>锛屼竴璧风户缁帰璁ㄨ璁℃ā寮忕殑濂ョ銆傚挶浠笉瑙佷笉鏁c€傪煒婐煈?/p>
馃憠
馃憠銆愬師鍒涖€?/span>//涓夐」鎻掑骇
public class Dominate{
public void cost(){
... //鐢ㄧ數閫昏緫
}
}
//鐢佃剳
public interface Computer{
void run();
}public class Adaptor implements Computer extends Dominate{
public void run(){
cost();
}
}Adaptor
鎻愪緵浜嗕竴涓腑闂寸殑閫傞厤鍣ㄨ鑹诧紝鍥犱负瑕佷互Computer
鐨勬柟寮忓幓浣跨敤锛屾墍浠ラ渶瑕佸疄鐜?code class="mq-75">Computer鎺ュ彛锛屽洜涓哄叾run()
鏂规硶闇€瑕佷娇鐢ㄥ埌Dominate
鐨?code class="mq-78">cost()閫昏緫锛屾墍浠ワ紝閫氳繃缁ф壙Dominate
鏂瑰紡灏卞彲浠ュ疄鐜拌繖涓渶姹傘€傝繖鏍蜂箣鍚庯紝鎴戦渶瑕佷娇鐢ㄧ數鑴戯紝鎴戜笉鍦ㄧ洿鎺ュ幓璋冪敤Computer
鐨?code class="mq-81">run()浜嗭紝鍥犱负杩欎釜閲岄潰娌℃湁鐢碉紝鎴戦€氳繃璋冪敤Adaptor
鐨?code class="mq-83">run()鏂规硶鍘讳娇鐢ㄧ數鑴戝氨鍙互浜嗐€?/p>
Adaptor
浠ュ悗鏃犳硶鍐嶇户鎵垮叾瀹冪殑绫绘潵鎵╁睍锛岃繖鎴栧鎴栧皯浼氬奖鍝嶅埌浠ュ悗鐨勬墿灞曟€с€?/p>
public class Adaptor implements Computer{
private Dominate dominate;
//鐪佺暐鏋勯€犲櫒
public void run(){
dominate.cost();
}
}閫傞厤鍣ㄦā寮?/span>
瀹為檯搴旂敤鍦烘櫙
榛樿閫傞厤鍣?/h3>
m1()
锛?code class="mq-126">m2()锛?code class="mq-127">m3()......閽堝涓嶅悓鍔熻兘鐐癸紝鐒跺悗鍏跺疄鐜扮被涔熸槸鏍规嵁涓嶅悓鐨勫姛鑳藉A鎺ュ彛杩涜瀹炵幇锛屼絾鏄瘡涓疄鐜扮被鍙兘鍙渶瑕佸疄鐜板叾涓嚜宸卞叧娉ㄧ殑閭d釜鏂规硶鍗冲彲锛岃嚦浜庡叾浣欐棤鍏崇殑鏂规硶鎴戝苟涓嶆兂瀹炵幇銆備絾鏄疛ava璇硶闄愬埗锛屽鏋滀竴涓疄浣撶被瑕佸疄鐜颁竴涓帴鍙o紝蹇呴』闇€瑕佸疄鐜板叾鎵€鏈夋娊璞℃柟娉曘€傝繖涔熷氨鎰忓懗鐫€浠栦細灏嗚嚜宸卞叧娉ㄧ殑閭d釜鏂规硶瀹炵幇锛岃€岃嚦浜庡叾浣欎笉鍏冲績閮藉彧鑳界┖瀹炵幇銆傝繖鏍风殑閫犳垚浜嗘瀬澶х殑浠g爜鍐椾綑锛岃€屼笖杩欎簺鍐椾綑鐨勪唬鐮佹鏃犱紭闆呭彲瑷€锛岄櫎浜嗗崰鐢ㄧ┖闂村拰缇庤涓嶈捣浠讳綍浣滅敤銆傚涓嬪浘锛?/p>
InputStreamReader
鍜?code class="mq-138">OutputStreamWriter涔熸槸搴旂敤浜嗛€傞厤鍣ㄦā寮忥紝瀹炵幇浜嗙敱瀛楄妭娴佸埌瀛楃娴佺殑杞崲锛屼笅闈㈡垜绠€鍗曠殑甯﹀ぇ瀹舵潵鐪嬬湅鍏跺疄鐜般€?/p>
OutputStreamWriter
缁ф壙浜?code class="mq-141">Writer锛堢洰鏍囨帴鍙o級锛屽苟涓旈噸鍐欎簡Writer
鎵€鏈夋牳蹇冪殑鏂规硶銆備絾鏄垜浠幇鍦ㄥ彧鑳芥彁渚涗竴涓瓧鑺傛祦OutputStream
绫诲瀷锛?code class="mq-144">OutputStreamWriter鍐呬細灏嗗叾灏佽鎴愪负StreamEncoder
瀵硅薄锛屽苟涓旇缃负OutputStreamWriter
鐨勫睘鎬с€傝嚦浜庡皝瑁?code class="mq-147">StreamEncoder瀵硅薄鏄洜涓鸿繕闇€瑕佽€冭檻瀛楃闆嗙紪鐮佺殑闂锛岃繖涓笉鏄噸鐐广€傜劧鍚庢鏃讹紝瀹㈡埛绔幓璁块棶OutputStreamWriter
鐨?code class="mq-149">write()绛夋柟娉曟椂锛屼細灏嗗叾濮旀墭缁?code class="mq-150">StreamEncoder瀵硅薄鐨勭浉鍏虫柟娉曟墽琛岋紝浠庤€屽疄鐜扮敱瀛楄妭娴佹搷浣滃埌瀛楃娴佹搷浣滅殑杞崲銆?/p>
寮€婧愭鏋朵腑鐨勮繍鐢?/h3>
HandlerAdaptor
灏辨槸涓€涓吀鍨嬩緥瀛愩€傚鏋滄湁娣卞叆浜嗚В杩嘢pringMVC鍘熺悊婧愮爜鐨勬湅鍙嬪簲璇ヤ細鐭ラ亾锛孲pringMVC涓墍鏈夌殑璇锋眰浼氳鍓嶇鎺у埗鍣?code class="mq-157">DispatcherServlet杞彂缁欐绾ф帶鍒跺櫒杩涜鍏蜂綋鐨勮姹傚鐞嗭紝SpringMVC灏嗗叾娆$骇鎺у埗鍣ㄩ兘绉颁箣涓?code class="mq-158">Handler锛屾垜浠钩鏃剁粷澶у鏁颁娇鐢ㄧ殑閮芥槸Controller
灏辨槸鍏朵腑涔嬩竴銆傚緢澶氫汉鍙兘浼氳涓烘绾ф帶鍒跺櫒鍙湁Controller
锛屽叾瀹?code class="mq-161">SpringMVC鐞嗚涓婃敮鎸佷换鎰忓绉嶆帶鍒跺櫒锛屽寘鎷琒truts2涓殑Action
绛夈€傞偅涔堥棶棰樻潵浜嗭紝鍚勭被娆$骇鎺у埗鍣ㄥ疄鐜板師鐞嗕簲鑺卞叓闂紝濡備笅鍥撅紝SpringMVC濡備綍灏嗗叾缁熶竴鎶借薄鍛紵HandlerAdaptor
杩欎釜缁勪欢锛屽惉鍚嶅瓧灏辩煡閬撹繖涓娇鐢ㄤ簡閫傞厤鍣ㄦā寮忋€傞偅浠栨槸鎬庝箞瀹炵幇鐨勫憿锛熸垜甯﹀ぇ瀹剁畝鍗曠湅涓€涓婼pringMVC鐨勫疄鐜般€?code class="mq-167">HandlerAdapter涓富瑕佹湁涓や釜鏂规硶锛?code class="mq-168">supports()鍜?code class="mq-169">handle()銆傚墠鑰呬細閫氳繃浼犲叆涓€涓叿浣撶殑Handler
瀹炰緥杩涜鍒ゆ柇鍏剁被鍨嬶紝纭畾褰撳墠鐨?code class="mq-171">Handler鏄惁鍙互琚玈pringMVC鏀寔澶勭悊銆傚鏋滃彲浠ワ紝浼氶€氳繃handle()
澶勭悊璇锋眰骞惰繑鍥?code class="mq-173">ModelAndView瀹炰緥杩涜鍚庣画瑙嗗浘瑙f瀽杩斿洖绛夊伐浣溿€傝繖鏍?code class="mq-174">DispatcherServlet鍦ㄥ仛璇锋眰杞彂鐨勬椂鍊欏氨涓嶉渶瑕佽€冭檻姣忎竴涓叿浣撶殑Handler
鐨勫疄鐜颁簡锛屼粬鍙渶瑕佽窡涓€绯诲垪鐨?code class="mq-176">HandlerAdaptor鎵撲氦閬擄紝鑷充簬瀵?code class="mq-177">Handler鐨勭被鍨嬫牎楠岋紝杩樻湁鍏蜂綋鐨勮姹傚鐞嗭紝閮芥槸鍦?code class="mq-178">HandlerAdaptor鍐呴儴瀹炵幇锛屽浜?code class="mq-179">DispatcherServlet锛堟澶勪綔涓哄鎴风锛夊畬鍏ㄦ槸閫忔槑鐨勩€傚涓嬪浘锛?/p>
HandlerInterceptor
鎷︽埅鍣ㄦ椂锛屼娇鐢ㄤ簡HandlerInterceptorAdapter
锛岄【鍚嶆€濅箟涔熸槸鐢ㄥ埌浜嗛€傞厤鍣ㄦā寮忥紝鑰屼笖杩樻槸涓婇潰璁插埌杩囩殑榛樿閫傞厤鍣ㄦā寮忋€?/p>
鈥斺€斺€斺€?nbsp; e n d 鈥斺€斺€斺€?/span>
鍥炲銆?/strong>鏋舵瀯銆戦鍙栨灦鏋勫笀瑙嗛涓€濂椼€?/strong>
鍘熷垱浠庢潵涓嶅紑璧炶祻鏄洜涓烘垜瑙夊緱
浣犵殑鈥?span class="mq-201">鍦ㄧ湅鈥濓紝灏辨槸缁欐垜鏈€濂界殑璧炶祻^_^
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章