java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

璁捐妯″紡锛圖esign Patterns锛?br/>

1                              鈥斺€斿彲澶嶇敤<a href="http://www.cfei.net/archives/tag/%e9%97%88%e3%88%a0%e6%82%9c%e7%80%b5%e7%a1%85%e8%96%84" title="浏览关于“闈㈠悜瀵硅薄”的文章" target="_blank" class="tag_link">闈㈠悜瀵硅薄</a>杞欢鐨勫熀纭€

璁捐妯″紡锛圖esign pattern锛夋槸涓€濂楄鍙嶅浣跨敤銆佸鏁颁汉鐭ユ檽鐨勩€佺粡杩囧垎绫荤紪鐩殑銆佷唬鐮佽璁$粡楠岀殑鎬荤粨銆備娇鐢ㄨ璁℃ā寮忔槸涓轰簡鍙噸鐢ㄤ唬鐮併€佽浠g爜鏇村鏄撹浠栦汉鐞嗚В銆佷繚璇佷唬鐮佸彲闈犳€с€?姣棤鐤戦棶锛岃璁℃ā寮忎簬宸变簬浠栦汉浜庣郴缁熼兘鏄璧㈢殑锛岃璁℃ā寮忎娇浠g爜缂栧埗鐪熸宸ョ▼鍖栵紝璁捐妯″紡鏄蒋浠跺伐绋嬬殑鍩虹煶锛屽鍚屽ぇ鍘︾殑涓€鍧楀潡鐮栫煶涓€鏍枫€傞」鐩腑鍚堢悊鐨勮繍鐢ㄨ璁℃ā寮忓彲浠ュ畬缇庣殑瑙e喅寰堝闂锛屾瘡绉嶆ā寮忓湪鐜板湪涓兘鏈夌浉搴旂殑鍘熺悊鏉ヤ笌涔嬪搴旓紝姣忎竴涓ā寮忔弿杩颁簡涓€涓湪鎴戜滑鍛ㄥ洿涓嶆柇閲嶅鍙戠敓鐨勯棶棰橈紝浠ュ強璇ラ棶棰樼殑鏍稿績瑙e喅鏂规锛岃繖涔熸槸瀹冭兘琚箍娉涘簲鐢ㄧ殑鍘熷洜銆傛湰绔犵郴Java涔嬬編[浠庤彍楦熷埌楂樻墜婕斿彉]绯诲垪涔嬭璁℃ā寮忥紝鎴戜滑浼氫互鐞嗚涓庡疄璺电浉缁撳悎鐨勬柟寮忔潵杩涜鏈珷鐨勫涔狅紝甯屾湜骞垮ぇ绋嬪簭鐖卞ソ鑰咃紝瀛﹀ソ璁捐妯″紡锛屽仛涓€涓紭绉€鐨勮蒋浠跺伐绋嬪笀锛?br/>浼佷笟绾ч」鐩疄鎴?甯︽簮鐮?鍦板潃涓嬭浇
23绉嶆ā寮廽ava瀹炵幇婧愮爜涓嬭浇
涓€銆佽璁℃ā寮忕殑鍒嗙被
鎬讳綋鏉ヨ璁捐妯″紡鍒嗕负涓夊ぇ绫伙細
鍒涘缓鍨嬫ā寮忥紝鍏变簲绉嶏細宸ュ巶鏂规硶妯″紡銆佹娊璞″伐鍘傛ā寮忋€佸崟渚嬫ā寮忋€佸缓閫犺€呮ā寮忋€佸師鍨嬫ā寮忋€?br/>缁撴瀯鍨嬫ā寮忥紝鍏变竷绉嶏細閫傞厤鍣ㄦā寮忋€佽楗板櫒妯″紡銆佷唬鐞嗘ā寮忋€佸瑙傛ā寮忋€佹ˉ鎺ユā寮忋€佺粍鍚堟ā寮忋€佷韩鍏冩ā寮忋€?br/>琛屼负鍨嬫ā寮忥紝鍏卞崄涓€绉嶏細绛栫暐妯″紡銆佹ā鏉挎柟娉曟ā寮忋€佽瀵熻€呮ā寮忋€佽凯浠e瓙妯″紡銆佽矗浠婚摼妯″紡銆佸懡浠ゆā寮忋€佸蹇樺綍妯″紡銆佺姸鎬佹ā寮忋€佽闂€呮ā寮忋€佷腑浠嬭€呮ā寮忋€佽В閲婂櫒妯″紡銆?br/>鍏跺疄杩樻湁涓ょ被锛氬苟鍙戝瀷妯″紡鍜岀嚎绋嬫睜妯″紡銆傜敤涓€涓浘鐗囨潵鏁翠綋鎻忚堪涓€涓嬶細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
浜屻€佽璁℃ā寮忕殑鍏ぇ鍘熷垯
1銆佸紑闂師鍒欙紙Open Close Principle锛?br/>寮€闂師鍒欏氨鏄瀵规墿灞曞紑鏀撅紝瀵逛慨鏀瑰叧闂€傚湪绋嬪簭闇€瑕佽繘琛屾嫇灞曠殑鏃跺€欙紝涓嶈兘鍘讳慨鏀瑰師鏈夌殑浠g爜锛屽疄鐜颁竴涓儹鎻掓嫈鐨勬晥鏋溿€傛墍浠ヤ竴鍙ヨ瘽姒傛嫭灏辨槸锛氫负浜嗕娇绋嬪簭鐨勬墿灞曟€уソ锛屾槗浜庣淮鎶ゅ拰鍗囩骇銆傛兂瑕佽揪鍒拌繖鏍风殑鏁堟灉锛屾垜浠渶瑕佷娇鐢ㄦ帴鍙e拰鎶借薄绫伙紝鍚庨潰鐨勫叿浣撹璁′腑鎴戜滑浼氭彁鍒拌繖鐐广€?br/>2銆侀噷姘忎唬鎹㈠師鍒欙紙Liskov Substitution Principle锛?br/>閲屾皬浠f崲鍘熷垯(Liskov Substitution Principle LSP)闈㈠悜瀵硅薄璁捐鐨勫熀鏈師鍒欎箣涓€銆?閲屾皬浠f崲鍘熷垯涓锛屼换浣曞熀绫诲彲浠ュ嚭鐜扮殑鍦版柟锛屽瓙绫讳竴瀹氬彲浠ュ嚭鐜般€?LSP鏄户鎵垮鐢ㄧ殑鍩虹煶锛屽彧鏈夊綋琛嶇敓绫诲彲浠ユ浛鎹㈡帀鍩虹被锛岃蒋浠跺崟浣嶇殑鍔熻兘涓嶅彈鍒板奖鍝嶆椂锛屽熀绫绘墠鑳界湡姝h澶嶇敤锛岃€岃鐢熺被涔熻兘澶熷湪鍩虹被鐨勫熀纭€涓婂鍔犳柊鐨勮涓恒€傞噷姘忎唬鎹㈠師鍒欐槸瀵光€滃紑-闂€濆師鍒欑殑琛ュ厖銆傚疄鐜扳€滃紑-闂€濆師鍒欑殑鍏抽敭姝ラ灏辨槸鎶借薄鍖栥€傝€屽熀绫讳笌瀛愮被鐨勭户鎵垮叧绯诲氨鏄娊璞″寲鐨勫叿浣撳疄鐜帮紝鎵€浠ラ噷姘忎唬鎹㈠師鍒欐槸瀵瑰疄鐜版娊璞″寲鐨勫叿浣撴楠ょ殑瑙勮寖銆傗€斺€?From Baidu 鐧剧
3銆佷緷璧栧€掕浆鍘熷垯锛圖ependence Inversion Principle锛?br/>杩欎釜鏄紑闂師鍒欑殑鍩虹锛屽叿浣撳唴瀹癸細鐪熷鎺ュ彛缂栫▼锛屼緷璧栦簬鎶借薄鑰屼笉渚濊禆浜庡叿浣撱€?br/>4銆佹帴鍙i殧绂诲師鍒欙紙Interface Segregation Principle锛?br/>杩欎釜鍘熷垯鐨勬剰鎬濇槸锛氫娇鐢ㄥ涓殧绂荤殑鎺ュ彛锛屾瘮浣跨敤鍗曚釜鎺ュ彛瑕佸ソ銆傝繕鏄竴涓檷浣庣被涔嬮棿鐨勮€﹀悎搴︾殑鎰忔€濓紝浠庤繖鍎挎垜浠湅鍑猴紝鍏跺疄璁捐妯″紡灏辨槸涓€涓蒋浠剁殑璁捐鎬濇兂锛屼粠澶у瀷杞欢鏋舵瀯鍑哄彂锛屼负浜嗗崌绾у拰缁存姢鏂逛究銆傛墍浠ヤ笂鏂囦腑澶氭鍑虹幇锛氶檷浣庝緷璧栵紝闄嶄綆鑰﹀悎銆?br/>5銆佽开绫崇壒娉曞垯锛堟渶灏戠煡閬撳師鍒欙級锛圖emeter Principle锛?br/>涓轰粈涔堝彨鏈€灏戠煡閬撳師鍒欙紝灏辨槸璇达細涓€涓疄浣撳簲褰撳敖閲忓皯鐨勪笌鍏朵粬瀹炰綋涔嬮棿鍙戠敓鐩镐簰浣滅敤锛屼娇寰楃郴缁熷姛鑳芥ā鍧楃浉瀵圭嫭绔嬨€?br/>6銆佸悎鎴愬鐢ㄥ師鍒欙紙Composite Reuse Principle锛?br/>鍘熷垯鏄敖閲忎娇鐢ㄥ悎鎴?鑱氬悎鐨勬柟寮忥紝鑰屼笉鏄娇鐢ㄧ户鎵裤€?br/>涓夈€丣ava鐨?3涓璁℃ā寮?br/>浠庤繖涓€鍧楀紑濮嬶紝鎴戜滑璇︾粏浠嬬粛Java涓?3绉嶈璁℃ā寮忕殑姒傚康锛屽簲鐢ㄥ満鏅瓑鎯呭喌锛屽苟缁撳悎浠栦滑鐨勭壒鐐瑰強璁捐妯″紡鐨勫師鍒欒繘琛屽垎鏋愩€?br/>1銆佸伐鍘傛柟娉曟ā寮忥紙Factory Method锛?br/>宸ュ巶鏂规硶妯″紡鍒嗕负涓夌锛?br/>11銆佹櫘閫氬伐鍘傛ā寮忥紝灏辨槸寤虹珛涓€涓伐鍘傜被锛屽瀹炵幇浜嗗悓涓€鎺ュ彛鐨勪竴浜涚被杩涜瀹炰緥鐨勫垱寤恒€傞鍏堢湅涓嬪叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
涓句緥濡備笅锛氾紙鎴戜滑涓句竴涓彂閫侀偖浠跺拰鐭俊鐨勪緥瀛愶級
棣栧厛锛屽垱寤轰簩鑰呯殑鍏卞悓鎺ュ彛锛?br/>

 view plaincopy <br/>public interface Sender { <br/>    public void Send(); <br/>} <br/>鍏舵锛屽垱寤哄疄鐜扮被锛?br/>1 view plaincopy <br/>public class MailSender implements Sender { <br/>    @Override <br/>    public void Send() { <br/>        System.out.println(鈥渢his is mailsender!鈥?; <br/>    } <br/>} <br/>1 view plaincopy <br/>public class SmsSender implements Sender {  <br/>11@Override  
public void Send() {  
    System.out.println("this is sms sender!");  
}

}
鏈€鍚庯紝寤哄伐鍘傜被锛?br/>

 view plaincopy <br/>public class SendFactory {  <br/>11public Sender produce(String type) {  
    if ("mail".equals(type)) {  
        return new MailSender();  
    } else if ("sms".equals(type)) {  
        return new SmsSender();  
    } else {  
        System.out.println("璇疯緭鍏ユ纭殑绫诲瀷!");  
        return null;  
    }  
}

}
鎴戜滑鏉ユ祴璇曚笅锛?br/>public class FactoryTest {

1public static void main(String[] args) {  
    SendFactory factory = new SendFactory();  
    Sender sender = factory.produce("sms");  
    sender.Send();  
}

}
杈撳嚭锛歵his is sms sender!
22銆佸涓伐鍘傛柟娉曟ā寮忥紝鏄鏅€氬伐鍘傛柟娉曟ā寮忕殑鏀硅繘锛屽湪鏅€氬伐鍘傛柟娉曟ā寮忎腑锛屽鏋滀紶閫掔殑瀛楃涓插嚭閿欙紝鍒欎笉鑳芥纭垱寤哄璞★紝鑰屽涓伐鍘傛柟娉曟ā寮忔槸鎻愪緵澶氫釜宸ュ巶鏂规硶锛屽垎鍒垱寤哄璞°€傚叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
灏嗕笂闈㈢殑浠g爜鍋氫笅淇敼锛屾敼鍔ㄤ笅SendFactory绫诲氨琛岋紝濡備笅锛?br/>

 view plaincopypublic class SendFactory { <br/>   public Sender produceMail(){ <br/>        return new MailSender(); <br/>    }  <br/>11public Sender produceSms(){  
    return new SmsSender();  
}

}
娴嬭瘯绫诲涓嬶細

 view plaincopy <br/>public class FactoryTest {  <br/>11public static void main(String[] args) {  
    SendFactory factory = new SendFactory();  
    Sender sender = factory.produceMail();  
    sender.Send();  
}

}
杈撳嚭锛歵his is mailsender!
33銆侀潤鎬佸伐鍘傛柟娉曟ā寮忥紝灏嗕笂闈㈢殑澶氫釜宸ュ巶鏂规硶妯″紡閲岀殑鏂规硶缃负闈欐€佺殑锛屼笉闇€瑕佸垱寤哄疄渚嬶紝鐩存帴璋冪敤鍗冲彲銆?br/>

 view plaincopy <br/>public class SendFactory {  <br/>11public static Sender produceMail(){  
    return new MailSender();  
}  

public static Sender produceSms(){  
    return new SmsSender();  
}

}

 view plaincopy <br/>public class FactoryTest {  <br/>11public static void main(String[] args) {      
    Sender sender = SendFactory.produceMail();  
    sender.Send();  
}

}
杈撳嚭锛歵his is mailsender!
鎬讳綋鏉ヨ锛屽伐鍘傛ā寮忛€傚悎锛氬嚒鏄嚭鐜颁簡澶ч噺鐨勪骇鍝侀渶瑕佸垱寤猴紝骞朵笖鍏锋湁鍏卞悓鐨勬帴鍙f椂锛屽彲浠ラ€氳繃宸ュ巶鏂规硶妯″紡杩涜鍒涘缓銆傚湪浠ヤ笂鐨勪笁绉嶆ā寮忎腑锛岀涓€绉嶅鏋滀紶鍏ョ殑瀛楃涓叉湁璇紝涓嶈兘姝g‘鍒涘缓瀵硅薄锛岀涓夌鐩稿浜庣浜岀锛屼笉闇€瑕佸疄渚嬪寲宸ュ巶绫伙紝鎵€浠ワ紝澶у鏁版儏鍐典笅锛屾垜浠細閫夌敤绗笁绉嶁€斺€旈潤鎬佸伐鍘傛柟娉曟ā寮忋€?br/>2銆佹娊璞″伐鍘傛ā寮忥紙Abstract Factory锛?
宸ュ巶鏂规硶妯″紡鏈変竴涓棶棰樺氨鏄紝绫荤殑鍒涘缓渚濊禆宸ュ巶绫伙紝涔熷氨鏄锛屽鏋滄兂瑕佹嫇灞曠▼搴忥紝蹇呴』瀵瑰伐鍘傜被杩涜淇敼锛岃繖杩濊儗浜嗛棴鍖呭師鍒欙紝鎵€浠ワ紝浠庤璁¤搴﹁€冭檻锛屾湁涓€瀹氱殑闂锛屽浣曡В鍐筹紵灏辩敤鍒版娊璞″伐鍘傛ā寮忥紝鍒涘缓澶氫釜宸ュ巶绫伙紝杩欐牱涓€鏃﹂渶瑕佸鍔犳柊鐨勫姛鑳斤紝鐩存帴澧炲姞鏂扮殑宸ュ巶绫诲氨鍙互浜嗭紝涓嶉渶瑕佷慨鏀逛箣鍓嶇殑浠g爜銆傚洜涓烘娊璞″伐鍘備笉澶ソ鐞嗚В锛屾垜浠厛鐪嬬湅鍥撅紝鐒跺悗灏卞拰浠g爜锛屽氨姣旇緝瀹规槗鐞嗚В銆?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
璇风湅渚嬪瓙锛?

 view plaincopy <br/>public interface Sender { <br/>    public void Send(); <br/>} <br/>涓や釜瀹炵幇绫伙細<br/>1 view plaincopy <br/>public class MailSender implements Sender { <br/>    @Override <br/>    public void Send() { <br/>        System.out.println(鈥渢his is mailsender!鈥?; <br/>    } <br/>} <br/>1 view plaincopy <br/>public class SmsSender implements Sender {  <br/>11@Override  
public void Send() {  
    System.out.println("this is sms sender!");  
}

}
涓や釜宸ュ巶绫伙細

 view plaincopy <br/>public class SendMailFactory implements Provider {  <br/>11@Override  
public Sender produce(){  
    return new MailSender();  
}

}

 view plaincopy <br/>public class SendSmsFactory implements Provider{  <br/>11@Override  
public Sender produce() {  
    return new SmsSender();  
}

}
鍦ㄦ彁渚涗竴涓帴鍙o細

 view plaincopy <br/>public interface Provider { <br/>    public Sender produce(); <br/>} <br/>娴嬭瘯绫伙細<br/>1 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  
    Provider provider = new SendMailFactory();  
    Sender sender = provider.produce();  
    sender.Send();  
}

}
鍏跺疄杩欎釜妯″紡鐨勫ソ澶勫氨鏄紝濡傛灉浣犵幇鍦ㄦ兂澧炲姞涓€涓姛鑳斤細鍙戝強鏃朵俊鎭紝鍒欏彧闇€鍋氫竴涓疄鐜扮被锛屽疄鐜癝ender鎺ュ彛锛屽悓鏃跺仛涓€涓伐鍘傜被锛屽疄鐜癙rovider鎺ュ彛锛屽氨OK浜嗭紝鏃犻渶鍘绘敼鍔ㄧ幇鎴愮殑浠g爜銆傝繖鏍峰仛锛屾嫇灞曟€ц緝濂斤紒
3銆佸崟渚嬫ā寮忥紙Singleton锛?br/>鍗曚緥瀵硅薄锛圫ingleton锛夋槸涓€绉嶅父鐢ㄧ殑璁捐妯″紡銆傚湪Java搴旂敤涓紝鍗曚緥瀵硅薄鑳戒繚璇佸湪涓€涓狫VM涓紝璇ュ璞″彧鏈変竴涓疄渚嬪瓨鍦ㄣ€傝繖鏍风殑妯″紡鏈夊嚑涓ソ澶勶細
1銆佹煇浜涚被鍒涘缓姣旇緝棰戠箒锛屽浜庝竴浜涘ぇ鍨嬬殑瀵硅薄锛岃繖鏄竴绗斿緢澶х殑绯荤粺寮€閿€銆?br/>2銆佺渷鍘讳簡new鎿嶄綔绗︼紝闄嶄綆浜嗙郴缁熷唴瀛樼殑浣跨敤棰戠巼锛屽噺杞籊C鍘嬪姏銆?br/>3銆佹湁浜涚被濡備氦鏄撴墍鐨勬牳蹇冧氦鏄撳紩鎿庯紝鎺у埗鐫€浜ゆ槗娴佺▼锛屽鏋滆绫诲彲浠ュ垱寤哄涓殑璇濓紝绯荤粺瀹屽叏涔变簡銆傦紙姣斿涓€涓啗闃熷嚭鐜颁簡澶氫釜鍙镐护鍛樺悓鏃舵寚鎸ワ紝鑲畾浼氫贡鎴愪竴鍥級锛屾墍浠ュ彧鏈変娇鐢ㄥ崟渚嬫ā寮忥紝鎵嶈兘淇濊瘉鏍稿績浜ゆ槗鏈嶅姟鍣ㄧ嫭绔嬫帶鍒舵暣涓祦绋嬨€?br/>棣栧厛鎴戜滑鍐欎竴涓畝鍗曠殑鍗曚緥绫伙細

 view plaincopy <br/>public class Singleton {  <br/>11/* 鎸佹湁绉佹湁闈欐€佸疄渚嬶紝闃叉琚紩鐢紝姝ゅ璧嬪€间负null锛岀洰鐨勬槸瀹炵幇寤惰繜鍔犺浇 */  
private static Singleton instance = null;  

/* 绉佹湁鏋勯€犳柟娉曪紝闃叉琚疄渚嬪寲 */  
private Singleton() {  
}  

/* 闈欐€佸伐绋嬫柟娉曪紝鍒涘缓瀹炰緥 */  
public static Singleton getInstance() {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    return instance;  
}  

/* 濡傛灉璇ュ璞¤鐢ㄤ簬搴忓垪鍖栵紝鍙互淇濊瘉瀵硅薄鍦ㄥ簭鍒楀寲鍓嶅悗淇濇寔涓€鑷?*/  
public Object readResolve() {  
    return instance;  
}

}
杩欎釜绫诲彲浠ユ弧瓒冲熀鏈姹傦紝浣嗘槸锛屽儚杩欐牱姣棤绾跨▼瀹夊叏淇濇姢鐨勭被锛屽鏋滄垜浠妸瀹冩斁鍏ュ绾跨▼鐨勭幆澧冧笅锛岃偗瀹氬氨浼氬嚭鐜伴棶棰樹簡锛屽浣曡В鍐筹紵鎴戜滑棣栧厛浼氭兂鍒板getInstance鏂规硶鍔爏ynchronized鍏抽敭瀛楋紝濡備笅锛?br/>

 view plaincopy <br/>public static synchronized Singleton getInstance() { <br/>        if (instance == null) { <br/>            instance = new Singleton(); <br/>        } <br/>        return instance; <br/>    } <br/>浣嗘槸锛宻ynchronized鍏抽敭瀛楅攣浣忕殑鏄繖涓璞★紝杩欐牱鐨勭敤娉曪紝鍦ㄦ€ц兘涓婁細鏈夋墍涓嬮檷锛屽洜涓烘瘡娆¤皟鐢╣etInstance()锛岄兘瑕佸瀵硅薄涓婇攣锛屼簨瀹炰笂锛屽彧鏈夊湪绗竴娆″垱寤哄璞$殑鏃跺€欓渶瑕佸姞閿侊紝涔嬪悗灏变笉闇€瑕佷簡锛屾墍浠ワ紝杩欎釜鍦版柟闇€瑕佹敼杩涖€傛垜浠敼鎴愪笅闈㈣繖涓細<br/>1 view plaincopy <br/>public static Singleton getInstance() { <br/>        if (instance == null) { <br/>            synchronized (instance) { <br/>                if (instance == null) { <br/>                    instance = new Singleton(); <br/>                } <br/>            } <br/>        } <br/>        return instance; <br/>    } <br/>浼间箮瑙e喅浜嗕箣鍓嶆彁鍒扮殑闂锛屽皢synchronized鍏抽敭瀛楀姞鍦ㄤ簡鍐呴儴锛屼篃灏辨槸璇村綋璋冪敤鐨勬椂鍊欐槸涓嶉渶瑕佸姞閿佺殑锛屽彧鏈夊湪instance涓簄ull锛屽苟鍒涘缓瀵硅薄鐨勬椂鍊欐墠闇€瑕佸姞閿侊紝鎬ц兘鏈変竴瀹氱殑鎻愬崌銆備絾鏄紝杩欐牱鐨勬儏鍐碉紝杩樻槸鏈夊彲鑳芥湁闂鐨勶紝鐪嬩笅闈㈢殑鎯呭喌锛氬湪Java鎸囦护涓垱寤哄璞″拰璧嬪€兼搷浣滄槸鍒嗗紑杩涜鐨勶紝涔熷氨鏄instance = new Singleton();璇彞鏄垎涓ゆ鎵ц鐨勩€備絾鏄疛VM骞朵笉淇濊瘉杩欎袱涓搷浣滅殑鍏堝悗椤哄簭锛屼篃灏辨槸璇存湁鍙兘JVM浼氫负鏂扮殑Singleton瀹炰緥鍒嗛厤绌洪棿锛岀劧鍚庣洿鎺ヨ祴鍊肩粰instance鎴愬憳锛岀劧鍚庡啀鍘诲垵濮嬪寲杩欎釜Singleton瀹炰緥銆傝繖鏍峰氨鍙兘鍑洪敊浜嗭紝鎴戜滑浠銆丅涓や釜绾跨▼涓轰緥锛?br/>a>A銆丅绾跨▼鍚屾椂杩涘叆浜嗙涓€涓猧f鍒ゆ柇<br/>b>A棣栧厛杩涘叆synchronized鍧楋紝鐢变簬instance涓簄ull锛屾墍浠ュ畠鎵цinstance = new Singleton();<br/>c>鐢变簬JVM鍐呴儴鐨勪紭鍖栨満鍒讹紝JVM鍏堢敾鍑轰簡涓€浜涘垎閰嶇粰Singleton瀹炰緥鐨勭┖鐧藉唴瀛橈紝骞惰祴鍊肩粰instance鎴愬憳锛堟敞鎰忔鏃禞VM娌℃湁寮€濮嬪垵濮嬪寲杩欎釜瀹炰緥锛夛紝鐒跺悗A绂诲紑浜唖ynchronized鍧椼€?br/>d>B杩涘叆synchronized鍧楋紝鐢变簬instance姝ゆ椂涓嶆槸null锛屽洜姝ゅ畠椹笂绂诲紑浜唖ynchronized鍧楀苟灏嗙粨鏋滆繑鍥炵粰璋冪敤璇ユ柟娉曠殑绋嬪簭銆?br/>e>姝ゆ椂B绾跨▼鎵撶畻浣跨敤Singleton瀹炰緥锛屽嵈鍙戠幇瀹冩病鏈夎鍒濆鍖栵紝浜庢槸閿欒鍙戠敓浜嗐€?br/>鎵€浠ョ▼搴忚繕鏄湁鍙兘鍙戠敓閿欒锛屽叾瀹炵▼搴忓湪杩愯杩囩▼鏄緢澶嶆潅鐨勶紝浠庤繖鐐规垜浠氨鍙互鐪嬪嚭锛屽挨鍏舵槸鍦ㄥ啓澶氱嚎绋嬬幆澧冧笅鐨勭▼搴忔洿鏈夐毦搴︼紝鏈夋寫鎴樻€с€傛垜浠璇ョ▼搴忓仛杩涗竴姝ヤ紭鍖栵細<br/>1 view plaincopy <br/>private static class SingletonFactory{ <br/>        private static Singleton instance = new Singleton(); <br/>    } <br/>    public static Singleton getInstance(){ <br/>        return SingletonFactory.instance; <br/>    } <br/>瀹為檯鎯呭喌鏄紝鍗曚緥妯″紡浣跨敤鍐呴儴绫绘潵缁存姢鍗曚緥鐨勫疄鐜帮紝JVM鍐呴儴鐨勬満鍒惰兘澶熶繚璇佸綋涓€涓被琚姞杞界殑鏃跺€欙紝杩欎釜绫荤殑鍔犺浇杩囩▼鏄嚎绋嬩簰鏂ョ殑銆傝繖鏍峰綋鎴戜滑绗竴娆¤皟鐢╣etInstance鐨勬椂鍊欙紝JVM鑳藉甯垜浠繚璇乮nstance鍙鍒涘缓涓€娆★紝骞朵笖浼氫繚璇佹妸璧嬪€肩粰instance鐨勫唴瀛樺垵濮嬪寲瀹屾瘯锛岃繖鏍锋垜浠氨涓嶇敤鎷呭績涓婇潰鐨勯棶棰樸€傚悓鏃惰鏂规硶涔熷彧浼氬湪绗竴娆¤皟鐢ㄧ殑鏃跺€欎娇鐢ㄤ簰鏂ユ満鍒讹紝杩欐牱灏辫В鍐充簡浣庢€ц兘闂銆傝繖鏍锋垜浠殏鏃舵€荤粨涓€涓畬缇庣殑鍗曚緥妯″紡锛?br/>1 view plaincopy <br/>public class Singleton {  <br/>11/* 绉佹湁鏋勯€犳柟娉曪紝闃叉琚疄渚嬪寲 */  
private Singleton() {  
}  

/* 姝ゅ浣跨敤涓€涓唴閮ㄧ被鏉ョ淮鎶ゅ崟渚?*/  
private static class SingletonFactory {  
    private static Singleton instance = new Singleton();  
}  

/* 鑾峰彇瀹炰緥 */  
public static Singleton getInstance() {  
    return SingletonFactory.instance;  
}  

/* 濡傛灉璇ュ璞¤鐢ㄤ簬搴忓垪鍖栵紝鍙互淇濊瘉瀵硅薄鍦ㄥ簭鍒楀寲鍓嶅悗淇濇寔涓€鑷?*/  
public Object readResolve() {  
    return getInstance();  
}

}
鍏跺疄璇村畠瀹岀編锛屼篃涓嶄竴瀹氾紝濡傛灉鍦ㄦ瀯閫犲嚱鏁颁腑鎶涘嚭寮傚父锛屽疄渚嬪皢姘歌繙寰椾笉鍒板垱寤猴紝涔熶細鍑洪敊銆傛墍浠ヨ锛屽崄鍒嗗畬缇庣殑涓滆タ鏄病鏈夌殑锛屾垜浠彧鑳芥牴鎹疄闄呮儏鍐碉紝閫夋嫨鏈€閫傚悎鑷繁搴旂敤鍦烘櫙鐨勫疄鐜版柟娉曘€備篃鏈変汉杩欐牱瀹炵幇锛氬洜涓烘垜浠彧闇€瑕佸湪鍒涘缓绫荤殑鏃跺€欒繘琛屽悓姝ワ紝鎵€浠ュ彧瑕佸皢鍒涘缓鍜実etInstance()鍒嗗紑锛屽崟鐙负鍒涘缓鍔爏ynchronized鍏抽敭瀛楋紝涔熸槸鍙互鐨勶細

 view plaincopy <br/>public class SingletonTest {  <br/>11private static SingletonTest instance = null;  

private SingletonTest() {  
}  

private static synchronized void syncInit() {  
    if (instance == null) {  
        instance = new SingletonTest();  
    }  
}  

public static SingletonTest getInstance() {  
    if (instance == null) {  
        syncInit();  
    }  
    return instance;  
}

}
鑰冭檻鎬ц兘鐨勮瘽锛屾暣涓▼搴忓彧闇€鍒涘缓涓€娆″疄渚嬶紝鎵€浠ユ€ц兘涔熶笉浼氭湁浠€涔堝奖鍝嶃€?br/>琛ュ厖锛氶噰鐢ㄢ€濆奖瀛愬疄渚嬧€濈殑鍔炴硶涓哄崟渚嬪璞$殑灞炴€у悓姝ユ洿鏂?br/>

 view plaincopy <br/>public class SingletonTest {  <br/>11private static SingletonTest instance = null;  
private Vector properties = null;  

public Vector getProperties() {  
    return properties;  
}  

private SingletonTest() {  
}  

private static synchronized void syncInit() {  
    if (instance == null) {  
        instance = new SingletonTest();  
    }  
}  

public static SingletonTest getInstance() {  
    if (instance == null) {  
        syncInit();  
    }  
    return instance;  
}  

public void updateProperties() {  
    SingletonTest shadow = new SingletonTest();  
    properties = shadow.getProperties();  
}

}
閫氳繃鍗曚緥妯″紡鐨勫涔犲憡璇夋垜浠細
1銆佸崟渚嬫ā寮忕悊瑙h捣鏉ョ畝鍗曪紝浣嗘槸鍏蜂綋瀹炵幇璧锋潵杩樻槸鏈変竴瀹氱殑闅惧害銆?br/>2銆乻ynchronized鍏抽敭瀛楅攣瀹氱殑鏄璞★紝鍦ㄧ敤鐨勬椂鍊欙紝涓€瀹氳鍦ㄦ伆褰撶殑鍦版柟浣跨敤锛堟敞鎰忛渶瑕佷娇鐢ㄩ攣鐨勫璞″拰杩囩▼锛屽彲鑳芥湁鐨勬椂鍊欏苟涓嶆槸鏁翠釜瀵硅薄鍙婃暣涓繃绋嬮兘闇€瑕侀攣锛夈€?br/>鍒拌繖鍎匡紝鍗曚緥妯″紡鍩烘湰宸茬粡璁插畬浜嗭紝缁撳熬澶勶紝绗旇€呯獊鐒舵兂鍒板彟涓€涓棶棰橈紝灏辨槸閲囩敤绫荤殑闈欐€佹柟娉曪紝瀹炵幇鍗曚緥妯″紡鐨勬晥鏋滐紝涔熸槸鍙鐨勶紝姝ゅ浜岃€呮湁浠€涔堜笉鍚岋紵
棣栧厛锛岄潤鎬佺被涓嶈兘瀹炵幇鎺ュ彛銆傦紙浠庣被鐨勮搴﹁鏄彲浠ョ殑锛屼絾鏄偅鏍峰氨鐮村潖浜嗛潤鎬佷簡銆傚洜涓烘帴鍙d腑涓嶅厑璁告湁static淇グ鐨勬柟娉曪紝鎵€浠ュ嵆浣垮疄鐜颁簡涔熸槸闈為潤鎬佺殑锛?br/>鍏舵锛屽崟渚嬪彲浠ヨ寤惰繜鍒濆鍖栵紝闈欐€佺被涓€鑸湪绗竴娆″姞杞芥槸鍒濆鍖栥€備箣鎵€浠ュ欢杩熷姞杞斤紝鏄洜涓烘湁浜涚被姣旇緝搴炲ぇ锛屾墍浠ュ欢杩熷姞杞芥湁鍔╀簬鎻愬崌鎬ц兘銆?br/>鍐嶆锛屽崟渚嬬被鍙互琚户鎵匡紝浠栫殑鏂规硶鍙互琚鍐欍€備絾鏄潤鎬佺被鍐呴儴鏂规硶閮芥槸static锛屾棤娉曡瑕嗗啓銆?br/>鏈€鍚庝竴鐐癸紝鍗曚緥绫绘瘮杈冪伒娲伙紝姣曠珶浠庡疄鐜颁笂鍙槸涓€涓櫘閫氱殑Java绫伙紝鍙婊¤冻鍗曚緥鐨勫熀鏈渶姹傦紝浣犲彲浠ュ湪閲岄潰闅忓績鎵€娆茬殑瀹炵幇涓€浜涘叾瀹冨姛鑳斤紝浣嗘槸闈欐€佺被涓嶈銆備粠涓婇潰杩欎簺姒傛嫭涓紝鍩烘湰鍙互鐪嬪嚭浜岃€呯殑鍖哄埆锛屼絾鏄紝浠庡彟涓€鏂归潰璁诧紝鎴戜滑涓婇潰鏈€鍚庡疄鐜扮殑閭d釜鍗曚緥妯″紡锛屽唴閮ㄥ氨鏄敤涓€涓潤鎬佺被鏉ュ疄鐜扮殑锛屾墍浠ワ紝浜岃€呮湁寰堝ぇ鐨勫叧鑱旓紝鍙槸鎴戜滑鑰冭檻闂鐨勫眰闈笉鍚岀舰浜嗐€備袱绉嶆€濇兂鐨勭粨鍚堬紝鎵嶈兘閫犲氨鍑哄畬缇庣殑瑙e喅鏂规锛屽氨鍍廐ashMap閲囩敤鏁扮粍+閾捐〃鏉ュ疄鐜颁竴鏍凤紝鍏跺疄鐢熸椿涓緢澶氫簨鎯呴兘鏄繖鏍凤紝鍗曠敤涓嶅悓鐨勬柟娉曟潵澶勭悊闂锛屾€绘槸鏈変紭鐐逛篃鏈夌己鐐癸紝鏈€瀹岀編鐨勬柟娉曟槸锛岀粨鍚堝悇涓柟娉曠殑浼樼偣锛屾墠鑳芥渶濂界殑瑙e喅闂锛?br/>4銆佸缓閫犺€呮ā寮忥紙Builder锛?br/>宸ュ巶绫绘ā寮忔彁渚涚殑鏄垱寤哄崟涓被鐨勬ā寮忥紝鑰屽缓閫犺€呮ā寮忓垯鏄皢鍚勭浜у搧闆嗕腑璧锋潵杩涜绠$悊锛岀敤鏉ュ垱寤哄鍚堝璞★紝鎵€璋撳鍚堝璞″氨鏄寚鏌愪釜绫诲叿鏈変笉鍚岀殑灞炴€э紝鍏跺疄寤洪€犺€呮ā寮忓氨鏄墠闈㈡娊璞″伐鍘傛ā寮忓拰鏈€鍚庣殑Test缁撳悎璧锋潵寰楀埌鐨勩€傛垜浠湅涓€涓嬩唬鐮侊細
杩樺拰鍓嶉潰涓€鏍凤紝涓€涓猄ender鎺ュ彛锛屼袱涓疄鐜扮被MailSender鍜孲msSender銆傛渶鍚庯紝寤洪€犺€呯被濡備笅锛?br/>

 view plaincopy <br/>public class Builder {  <br/>11private List<Sender> list = new ArrayList<Sender>();  

public void produceMailSender(int count){  
    for(int i=0; i<count; i++){  
        list.add(new MailSender());  
    }  
}  

public void produceSmsSender(int count){  
    for(int i=0; i<count; i++){  
        list.add(new SmsSender());  
    }  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  
    Builder builder = new Builder();  
    builder.produceMailSender(10);  
}

}
浠庤繖鐐圭湅鍑猴紝寤洪€犺€呮ā寮忓皢寰堝鍔熻兘闆嗘垚鍒颁竴涓被閲岋紝杩欎釜绫诲彲浠ュ垱閫犲嚭姣旇緝澶嶆潅鐨勪笢瑗裤€傛墍浠ヤ笌宸ョ▼妯″紡鐨勫尯鍒氨鏄細宸ュ巶妯″紡鍏虫敞鐨勬槸鍒涘缓鍗曚釜浜у搧锛岃€屽缓閫犺€呮ā寮忓垯鍏虫敞鍒涘缓绗﹀悎瀵硅薄锛屽涓儴鍒嗐€傚洜姝わ紝鏄€夋嫨宸ュ巶妯″紡杩樻槸寤洪€犺€呮ā寮忥紝渚濆疄闄呮儏鍐佃€屽畾銆?br/>5銆佸師鍨嬫ā寮忥紙Prototype锛?br/>鍘熷瀷妯″紡铏界劧鏄垱寤哄瀷鐨勬ā寮忥紝浣嗘槸涓庡伐绋嬫ā寮忔病鏈夊叧绯伙紝浠庡悕瀛楀嵆鍙湅鍑猴紝璇ユā寮忕殑鎬濇兂灏辨槸灏嗕竴涓璞′綔涓哄師鍨嬶紝瀵瑰叾杩涜澶嶅埗銆佸厠闅嗭紝浜х敓涓€涓拰鍘熷璞$被浼肩殑鏂板璞°€傛湰灏忕粨浼氶€氳繃瀵硅薄鐨勫鍒讹紝杩涜璁茶В銆傚湪Java涓紝澶嶅埗瀵硅薄鏄€氳繃clone()瀹炵幇鐨勶紝鍏堝垱寤轰竴涓師鍨嬬被锛?br/>

 view plaincopy <br/>public class Prototype implements Cloneable {  <br/>11public Object clone() throws CloneNotSupportedException {  
    Prototype proto = (Prototype) super.clone();  
    return proto;  
}

}
寰堢畝鍗曪紝涓€涓師鍨嬬被锛屽彧闇€瑕佸疄鐜癈loneable鎺ュ彛锛岃鍐檆lone鏂规硶锛屾澶刢lone鏂规硶鍙互鏀规垚浠绘剰鐨勫悕绉帮紝鍥犱负Cloneable鎺ュ彛鏄釜绌烘帴鍙o紝浣犲彲浠ヤ换鎰忓畾涔夊疄鐜扮被鐨勬柟娉曞悕锛屽cloneA鎴栬€卌loneB锛屽洜涓烘澶勭殑閲嶇偣鏄痵uper.clone()杩欏彞璇濓紝super.clone()璋冪敤鐨勬槸Object鐨刢lone()鏂规硶锛岃€屽湪Object绫讳腑锛宑lone()鏄痭ative鐨勶紝鍏蜂綋鎬庝箞瀹炵幇锛屾垜浼氬湪鍙︿竴绡囨枃绔犱腑锛屽叧浜庤В璇籎ava涓湰鍦版柟娉曠殑璋冪敤锛屾澶勪笉鍐嶆繁绌躲€傚湪杩欏効锛屾垜灏嗙粨鍚堝璞$殑娴呭鍒跺拰娣卞鍒舵潵璇翠竴涓嬶紝棣栧厛闇€瑕佷簡瑙e璞℃繁銆佹祬澶嶅埗鐨勬蹇碉細
娴呭鍒讹細灏嗕竴涓璞″鍒跺悗锛屽熀鏈暟鎹被鍨嬬殑鍙橀噺閮戒細閲嶆柊鍒涘缓锛岃€屽紩鐢ㄧ被鍨嬶紝鎸囧悜鐨勮繕鏄師瀵硅薄鎵€鎸囧悜鐨勩€?br/>娣卞鍒讹細灏嗕竴涓璞″鍒跺悗锛屼笉璁烘槸鍩烘湰鏁版嵁绫诲瀷杩樻湁寮曠敤绫诲瀷锛岄兘鏄噸鏂板垱寤虹殑銆傜畝鍗曟潵璇达紝灏辨槸娣卞鍒惰繘琛屼簡瀹屽叏褰诲簳鐨勫鍒讹紝鑰屾祬澶嶅埗涓嶅交搴曘€?br/>姝ゅ锛屽啓涓€涓繁娴呭鍒剁殑渚嬪瓙锛?br/>

 view plaincopy <br/>public class Prototype implements Cloneable, Serializable {  <br/>11private static final long serialVersionUID = 1L;  
private String string;  

private SerializableObject obj;  

/* 娴呭鍒?*/  
public Object clone() throws CloneNotSupportedException {  
    Prototype proto = (Prototype) super.clone();  
    return proto;  
}  

/* 娣卞鍒?*/  
public Object deepClone() throws IOException, ClassNotFoundException {  

    /* 鍐欏叆褰撳墠瀵硅薄鐨勪簩杩涘埗娴?*/  
    ByteArrayOutputStream bos = new ByteArrayOutputStream();  
    ObjectOutputStream oos = new ObjectOutputStream(bos);  
    oos.writeObject(this);  

    /* 璇诲嚭浜岃繘鍒舵祦浜х敓鐨勬柊瀵硅薄 */  
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());  
    ObjectInputStream ois = new ObjectInputStream(bis);  
    return ois.readObject();  
}  

public String getString() {  
    return string;  
}  

public void setString(String string) {  
    this.string = string;  
}  

public SerializableObject getObj() {  
    return obj;  
}  

public void setObj(SerializableObject obj) {  
    this.obj = obj;  
}

}
class SerializableObject implements Serializable {
private static final long serialVersionUID = 1L;
}
瑕佸疄鐜版繁澶嶅埗锛岄渶瑕侀噰鐢ㄦ祦鐨勫舰寮忚鍏ュ綋鍓嶅璞$殑浜岃繘鍒惰緭鍏ワ紝鍐嶅啓鍑轰簩杩涘埗鏁版嵁瀵瑰簲鐨勫璞°€?
鎴戜滑鎺ョ潃璁ㄨ璁捐妯″紡锛屼笂绡囨枃绔犳垜璁插畬浜?绉嶅垱寤哄瀷妯″紡锛岃繖绔犲紑濮嬶紝鎴戝皢璁蹭笅7绉嶇粨鏋勫瀷妯″紡锛氶€傞厤鍣ㄦā寮忋€佽楗版ā寮忋€佷唬鐞嗘ā寮忋€佸瑙傛ā寮忋€佹ˉ鎺ユā寮忋€佺粍鍚堟ā寮忋€佷韩鍏冩ā寮忋€傚叾涓璞$殑閫傞厤鍣ㄦā寮忔槸鍚勭妯″紡鐨勮捣婧愶紝鎴戜滑鐪嬩笅闈㈢殑鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
閫傞厤鍣ㄦā寮忓皢鏌愪釜绫荤殑鎺ュ彛杞崲鎴愬鎴风鏈熸湜鐨勫彟涓€涓帴鍙h〃绀猴紝鐩殑鏄秷闄ょ敱浜庢帴鍙d笉鍖归厤鎵€閫犳垚鐨勭被鐨勫吋瀹规€ч棶棰樸€備富瑕佸垎涓轰笁绫伙細绫荤殑閫傞厤鍣ㄦā寮忋€佸璞$殑閫傞厤鍣ㄦā寮忋€佹帴鍙g殑閫傞厤鍣ㄦā寮忋€傞鍏堬紝鎴戜滑鏉ョ湅鐪嬬被鐨勯€傞厤鍣ㄦā寮忥紝鍏堢湅绫诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鏍稿績鎬濇兂灏辨槸锛氭湁涓€涓猄ource绫伙紝鎷ユ湁涓€涓柟娉曪紝寰呴€傞厤锛岀洰鏍囨帴鍙f椂Targetable锛岄€氳繃Adapter绫伙紝灏哠ource鐨勫姛鑳芥墿灞曞埌Targetable閲岋紝鐪嬩唬鐮侊細

 view plaincopy <br/>public class Source {  <br/>11public void method1() {  
    System.out.println("this is original method!");  
}

}

 view plaincopy <br/>public interface Targetable {  <br/>11/* 涓庡師绫讳腑鐨勬柟娉曠浉鍚?*/  
public void method1();  

/* 鏂扮被鐨勬柟娉?*/  
public void method2();

}

 view plaincopy <br/>public class Adapter extends Source implements Targetable {  <br/>11@Override  
public void method2() {  
    System.out.println("this is the targetable method!");  
}

}
Adapter绫荤户鎵縎ource绫伙紝瀹炵幇Targetable鎺ュ彛锛屼笅闈㈡槸娴嬭瘯绫伙細

 view plaincopy <br/>public class AdapterTest {  <br/>11public static void main(String[] args) {  
    Targetable target = new Adapter();  
    target.method1();  
    target.method2();  
}

}
杈撳嚭锛?br/>this is original method!
this is the targetable method!
杩欐牱Targetable鎺ュ彛鐨勫疄鐜扮被灏卞叿鏈変簡Source绫荤殑鍔熻兘銆?br/>瀵硅薄鐨勯€傞厤鍣ㄦā寮?br/>鍩烘湰鎬濊矾鍜岀被鐨勯€傞厤鍣ㄦā寮忕浉鍚岋紝鍙槸灏咥dapter绫讳綔淇敼锛岃繖娆′笉缁ф壙Source绫伙紝鑰屾槸鎸佹湁Source绫荤殑瀹炰緥锛屼互杈惧埌瑙e喅鍏煎鎬х殑闂銆傜湅鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鍙渶瑕佷慨鏀笰dapter绫荤殑婧愮爜鍗冲彲锛?br/>

 view plaincopy <br/>public class Wrapper implements Targetable {  <br/>11private Source source;  

public Wrapper(Source source){  
    super();  
    this.source = source;  
}  
@Override  
public void method2() {  
    System.out.println("this is the targetable method!");  
}  

@Override  
public void method1() {  
    source.method1();  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class AdapterTest {  <br/>11public static void main(String[] args) {  
    Source source = new Source();  
    Targetable target = new Wrapper(source);  
    target.method1();  
    target.method2();  
}

}
杈撳嚭涓庣涓€绉嶄竴鏍凤紝鍙槸閫傞厤鐨勬柟娉曚笉鍚岃€屽凡銆?br/>绗笁绉嶉€傞厤鍣ㄦā寮忔槸鎺ュ彛鐨勯€傞厤鍣ㄦā寮忥紝鎺ュ彛鐨勯€傞厤鍣ㄦ槸杩欐牱鐨勶細鏈夋椂鎴戜滑鍐欑殑涓€涓帴鍙d腑鏈夊涓娊璞℃柟娉曪紝褰撴垜浠啓璇ユ帴鍙g殑瀹炵幇绫绘椂锛屽繀椤诲疄鐜拌鎺ュ彛鐨勬墍鏈夋柟娉曪紝杩欐槑鏄炬湁鏃舵瘮杈冩氮璐癸紝鍥犱负骞朵笉鏄墍鏈夌殑鏂规硶閮芥槸鎴戜滑闇€瑕佺殑锛屾湁鏃跺彧闇€瑕佹煇涓€浜涳紝姝ゅ涓轰簡瑙e喅杩欎釜闂锛屾垜浠紩鍏ヤ簡鎺ュ彛鐨勯€傞厤鍣ㄦā寮忥紝鍊熷姪浜庝竴涓娊璞$被锛岃鎶借薄绫诲疄鐜颁簡璇ユ帴鍙o紝瀹炵幇浜嗘墍鏈夌殑鏂规硶锛岃€屾垜浠笉鍜屽師濮嬬殑鎺ュ彛鎵撲氦閬擄紝鍙拰璇ユ娊璞$被鍙栧緱鑱旂郴锛屾墍浠ユ垜浠啓涓€涓被锛岀户鎵胯鎶借薄绫伙紝閲嶅啓鎴戜滑闇€瑕佺殑鏂规硶灏辫銆傜湅涓€涓嬬被鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
杩欎釜寰堝ソ鐞嗚В锛屽湪瀹為檯寮€鍙戜腑锛屾垜浠篃甯镐細閬囧埌杩欑鎺ュ彛涓畾涔変簡澶鐨勬柟娉曪紝浠ヨ嚧浜庢湁鏃舵垜浠湪涓€浜涘疄鐜扮被涓苟涓嶆槸閮介渶瑕併€傜湅浠g爜锛?

 view plaincopy <br/>public interface Sourceable {  <br/>11public void method1();  
public void method2();

}
鎶借薄绫籛rapper2锛?br/>

 view plaincopy <br/>public abstract class Wrapper2 implements Sourceable{  <br/>11public void method1(){}  
public void method2(){}

}

 view plaincopy <br/>public class SourceSub1 extends Wrapper2 { <br/>    public void method1(){ <br/>        System.out.println(鈥渢he sourceable interface鈥檚 first Sub1!鈥?; <br/>    } <br/>} <br/>1 view plaincopy <br/>public class SourceSub2 extends Wrapper2 { <br/>    public void method2(){ <br/>        System.out.println(鈥渢he sourceable interface鈥檚 second Sub2!鈥?; <br/>    } <br/>} <br/>1 view plaincopy <br/>public class WrapperTest {  <br/>11public static void main(String[] args) {  
    Sourceable source1 = new SourceSub1();  
    Sourceable source2 = new SourceSub2();  

    source1.method1();  
    source1.method2();  
    source2.method1();  
    source2.method2();  
}

}
娴嬭瘯杈撳嚭锛?br/>the sourceable interface鈥檚 first Sub1!
the sourceable interface鈥檚 second Sub2!
杈惧埌浜嗘垜浠殑鏁堟灉锛?br/>璁蹭簡杩欎箞澶氾紝鎬荤粨涓€涓嬩笁绉嶉€傞厤鍣ㄦā寮忕殑搴旂敤鍦烘櫙锛?br/>绫荤殑閫傞厤鍣ㄦā寮忥細褰撳笇鏈涘皢涓€涓被杞崲鎴愭弧瓒冲彟涓€涓柊鎺ュ彛鐨勭被鏃讹紝鍙互浣跨敤绫荤殑閫傞厤鍣ㄦā寮忥紝鍒涘缓涓€涓柊绫伙紝缁ф壙鍘熸湁鐨勭被锛屽疄鐜版柊鐨勬帴鍙e嵆鍙€?br/>瀵硅薄鐨勯€傞厤鍣ㄦā寮忥細褰撳笇鏈涘皢涓€涓璞¤浆鎹㈡垚婊¤冻鍙︿竴涓柊鎺ュ彛鐨勫璞℃椂锛屽彲浠ュ垱寤轰竴涓猈rapper绫伙紝鎸佹湁鍘熺被鐨勪竴涓疄渚嬶紝鍦╓rapper绫荤殑鏂规硶涓紝璋冪敤瀹炰緥鐨勬柟娉曞氨琛屻€?br/>鎺ュ彛鐨勯€傞厤鍣ㄦā寮忥細褰撲笉甯屾湜瀹炵幇涓€涓帴鍙d腑鎵€鏈夌殑鏂规硶鏃讹紝鍙互鍒涘缓涓€涓娊璞$被Wrapper锛屽疄鐜版墍鏈夋柟娉曪紝鎴戜滑鍐欏埆鐨勭被鐨勬椂鍊欙紝缁ф壙鎶借薄绫诲嵆鍙€?br/>7銆佽楗版ā寮忥紙Decorator锛?br/>椤惧悕鎬濅箟锛岃楗版ā寮忓氨鏄粰涓€涓璞″鍔犱竴浜涙柊鐨勫姛鑳斤紝鑰屼笖鏄姩鎬佺殑锛岃姹傝楗板璞″拰琚楗板璞″疄鐜板悓涓€涓帴鍙o紝瑁呴グ瀵硅薄鎸佹湁琚楗板璞$殑瀹炰緥锛屽叧绯诲浘濡備笅锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
Source绫绘槸琚楗扮被锛孌ecorator绫绘槸涓€涓楗扮被锛屽彲浠ヤ负Source绫诲姩鎬佺殑娣诲姞涓€浜涘姛鑳斤紝浠g爜濡備笅锛?br/>

 view plaincopy <br/>public interface Sourceable { <br/>    public void method(); <br/>} <br/>1 view plaincopy <br/>public class Source implements Sourceable {  <br/>11@Override  
public void method() {  
    System.out.println("the original method!");  
}

}

 view plaincopy <br/>public class Decorator implements Sourceable {  <br/>11private Sourceable source;  

public Decorator(Sourceable source){  
    super();  
    this.source = source;  
}  
@Override  
public void method() {  
    System.out.println("before decorator!");  
    source.method();  
    System.out.println("after decorator!");  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class DecoratorTest {  <br/>11public static void main(String[] args) {  
    Sourceable source = new Source();  
    Sourceable obj = new Decorator(source);  
    obj.method();  
}

}
杈撳嚭锛?br/>before decorator!
the original method!
after decorator!
瑁呴グ鍣ㄦā寮忕殑搴旂敤鍦烘櫙锛?br/>1銆侀渶瑕佹墿灞曚竴涓被鐨勫姛鑳姐€?br/>2銆佸姩鎬佺殑涓轰竴涓璞″鍔犲姛鑳斤紝鑰屼笖杩樿兘鍔ㄦ€佹挙閿€銆傦紙缁ф壙涓嶈兘鍋氬埌杩欎竴鐐癸紝缁ф壙鐨勫姛鑳芥槸闈欐€佺殑锛屼笉鑳藉姩鎬佸鍒犮€傦級
缂虹偣锛氫骇鐢熻繃澶氱浉浼肩殑瀵硅薄锛屼笉鏄撴帓閿欙紒
8銆佷唬鐞嗘ā寮忥紙Proxy锛?br/>鍏跺疄姣忎釜妯″紡鍚嶇О灏辫〃鏄庝簡璇ユā寮忕殑浣滅敤锛屼唬鐞嗘ā寮忓氨鏄涓€涓唬鐞嗙被鍑烘潵锛屾浛鍘熷璞¤繘琛屼竴浜涙搷浣滐紝姣斿鎴戜滑鍦ㄧ鎴垮瓙鐨勬椂鍊欏洖鍘绘壘涓粙锛屼负浠€涔堝憿锛熷洜涓轰綘瀵硅鍦板尯鎴垮眿鐨勪俊鎭帉鎻$殑涓嶅鍏ㄩ潰锛屽笇鏈涙壘涓€涓洿鐔熸倝鐨勪汉鍘诲府浣犲仛锛屾澶勭殑浠g悊灏辨槸杩欎釜鎰忔€濄€傚啀濡傛垜浠湁鐨勬椂鍊欐墦瀹樺徃锛屾垜浠渶瑕佽寰嬪笀锛屽洜涓哄緥甯堝湪娉曞緥鏂归潰鏈変笓闀匡紝鍙互鏇挎垜浠繘琛屾搷浣滐紝琛ㄨ揪鎴戜滑鐨勬兂娉曘€傚厛鏉ョ湅鐪嬪叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鏍规嵁涓婃枃鐨勯槓杩帮紝浠g悊妯″紡灏辨瘮杈冨鏄撶殑鐞嗚В浜嗭紝鎴戜滑鐪嬩笅浠g爜锛?br/>

 view plaincopy <br/>public interface Sourceable { <br/>    public void method(); <br/>} <br/>1 view plaincopy <br/>public class Source implements Sourceable {  <br/>11@Override  
public void method() {  
    System.out.println("the original method!");  
}

}

 view plaincopy <br/>public class Proxy implements Sourceable {  <br/>11private Source source;  
public Proxy(){  
    super();  
    this.source = new Source();  
}  
@Override  
public void method() {  
    before();  
    source.method();  
    atfer();  
}  
private void atfer() {  
    System.out.println("after proxy!");  
}  
private void before() {  
    System.out.println("before proxy!");  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class ProxyTest {  <br/>11public static void main(String[] args) {  
    Sourceable source = new Proxy();  
    source.method();  
}

}
杈撳嚭锛?br/>before proxy!
the original method!
after proxy!
浠g悊妯″紡鐨勫簲鐢ㄥ満鏅細
濡傛灉宸叉湁鐨勬柟娉曞湪浣跨敤鐨勬椂鍊欓渶瑕佸鍘熸湁鐨勬柟娉曡繘琛屾敼杩涳紝姝ゆ椂鏈変袱绉嶅姙娉曪細
1銆佷慨鏀瑰師鏈夌殑鏂规硶鏉ラ€傚簲銆傝繖鏍疯繚鍙嶄簡鈥滃鎵╁睍寮€鏀撅紝瀵逛慨鏀瑰叧闂€濈殑鍘熷垯銆?br/>2銆佸氨鏄噰鐢ㄤ竴涓唬鐞嗙被璋冪敤鍘熸湁鐨勬柟娉曪紝涓斿浜х敓鐨勭粨鏋滆繘琛屾帶鍒躲€傝繖绉嶆柟娉曞氨鏄唬鐞嗘ā寮忋€?br/>浣跨敤浠g悊妯″紡锛屽彲浠ュ皢鍔熻兘鍒掑垎鐨勬洿鍔犳竻鏅帮紝鏈夊姪浜庡悗鏈熺淮鎶わ紒
9銆佸瑙傛ā寮忥紙Facade锛?br/>澶栬妯″紡鏄负浜嗚В鍐崇被涓庣被涔嬪鐨勪緷璧栧叧绯荤殑锛屽儚spring涓€鏍凤紝鍙互灏嗙被鍜岀被涔嬮棿鐨勫叧绯婚厤缃埌閰嶇疆鏂囦欢涓紝鑰屽瑙傛ā寮忓氨鏄皢浠栦滑鐨勫叧绯绘斁鍦ㄤ竴涓狥acade绫讳腑锛岄檷浣庝簡绫荤被涔嬮棿鐨勮€﹀悎搴︼紝璇ユā寮忎腑娌℃湁娑夊強鍒版帴鍙o紝鐪嬩笅绫诲浘锛氾紙鎴戜滑浠ヤ竴涓绠楁満鐨勫惎鍔ㄨ繃绋嬩负渚嬶級
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鎴戜滑鍏堢湅涓嬪疄鐜扮被锛?br/>

 view plaincopy <br/>public class CPU {  <br/>11public void startup(){  
    System.out.println("cpu startup!");  
}  

public void shutdown(){  
    System.out.println("cpu shutdown!");  
}

}

 view plaincopy <br/>public class Memory {  <br/>11public void startup(){  
    System.out.println("memory startup!");  
}  

public void shutdown(){  
    System.out.println("memory shutdown!");  
}

}

 view plaincopy <br/>public class Disk {  <br/>11public void startup(){  
    System.out.println("disk startup!");  
}  

public void shutdown(){  
    System.out.println("disk shutdown!");  
}

}

 view plaincopy <br/>public class Computer { <br/>    private CPU cpu; <br/>    private Memory memory; <br/>    private Disk disk;  <br/>11public Computer(){  
    cpu = new CPU();  
    memory = new Memory();  
    disk = new Disk();  
}  

public void startup(){  
    System.out.println("start the computer!");  
    cpu.startup();  
    memory.startup();  
    disk.startup();  
    System.out.println("start computer finished!");  
}  

public void shutdown(){  
    System.out.println("begin to close the computer!");  
    cpu.shutdown();  
    memory.shutdown();  
    disk.shutdown();  
    System.out.println("computer closed!");  
}

}
User绫诲涓嬶細

 view plaincopy <br/>public class User {  <br/>11public static void main(String[] args) {  
    Computer computer = new Computer();  
    computer.startup();  
    computer.shutdown();  
}

}
杈撳嚭锛?br/>start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close the computer!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!
濡傛灉鎴戜滑娌℃湁Computer绫伙紝閭d箞锛孋PU銆丮emory銆丏isk浠栦滑涔嬮棿灏嗕細鐩镐簰鎸佹湁瀹炰緥锛屼骇鐢熷叧绯伙紝杩欐牱浼氶€犳垚涓ラ噸鐨勪緷璧栵紝淇敼涓€涓被锛屽彲鑳戒細甯︽潵鍏朵粬绫荤殑淇敼锛岃繖涓嶆槸鎴戜滑鎯宠鐪嬪埌鐨勶紝鏈変簡Computer绫伙紝浠栦滑涔嬮棿鐨勫叧绯昏鏀惧湪浜咰omputer绫婚噷锛岃繖鏍峰氨璧峰埌浜嗚В鑰︾殑浣滅敤锛岃繖锛屽氨鏄瑙傛ā寮忥紒
10銆佹ˉ鎺ユā寮忥紙Bridge锛?br/>妗ユ帴妯″紡灏辨槸鎶婁簨鐗╁拰鍏跺叿浣撳疄鐜板垎寮€锛屼娇浠栦滑鍙互鍚勮嚜鐙珛鐨勫彉鍖栥€傛ˉ鎺ョ殑鐢ㄦ剰鏄細灏嗘娊璞″寲涓庡疄鐜板寲瑙h€︼紝浣垮緱浜岃€呭彲浠ョ嫭绔嬪彉鍖栵紝鍍忔垜浠父鐢ㄧ殑JDBC妗riverManager涓€鏍凤紝JDBC杩涜杩炴帴鏁版嵁搴撶殑鏃跺€欙紝鍦ㄥ悇涓暟鎹簱涔嬮棿杩涜鍒囨崲锛屽熀鏈笉闇€瑕佸姩澶鐨勪唬鐮侊紝鐢氳嚦涓濇涓嶇敤鍔紝鍘熷洜灏辨槸JDBC鎻愪緵缁熶竴鎺ュ彛锛屾瘡涓暟鎹簱鎻愪緵鍚勮嚜鐨勫疄鐜帮紝鐢ㄤ竴涓彨鍋氭暟鎹簱椹卞姩鐨勭▼搴忔潵妗ユ帴灏辫浜嗐€傛垜浠潵鐪嬬湅鍏崇郴鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
瀹炵幇浠g爜锛?br/>鍏堝畾涔夋帴鍙o細

 view plaincopy <br/>public interface Sourceable { <br/>    public void method(); <br/>} <br/>鍒嗗埆瀹氫箟涓や釜瀹炵幇绫伙細<br/>1 view plaincopy <br/>public class SourceSub1 implements Sourceable {  <br/>11@Override  
public void method() {  
    System.out.println("this is the first sub!");  
}

}

 view plaincopy <br/>public class SourceSub2 implements Sourceable {  <br/>11@Override  
public void method() {  
    System.out.println("this is the second sub!");  
}

}
瀹氫箟涓€涓ˉ锛屾寔鏈塖ourceable鐨勪竴涓疄渚嬶細

 view plaincopy <br/>public abstract class Bridge { <br/>    private Sourceable source;  <br/>11public void method(){  
    source.method();  
}  

public Sourceable getSource() {  
    return source;  
}  

public void setSource(Sourceable source) {  
    this.source = source;  
}

}

 view plaincopy <br/>public class MyBridge extends Bridge { <br/>    public void method(){ <br/>        getSource().method(); <br/>    } <br/>} <br/>娴嬭瘯绫伙細<br/>1 view plaincopy <br/>public class BridgeTest {  <br/>11public static void main(String[] args) {  

    Bridge bridge = new MyBridge();  

    /*璋冪敤绗竴涓璞?/  
    Sourceable source1 = new SourceSub1();  
    bridge.setSource(source1);  
    bridge.method();  

    /*璋冪敤绗簩涓璞?/  
    Sourceable source2 = new SourceSub2();  
    bridge.setSource(source2);  
    bridge.method();  
}

}
output锛?br/>this is the first sub!
this is the second sub!
杩欐牱锛屽氨閫氳繃瀵笲ridge绫荤殑璋冪敤锛屽疄鐜颁簡瀵规帴鍙ourceable鐨勫疄鐜扮被SourceSub1鍜孲ourceSub2鐨勮皟鐢ㄣ€傛帴涓嬫潵鎴戝啀鐢讳釜鍥撅紝澶у灏卞簲璇ユ槑鐧戒簡锛屽洜涓鸿繖涓浘鏄垜浠琂DBC杩炴帴鐨勫師鐞嗭紝鏈夋暟鎹簱瀛︿範鍩虹鐨勶紝涓€缁撳悎灏遍兘鎳備簡銆?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
11銆佺粍鍚堟ā寮忥紙Composite锛?br/>缁勫悎妯″紡鏈夋椂鍙堝彨閮ㄥ垎-鏁翠綋妯″紡鍦ㄥ鐞嗙被浼兼爲褰㈢粨鏋勭殑闂鏃舵瘮杈冩柟渚匡紝鐪嬬湅鍏崇郴鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鐩存帴鏉ョ湅浠g爜锛?br/>

 view plaincopy <br/>public class TreeNode {  <br/>11private String name;  
private TreeNode parent;  
private Vector<TreeNode> children = new Vector<TreeNode>();  

public TreeNode(String name){  
    this.name = name;  
}  

public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public TreeNode getParent() {  
    return parent;  
}  

public void setParent(TreeNode parent) {  
    this.parent = parent;  
}  

//娣诲姞瀛╁瓙鑺傜偣  
public void add(TreeNode node){  
    children.add(node);  
}  

//鍒犻櫎瀛╁瓙鑺傜偣  
public void remove(TreeNode node){  
    children.remove(node);  
}  

//鍙栧緱瀛╁瓙鑺傜偣  
public Enumeration<TreeNode> getChildren(){  
    return children.elements();  
}

}

 view plaincopy <br/>public class Tree {  <br/>11TreeNode root = null;  

public Tree(String name) {  
    root = new TreeNode(name);  
}  

public static void main(String[] args) {  
    Tree tree = new Tree("A");  
    TreeNode nodeB = new TreeNode("B");  
    TreeNode nodeC = new TreeNode("C");  

    nodeB.add(nodeC);  
    tree.root.add(nodeB);  
    System.out.println("build the tree finished!");  
}

}
浣跨敤鍦烘櫙锛氬皢澶氫釜瀵硅薄缁勫悎鍦ㄤ竴璧疯繘琛屾搷浣滐紝甯哥敤浜庤〃绀烘爲褰㈢粨鏋勪腑锛屼緥濡備簩鍙夋爲锛屾暟绛夈€?br/>12銆佷韩鍏冩ā寮忥紙Flyweight锛?br/>浜厓妯″紡鐨勪富瑕佺洰鐨勬槸瀹炵幇瀵硅薄鐨勫叡浜紝鍗冲叡浜睜锛屽綋绯荤粺涓璞″鐨勬椂鍊欏彲浠ュ噺灏戝唴瀛樼殑寮€閿€锛岄€氬父涓庡伐鍘傛ā寮忎竴璧蜂娇鐢ㄣ€?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
FlyWeightFactory璐熻矗鍒涘缓鍜岀鐞嗕韩鍏冨崟鍏冿紝褰撲竴涓鎴风璇锋眰鏃讹紝宸ュ巶闇€瑕佹鏌ュ綋鍓嶅璞℃睜涓槸鍚︽湁绗﹀悎鏉′欢鐨勫璞★紝濡傛灉鏈夛紝灏辫繑鍥炲凡缁忓瓨鍦ㄧ殑瀵硅薄锛屽鏋滄病鏈夛紝鍒欏垱寤轰竴涓柊瀵硅薄锛孎lyWeight鏄秴绫汇€備竴鎻愬埌鍏变韩姹狅紝鎴戜滑寰堝鏄撹仈鎯冲埌Java閲岄潰鐨凧DBC杩炴帴姹狅紝鎯虫兂姣忎釜杩炴帴鐨勭壒鐐癸紝鎴戜滑涓嶉毦鎬荤粨鍑猴細閫傜敤浜庝綔鍏变韩鐨勪竴浜涗釜瀵硅薄锛屼粬浠湁涓€浜涘叡鏈夌殑灞炴€э紝灏辨嬁鏁版嵁搴撹繛鎺ユ睜鏉ヨ锛寀rl銆乨riverClassName銆乽sername銆乸assword鍙奷bname锛岃繖浜涘睘鎬у浜庢瘡涓繛鎺ユ潵璇撮兘鏄竴鏍风殑锛屾墍浠ュ氨閫傚悎鐢ㄤ韩鍏冩ā寮忔潵澶勭悊锛屽缓涓€涓伐鍘傜被锛屽皢涓婅堪绫讳技灞炴€т綔涓哄唴閮ㄦ暟鎹紝鍏跺畠鐨勪綔涓哄閮ㄦ暟鎹紝鍦ㄦ柟娉曡皟鐢ㄦ椂锛屽綋鍋氬弬鏁颁紶杩涙潵锛岃繖鏍峰氨鑺傜渷浜嗙┖闂达紝鍑忓皯浜嗗疄渚嬬殑鏁伴噺銆?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鐪嬩釜渚嬪瓙锛?br/>鐪嬩笅鏁版嵁搴撹繛鎺ユ睜鐨勪唬鐮侊細

 view plaincopy <br/>public class ConnectionPool {  <br/>11private Vector<Connection> pool;  

/*鍏湁灞炴€?/  
private String url = "jdbc:mysql://localhost:3306/test";  
private String username = "root";  
private String password = "root";  
private String driverClassName = "com.mysql.jdbc.Driver";  

private int poolSize = 100;  
private static ConnectionPool instance = null;  
Connection conn = null;  

/*鏋勯€犳柟娉曪紝鍋氫竴浜涘垵濮嬪寲宸ヤ綔*/  
private ConnectionPool() {  
    pool = new Vector<Connection>(poolSize);  

    for (int i = 0; i < poolSize; i++) {  
        try {  
            Class.forName(driverClassName);  
            conn = DriverManager.getConnection(url, username, password);  
            pool.add(conn);  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}  

/* 杩斿洖杩炴帴鍒拌繛鎺ユ睜 */  
public synchronized void release() {  
    pool.add(conn);  
}  

/* 杩斿洖杩炴帴姹犱腑鐨勪竴涓暟鎹簱杩炴帴 */  
public synchronized Connection getConnection() {  
    if (pool.size() > 0) {  
        Connection conn = pool.get(0);  
        pool.remove(conn);  
        return conn;  
    } else {  
        return null;  
    }  
}

}
閫氳繃杩炴帴姹犵殑绠$悊锛屽疄鐜颁簡鏁版嵁搴撹繛鎺ョ殑鍏变韩锛屼笉闇€瑕佹瘡涓€娆¢兘閲嶆柊鍒涘缓杩炴帴锛岃妭鐪佷簡鏁版嵁搴撻噸鏂板垱寤虹殑寮€閿€锛屾彁鍗囦簡绯荤粺鐨勬€ц兘锛佹湰绔犺瑙d簡7绉嶇粨鏋勫瀷妯″紡锛屽洜涓虹瘒骞呯殑闂锛屽墿涓嬬殑11绉嶈涓哄瀷妯″紡锛?
鏈珷鏄叧浜庤璁℃ā寮忕殑鏈€鍚庝竴璁诧紝浼氳鍒扮涓夌璁捐妯″紡鈥斺€旇涓哄瀷妯″紡锛屽叡11绉嶏細绛栫暐妯″紡銆佹ā鏉挎柟娉曟ā寮忋€佽瀵熻€呮ā寮忋€佽凯浠e瓙妯″紡銆佽矗浠婚摼妯″紡銆佸懡浠ゆā寮忋€佸蹇樺綍妯″紡銆佺姸鎬佹ā寮忋€佽闂€呮ā寮忋€佷腑浠嬭€呮ā寮忋€佽В閲婂櫒妯″紡銆傝繖娈垫椂闂翠竴鐩村湪鍐欏叧浜庤璁℃ā寮忕殑涓滆タ锛岀粓浜庡啓鍒颁竴鍗婁簡锛屽啓鍗氭枃鏄釜寰堣垂鏃堕棿鐨勪笢瑗匡紝鍥犱负鎴戝緱涓鸿鑰呰礋璐o紝涓嶈鏄浘杩樻槸浠g爜杩樻槸琛ㄨ堪锛岄兘甯屾湜鑳藉敖閲忓啓娓呮锛屼互渚胯鑰呯悊瑙o紝鎴戞兂涓嶈鏄垜杩樻槸璇昏€咃紝閮藉笇鏈涚湅鍒伴珮璐ㄩ噺鐨勫崥鏂囧嚭鏉ワ紝浠庢垜鏈汉鍑哄彂锛屾垜浼氫竴鐩村潥鎸佷笅鍘伙紝涓嶆柇鏇存柊锛屾簮婧愬姩鍔涙潵鑷簬璇昏€呮湅鍙嬩滑鐨勪笉鏂敮鎸侊紝鎴戜細灏借嚜宸辩殑鍔姏锛屽啓濂芥瘡涓€绡囨枃绔狅紒甯屾湜澶у鑳戒笉鏂粰鍑烘剰瑙佸拰寤鸿锛屽叡鍚屾墦閫犲畬缇庣殑鍗氭枃锛?br/>鍏堟潵寮犲浘锛岀湅鐪嬭繖11涓ā寮忕殑鍏崇郴锛?br/>绗竴绫伙細閫氳繃鐖剁被涓庡瓙绫荤殑鍏崇郴杩涜瀹炵幇銆傜浜岀被锛氫袱涓被涔嬮棿銆傜涓夌被锛氱被鐨勭姸鎬併€傜鍥涚被锛氶€氳繃涓棿绫?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
13銆佺瓥鐣ユā寮忥紙strategy锛?br/>绛栫暐妯″紡瀹氫箟浜嗕竴绯诲垪绠楁硶锛屽苟灏嗘瘡涓畻娉曞皝瑁呰捣鏉ワ紝浣夸粬浠彲浠ョ浉浜掓浛鎹紝涓旂畻娉曠殑鍙樺寲涓嶄細褰卞搷鍒颁娇鐢ㄧ畻娉曠殑瀹㈡埛銆傞渶瑕佽璁′竴涓帴鍙o紝涓轰竴绯诲垪瀹炵幇绫绘彁渚涚粺涓€鐨勬柟娉曪紝澶氫釜瀹炵幇绫诲疄鐜拌鎺ュ彛锛岃璁′竴涓娊璞$被锛堝彲鏈夊彲鏃狅紝灞炰簬杈呭姪绫伙級锛屾彁渚涜緟鍔╁嚱鏁帮紝鍏崇郴鍥惧涓嬶細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鍥句腑ICalculator鎻愪緵鍚屾剰鐨勬柟娉曪紝
AbstractCalculator鏄緟鍔╃被锛屾彁渚涜緟鍔╂柟娉曪紝鎺ヤ笅鏉ワ紝渚濇瀹炵幇涓嬫瘡涓被锛?br/>棣栧厛缁熶竴鎺ュ彛锛?br/>

 view plaincopy <br/>public interface ICalculator { <br/>    public int calculate(String exp); <br/>} <br/>杈呭姪绫伙細<br/>1 view plaincopy <br/>public abstract class AbstractCalculator {  <br/>11public int[] split(String exp,String opt){  
    String array[] = exp.split(opt);  
    int arrayInt[] = new int[2];  
    arrayInt[0] = Integer.parseInt(array[0]);  
    arrayInt[1] = Integer.parseInt(array[1]);  
    return arrayInt;  
}

}
涓変釜瀹炵幇绫伙細

 view plaincopy <br/>public class Plus extends AbstractCalculator implements ICalculator {  <br/>11@Override  
public int calculate(String exp) {  
    int arrayInt[] = split(exp,"\\+");  
    return arrayInt[0]+arrayInt[1];  
}

}

 view plaincopy <br/>public class Minus extends AbstractCalculator implements ICalculator {  <br/>11@Override  
public int calculate(String exp) {  
    int arrayInt[] = split(exp,"-");  
    return arrayInt[0]-arrayInt[1];  
}

}

 view plaincopy <br/>public class Multiply extends AbstractCalculator implements ICalculator {  <br/>11@Override  
public int calculate(String exp) {  
    int arrayInt[] = split(exp,"\\*");  
    return arrayInt[0]*arrayInt[1];  
}

}
绠€鍗曠殑娴嬭瘯绫伙細

 view plaincopy <br/>public class StrategyTest {  <br/>11public static void main(String[] args) {  
    String exp = "2+8";  
    ICalculator cal = new Plus();  
    int result = cal.calculate(exp);  
    System.out.println(result);  
}

}
杈撳嚭锛?0
绛栫暐妯″紡鐨勫喅瀹氭潈鍦ㄧ敤鎴凤紝绯荤粺鏈韩鎻愪緵涓嶅悓绠楁硶鐨勫疄鐜帮紝鏂板鎴栬€呭垹闄ょ畻娉曪紝瀵瑰悇绉嶇畻娉曞仛灏佽銆傚洜姝わ紝绛栫暐妯″紡澶氱敤鍦ㄧ畻娉曞喅绛栫郴缁熶腑锛屽閮ㄧ敤鎴峰彧闇€瑕佸喅瀹氱敤鍝釜绠楁硶鍗冲彲銆?br/>14銆佹ā鏉挎柟娉曟ā寮忥紙Template Method锛?br/>瑙i噴涓€涓嬫ā鏉挎柟娉曟ā寮忥紝灏辨槸鎸囷細涓€涓娊璞$被涓紝鏈変竴涓富鏂规硶锛屽啀瀹氫箟1鈥涓柟娉曪紝鍙互鏄娊璞$殑锛屼篃鍙互鏄疄闄呯殑鏂规硶锛屽畾涔変竴涓被锛岀户鎵胯鎶借薄绫伙紝閲嶅啓鎶借薄鏂规硶锛岄€氳繃璋冪敤鎶借薄绫伙紝瀹炵幇瀵瑰瓙绫荤殑璋冪敤锛屽厛鐪嬩釜鍏崇郴鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
灏辨槸鍦ˋbstractCalculator绫讳腑瀹氫箟涓€涓富鏂规硶calculate锛宑alculate()璋冪敤spilt()绛夛紝Plus鍜孧inus鍒嗗埆缁ф壙AbstractCalculator绫伙紝閫氳繃瀵笰bstractCalculator鐨勮皟鐢ㄥ疄鐜板瀛愮被鐨勮皟鐢紝鐪嬩笅闈㈢殑渚嬪瓙锛?br/>

 view plaincopy <br/>public abstract class AbstractCalculator {  <br/>11/*涓绘柟娉曪紝瀹炵幇瀵规湰绫诲叾瀹冩柟娉曠殑璋冪敤*/  
public final int calculate(String exp,String opt){  
    int array[] = split(exp,opt);  
    return calculate(array[0],array[1]);  
}  

/*琚瓙绫婚噸鍐欑殑鏂规硶*/  
abstract public int calculate(int num1,int num2);  

public int[] split(String exp,String opt){  
    String array[] = exp.split(opt);  
    int arrayInt[] = new int[2];  
    arrayInt[0] = Integer.parseInt(array[0]);  
    arrayInt[1] = Integer.parseInt(array[1]);  
    return arrayInt;  
}

}

 view plaincopy <br/>public class Plus extends AbstractCalculator {  <br/>11@Override  
public int calculate(int num1,int num2) {  
    return num1 + num2;  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class StrategyTest {  <br/>11public static void main(String[] args) {  
    String exp = "8+8";  
    AbstractCalculator cal = new Plus();  
    int result = cal.calculate(exp, "\\+");  
    System.out.println(result);  
}

}
鎴戣窡韪笅杩欎釜灏忕▼搴忕殑鎵ц杩囩▼锛氶鍏堝皢exp鍜屸€漒+鈥濆仛鍙傛暟锛岃皟鐢ˋbstractCalculator绫婚噷鐨刢alculate(String,String)鏂规硶锛屽湪calculate(String,String)閲岃皟鐢ㄥ悓绫荤殑split()锛屼箣鍚庡啀璋冪敤calculate(int ,int)鏂规硶锛屼粠杩欎釜鏂规硶杩涘叆鍒板瓙绫讳腑锛屾墽琛屽畬return num1 + num2鍚庯紝灏嗗€艰繑鍥炲埌AbstractCalculator绫伙紝璧嬬粰result锛屾墦鍗板嚭鏉ャ€傛濂介獙璇佷簡鎴戜滑寮€澶寸殑鎬濊矾銆?br/>15銆佽瀵熻€呮ā寮忥紙Observer锛?br/>鍖呮嫭杩欎釜妯″紡鍦ㄥ唴鐨勬帴涓嬫潵鐨勫洓涓ā寮忥紝閮芥槸绫诲拰绫讳箣闂寸殑鍏崇郴锛屼笉娑夊強鍒扮户鎵匡紝瀛︾殑鏃跺€欏簲璇?璁板緱褰掔撼锛岃寰楁湰鏂囨渶寮€濮嬬殑閭d釜鍥俱€傝瀵熻€呮ā寮忓緢濂界悊瑙o紝绫讳技浜庨偖浠惰闃呭拰RSS璁㈤槄锛屽綋鎴戜滑娴忚涓€浜涘崥瀹㈡垨wiki鏃讹紝缁忓父浼氱湅鍒癛SS鍥炬爣锛屽氨杩欑殑鎰忔€濇槸锛屽綋浣犺闃呬簡璇ユ枃绔狅紝濡傛灉鍚庣画鏈夋洿鏂帮紝浼氬強鏃堕€氱煡浣犮€傚叾瀹烇紝绠€鍗曟潵璁插氨涓€鍙ヨ瘽锛氬綋涓€涓璞″彉鍖栨椂锛屽叾瀹冧緷璧栬瀵硅薄鐨勫璞¢兘浼氭敹鍒伴€氱煡锛屽苟涓旈殢鐫€鍙樺寲锛佸璞′箣闂存槸涓€绉嶄竴瀵瑰鐨勫叧绯汇€傚厛鏉ョ湅鐪嬪叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鎴戣В閲婁笅杩欎簺绫荤殑浣滅敤锛歁ySubject绫诲氨鏄垜浠殑涓诲璞★紝Observer1鍜孫bserver2鏄緷璧栦簬MySubject鐨勫璞★紝褰揗ySubject鍙樺寲鏃讹紝Observer1鍜孫bserver2蹇呯劧鍙樺寲銆侫bstractSubject绫讳腑瀹氫箟鐫€闇€瑕佺洃鎺х殑瀵硅薄鍒楄〃锛屽彲浠ュ鍏惰繘琛屼慨鏀癸細澧炲姞鎴栧垹闄よ鐩戞帶瀵硅薄锛屼笖褰揗ySubject鍙樺寲鏃讹紝璐熻矗閫氱煡鍦ㄥ垪琛ㄥ唴瀛樺湪鐨勫璞°€傛垜浠湅瀹炵幇浠g爜锛?br/>涓€涓狾bserver鎺ュ彛锛?br/>

 view plaincopy <br/>public interface Observer { <br/>    public void update(); <br/>} <br/>涓や釜瀹炵幇绫伙細<br/>1 view plaincopy <br/>public class Observer1 implements Observer {  <br/>11@Override  
public void update() {  
    System.out.println("observer1 has received!");  
}

}

 view plaincopy <br/>public class Observer2 implements Observer {  <br/>11@Override  
public void update() {  
    System.out.println("observer2 has received!");  
}

}
Subject鎺ュ彛鍙婂疄鐜扮被锛?br/>

 view plaincopy <br/>public interface Subject {  <br/>11/*澧炲姞瑙傚療鑰?/  
public void add(Observer observer);  

/*鍒犻櫎瑙傚療鑰?/  
public void del(Observer observer);  

/*閫氱煡鎵€鏈夌殑瑙傚療鑰?/  
public void notifyObservers();  

/*鑷韩鐨勬搷浣?/  
public void operation();

}

 view plaincopy <br/>public abstract class AbstractSubject implements Subject {  <br/>11private Vector<Observer> vector = new Vector<Observer>();  
@Override  
public void add(Observer observer) {  
    vector.add(observer);  
}  

@Override  
public void del(Observer observer) {  
    vector.remove(observer);  
}  

@Override  
public void notifyObservers() {  
    Enumeration<Observer> enumo = vector.elements();  
    while(enumo.hasMoreElements()){  
        enumo.nextElement().update();  
    }  
}

}

 view plaincopy <br/>public class MySubject extends AbstractSubject {  <br/>11@Override  
public void operation() {  
    System.out.println("update self!");  
    notifyObservers();  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class ObserverTest {  <br/>11public static void main(String[] args) {  
    Subject sub = new MySubject();  
    sub.add(new Observer1());  
    sub.add(new Observer2());  

    sub.operation();  
}

}
杈撳嚭锛?br/>update self!
observer1 has received!
observer2 has received!
杩欎簺涓滆タ锛屽叾瀹炰笉闅撅紝鍙槸鏈変簺鎶借薄锛屼笉澶鏄撴暣浣撶悊瑙o紝寤鸿璇昏€咃細鏍规嵁鍏崇郴鍥撅紝鏂板缓椤圭洰锛岃嚜宸卞啓浠g爜锛堟垨鑰呭弬鑰冩垜鐨勪唬鐮侊級,鎸夌収鎬讳綋鎬濊矾璧颁竴閬嶏紝杩欐牱鎵嶈兘浣撲細瀹冪殑鎬濇兂锛岀悊瑙h捣鏉ュ鏄擄紒
16銆佽凯浠e瓙妯″紡锛圛terator锛?br/>椤惧悕鎬濅箟锛岃凯浠e櫒妯″紡灏辨槸椤哄簭璁块棶鑱氶泦涓殑瀵硅薄锛屼竴鑸潵璇达紝闆嗗悎涓潪甯稿父瑙侊紝濡傛灉瀵归泦鍚堢被姣旇緝鐔熸倝鐨勮瘽锛岀悊瑙f湰妯″紡浼氬崄鍒嗚交鏉俱€傝繖鍙ヨ瘽鍖呭惈涓ゅ眰鎰忔€濓細涓€鏄渶瑕侀亶鍘嗙殑瀵硅薄锛屽嵆鑱氶泦瀵硅薄锛屼簩鏄凯浠e櫒瀵硅薄锛岀敤浜庡鑱氶泦瀵硅薄杩涜閬嶅巻璁块棶銆傛垜浠湅涓嬪叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
杩欎釜鎬濊矾鍜屾垜浠父鐢ㄧ殑涓€妯′竴鏍凤紝MyCollection涓畾涔変簡闆嗗悎鐨勪竴浜涙搷浣滐紝MyIterator涓畾涔変簡涓€绯诲垪杩唬鎿嶄綔锛屼笖鎸佹湁Collection瀹炰緥锛屾垜浠潵鐪嬬湅瀹炵幇浠g爜锛?br/>涓や釜鎺ュ彛锛?br/>

 view plaincopy <br/>public interface Collection {  <br/>11public Iterator iterator();  

/*鍙栧緱闆嗗悎鍏冪礌*/  
public Object get(int i);  

/*鍙栧緱闆嗗悎澶у皬*/  
public int size();

}

 view plaincopy <br/>public interface Iterator { <br/>    //鍓嶇Щ <br/>    public Object previous();  <br/>11//鍚庣Щ  
public Object next();  
public boolean hasNext();  

//鍙栧緱绗竴涓厓绱? 
public Object first();

}
涓や釜瀹炵幇锛?br/>

 view plaincopy <br/>public class MyCollection implements Collection {  <br/>11public String string[] = {"A","B","C","D","E"};  
@Override  
public Iterator iterator() {  
    return new MyIterator(this);  
}  

@Override  
public Object get(int i) {  
    return string[i];  
}  

@Override  
public int size() {  
    return string.length;  
}

}

 view plaincopy <br/>public class MyIterator implements Iterator {  <br/>11private Collection collection;  
private int pos = -1;  

public MyIterator(Collection collection){  
    this.collection = collection;  
}  

@Override  
public Object previous() {  
    if(pos > 0){  
        pos--;  
    }  
    return collection.get(pos);  
}  

@Override  
public Object next() {  
    if(pos<collection.size()-1){  
        pos++;  
    }  
    return collection.get(pos);  
}  

@Override  
public boolean hasNext() {  
    if(pos<collection.size()-1){  
        return true;  
    }else{  
        return false;  
    }  
}  

@Override  
public Object first() {  
    pos = 0;  
    return collection.get(pos);  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  
    Collection collection = new MyCollection();  
    Iterator it = collection.iterator();  

    while(it.hasNext()){  
        System.out.println(it.next());  
    }  
}

}
杈撳嚭锛欰 B C D E
姝ゅ鎴戜滑璨屼技妯℃嫙浜嗕竴涓泦鍚堢被鐨勮繃绋嬶紝鎰熻鏄笉鏄緢鐖斤紵鍏跺疄JDK涓悇涓被涔熼兘鏄繖浜涘熀鏈殑涓滆タ锛屽姞涓€浜涜璁℃ā寮忥紝鍐嶅姞涓€浜涗紭鍖栨斁鍒颁竴璧风殑锛屽彧瑕佹垜浠妸杩欎簺涓滆タ瀛︿細浜嗭紝鎺屾彙濂戒簡锛屾垜浠篃鍙互鍐欏嚭鑷繁鐨勯泦鍚堢被锛岀敋鑷虫鏋讹紒
17銆佽矗浠婚摼妯″紡锛圕hain of Responsibility锛?
鎺ヤ笅鏉ユ垜浠皢瑕佽皥璋堣矗浠婚摼妯″紡锛屾湁澶氫釜瀵硅薄锛屾瘡涓璞℃寔鏈夊涓嬩竴涓璞$殑寮曠敤锛岃繖鏍峰氨浼氬舰鎴愪竴鏉¢摼锛岃姹傚湪杩欐潯閾句笂浼犻€掞紝鐩村埌鏌愪竴瀵硅薄鍐冲畾澶勭悊璇ヨ姹傘€備絾鏄彂鍑鸿€呭苟涓嶆竻妤氬埌搴曟渶缁堥偅涓璞′細澶勭悊璇ヨ姹傦紝鎵€浠ワ紝璐d换閾炬ā寮忓彲浠ュ疄鐜帮紝鍦ㄩ殣鐬掑鎴风鐨勬儏鍐典笅锛屽绯荤粺杩涜鍔ㄦ€佺殑璋冩暣銆傚厛鐪嬬湅鍏崇郴鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
Abstracthandler绫绘彁渚涗簡get鍜宻et鏂规硶锛屾柟渚縈yHandle绫昏缃拰淇敼寮曠敤瀵硅薄锛孧yHandle绫绘槸鏍稿績锛屽疄渚嬪寲鍚庣敓鎴愪竴绯诲垪鐩镐簰鎸佹湁鐨勫璞★紝鏋勬垚涓€鏉¢摼銆?br/>

 view plaincopy <br/>public interface Handler { <br/>    public void operator(); <br/>} <br/>1 view plaincopy <br/>public abstract class AbstractHandler {  <br/>11private Handler handler;  

public Handler getHandler() {  
    return handler;  
}  

public void setHandler(Handler handler) {  
    this.handler = handler;  
}

}

 view plaincopy <br/>public class MyHandler extends AbstractHandler implements Handler {  <br/>11private String name;  

public MyHandler(String name) {  
    this.name = name;  
}  

@Override  
public void operator() {  
    System.out.println(name+"deal!");  
    if(getHandler()!=null){  
        getHandler().operator();  
    }  
}

}

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  
    MyHandler h1 = new MyHandler("h1");  
    MyHandler h2 = new MyHandler("h2");  
    MyHandler h3 = new MyHandler("h3");  

    h1.setHandler(h2);  
    h2.setHandler(h3);  

    h1.operator();  
}

}
杈撳嚭锛?br/>h1deal!
h2deal!
h3deal!
姝ゅ寮鸿皟涓€鐐瑰氨鏄紝閾炬帴涓婄殑璇锋眰鍙互鏄竴鏉¢摼锛屽彲浠ユ槸涓€涓爲锛岃繕鍙互鏄竴涓幆锛屾ā寮忔湰韬笉绾︽潫杩欎釜锛岄渶瑕佹垜浠嚜宸卞幓瀹炵幇锛屽悓鏃讹紝鍦ㄤ竴涓椂鍒伙紝鍛戒护鍙厑璁哥敱涓€涓璞′紶缁欏彟涓€涓璞★紝鑰屼笉鍏佽浼犵粰澶氫釜瀵硅薄銆?br/>18銆佸懡浠ゆā寮忥紙Command锛?br/>鍛戒护妯″紡寰堝ソ鐞嗚В锛屼妇涓緥瀛愶紝鍙镐护鍛樹笅浠よ澹叺鍘诲共浠朵簨鎯咃紝浠庢暣涓簨鎯呯殑瑙掑害鏉ヨ€冭檻锛屽徃浠ゅ憳鐨勪綔鐢ㄦ槸锛屽彂鍑哄彛浠わ紝鍙d护缁忚繃浼犻€掞紝浼犲埌浜嗗+鍏佃€虫湹閲岋紝澹叺鍘绘墽琛屻€傝繖涓繃绋嬪ソ鍦紝涓夎€呯浉浜掕В鑰︼紝浠讳綍涓€鏂归兘涓嶇敤鍘讳緷璧栧叾浠栦汉锛屽彧闇€瑕佸仛濂借嚜宸辩殑浜嬪効灏辫锛屽徃浠ゅ憳瑕佺殑鏄粨鏋滐紝涓嶄細鍘诲叧娉ㄥ埌搴曞+鍏垫槸鎬庝箞瀹炵幇鐨勩€傛垜浠湅鐪嬪叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
Invoker鏄皟鐢ㄨ€咃紙鍙镐护鍛橈級锛孯eceiver鏄璋冪敤鑰咃紙澹叺锛夛紝MyCommand鏄懡浠わ紝瀹炵幇浜咰ommand鎺ュ彛锛屾寔鏈夋帴鏀跺璞★紝鐪嬪疄鐜颁唬鐮侊細

 view plaincopy <br/>public interface Command { <br/>    public void exe(); <br/>} <br/>1 view plaincopy <br/>public class MyCommand implements Command {  <br/>11private Receiver receiver;  

public MyCommand(Receiver receiver) {  
    this.receiver = receiver;  
}  

@Override  
public void exe() {  
    receiver.action();  
}

}

 view plaincopy <br/>public class Receiver { <br/>    public void action(){ <br/>        System.out.println(鈥渃ommand received!鈥?; <br/>    } <br/>} <br/>1 view plaincopy <br/>public class Invoker {  <br/>11private Command command;  

public Invoker(Command command) {  
    this.command = command;  
}  

public void action(){  
    command.exe();  
}

}

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  
    Receiver receiver = new Receiver();  
    Command cmd = new MyCommand(receiver);  
    Invoker invoker = new Invoker(cmd);  
    invoker.action();  
}

}
杈撳嚭锛歝ommand received!
杩欎釜寰堝搱鐞嗚В锛屽懡浠ゆā寮忕殑鐩殑灏辨槸杈惧埌鍛戒护鐨勫彂鍑鸿€呭拰鎵ц鑰呬箣闂磋В鑰︼紝瀹炵幇璇锋眰鍜屾墽琛屽垎寮€锛岀啛鎮塖truts鐨勫悓瀛﹀簲璇ョ煡閬擄紝Struts鍏跺疄灏辨槸涓€绉嶅皢璇锋眰鍜屽憟鐜板垎绂荤殑鎶€鏈紝鍏朵腑蹇呯劧娑夊強鍛戒护妯″紡鐨勬€濇兂锛?br/>鍏跺疄姣忎釜璁捐妯″紡閮芥槸寰堥噸瑕佺殑涓€绉嶆€濇兂锛岀湅涓婂幓寰堢啛锛屽叾瀹炴槸鍥犱负鎴戜滑鍦ㄥ鍒扮殑涓滆タ涓兘鏈夋秹鍙婏紝灏界鏈夋椂鎴戜滑骞朵笉鐭ラ亾锛屽叾瀹炲湪Java鏈韩鐨勮璁′箣涓澶勯兘鏈変綋鐜帮紝鍍廇WT銆丣DBC銆侀泦鍚堢被銆両O绠¢亾鎴栬€呮槸Web妗嗘灦锛岄噷闈㈣璁℃ā寮忔棤澶勪笉鍦ㄣ€傚洜涓烘垜浠瘒骞呮湁闄愶紝寰堥毦璁叉瘡涓€涓璁℃ā寮忛兘璁茬殑寰堣缁嗭紝涓嶈繃鎴戜細灏芥垜鎵€鑳斤紝灏介噺鍦ㄦ湁闄愮殑绌洪棿鍜岀瘒骞呭唴锛屾妸鎰忔€濆啓娓呮浜嗭紝鏇村ソ璁╁ぇ瀹舵槑鐧姐€傛湰绔犱笉鍑烘剰澶栫殑璇濓紝搴旇鏄璁℃ā寮忔渶鍚庝竴璁蹭簡锛岄鍏堣繕鏄笂涓€绡囧紑澶寸殑閭d釜鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鏈珷璁茶绗笁绫诲拰绗洓绫汇€?br/>19銆佸蹇樺綍妯″紡锛圡emento锛?br/>涓昏鐩殑鏄繚瀛樹竴涓璞$殑鏌愪釜鐘舵€侊紝浠ヤ究鍦ㄩ€傚綋鐨勬椂鍊欐仮澶嶅璞★紝涓汉瑙夊緱鍙浠芥ā寮忔洿褰㈣薄浜涳紝閫氫織鐨勮涓嬶細鍋囪鏈夊師濮嬬被A锛孉涓湁鍚勭灞炴€э紝A鍙互鍐冲畾闇€瑕佸浠界殑灞炴€э紝澶囧繕褰曠被B鏄敤鏉ュ瓨鍌ˋ鐨勪竴浜涘唴閮ㄧ姸鎬侊紝绫籆鍛紝灏辨槸涓€涓敤鏉ュ瓨鍌ㄥ蹇樺綍鐨勶紝涓斿彧鑳藉瓨鍌紝涓嶈兘淇敼绛夋搷浣溿€傚仛涓浘鏉ュ垎鏋愪竴涓嬶細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
Original绫绘槸鍘熷绫伙紝閲岄潰鏈夐渶瑕佷繚瀛樼殑灞炴€alue鍙婂垱寤轰竴涓蹇樺綍绫伙紝鐢ㄦ潵淇濆瓨value鍊笺€侻emento绫绘槸澶囧繕褰曠被锛孲torage绫绘槸瀛樺偍澶囧繕褰曠殑绫伙紝鎸佹湁Memento绫荤殑瀹炰緥锛岃妯″紡寰堝ソ鐞嗚В銆傜洿鎺ョ湅婧愮爜锛?br/>

 view plaincopy <br/>public class Original {  <br/>11private String value;  

public String getValue() {  
    return value;  
}  

public void setValue(String value) {  
    this.value = value;  
}  

public Original(String value) {  
    this.value = value;  
}  

public Memento createMemento(){  
    return new Memento(value);  
}  

public void restoreMemento(Memento memento){  
    this.value = memento.getValue();  
}

}

 view plaincopy <br/>public class Memento {  <br/>11private String value;  

public Memento(String value) {  
    this.value = value;  
}  

public String getValue() {  
    return value;  
}  

public void setValue(String value) {  
    this.value = value;  
}

}

 view plaincopy <br/>public class Storage {  <br/>11private Memento memento;  

public Storage(Memento memento) {  
    this.memento = memento;  
}  

public Memento getMemento() {  
    return memento;  
}  

public void setMemento(Memento memento) {  
    this.memento = memento;  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  

    // 鍒涘缓鍘熷绫? 
    Original origi = new Original("egg");  

    // 鍒涘缓澶囧繕褰? 
    Storage storage = new Storage(origi.createMemento());  

    // 淇敼鍘熷绫荤殑鐘舵€? 
    System.out.println("鍒濆鍖栫姸鎬佷负锛? + origi.getValue());  
    origi.setValue("niu");  
    System.out.println("淇敼鍚庣殑鐘舵€佷负锛? + origi.getValue());  

    // 鍥炲鍘熷绫荤殑鐘舵€? 
    origi.restoreMemento(storage.getMemento());  
    System.out.println("鎭㈠鍚庣殑鐘舵€佷负锛? + origi.getValue());  
}

}
杈撳嚭锛?br/>鍒濆鍖栫姸鎬佷负锛歟gg
淇敼鍚庣殑鐘舵€佷负锛歯iu
鎭㈠鍚庣殑鐘舵€佷负锛歟gg
绠€鍗曟弿杩颁笅锛氭柊寤哄師濮嬬被鏃讹紝value琚垵濮嬪寲涓篹gg锛屽悗缁忚繃淇敼锛屽皢value鐨勫€肩疆涓簄iu锛屾渶鍚庡€掓暟绗簩琛岃繘琛屾仮澶嶇姸鎬侊紝缁撴灉鎴愬姛鎭㈠浜嗐€傚叾瀹炴垜瑙夊緱杩欎釜妯″紡鍙€滃浠?鎭㈠鈥濇ā寮忔渶褰㈣薄銆?br/>20銆佺姸鎬佹ā寮忥紙State锛?br/>鏍稿績鎬濇兂灏辨槸锛氬綋瀵硅薄鐨勭姸鎬佹敼鍙樻椂锛屽悓鏃舵敼鍙樺叾琛屼负锛屽緢濂界悊瑙o紒灏辨嬁QQ鏉ヨ锛屾湁鍑犵鐘舵€侊紝鍦ㄧ嚎銆侀殣韬€佸繖纰岀瓑锛屾瘡涓姸鎬佸搴斾笉鍚岀殑鎿嶄綔锛岃€屼笖浣犵殑濂藉弸涔熻兘鐪嬪埌浣犵殑鐘舵€侊紝鎵€浠ワ紝鐘舵€佹ā寮忓氨涓ょ偣锛?銆佸彲浠ラ€氳繃鏀瑰彉鐘舵€佹潵鑾峰緱涓嶅悓鐨勮涓恒€?銆佷綘鐨勫ソ鍙嬭兘鍚屾椂鐪嬪埌浣犵殑鍙樺寲銆傜湅鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
State绫绘槸涓姸鎬佺被锛孋ontext绫诲彲浠ュ疄鐜板垏鎹紝鎴戜滑鏉ョ湅鐪嬩唬鐮侊細

 view plaincopy <br/>package com.xtfggef.dp.state;  <br/>/**  <br/> * 鐘舵€佺被鐨勬牳蹇冪被  <br/> * 2012-12-1  <br/> * @author erqing  <br/> *  <br/> */ <br/>public class State {  <br/>11private String value;  

public String getValue() {  
    return value;  
}  

public void setValue(String value) {  
    this.value = value;  
}  

public void method1(){  
    System.out.println("execute the first opt!");  
}  

public void method2(){  
    System.out.println("execute the second opt!");  
}

}

 view plaincopy <br/>package com.xtfggef.dp.state;  <br/>/**  <br/> * 鐘舵€佹ā寮忕殑鍒囨崲绫?  2012-12-1  <br/> * @author erqing  <br/> * <br/> */ <br/>public class Context {  <br/>11private State state;  

public Context(State state) {  
    this.state = state;  
}  

public State getState() {  
    return state;  
}  

public void setState(State state) {  
    this.state = state;  
}  

public void method() {  
    if (state.getValue().equals("state1")) {  
        state.method1();  
    } else if (state.getValue().equals("state2")) {  
        state.method2();  
    }  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  

    State state = new State();  
    Context context = new Context(state);  

    //璁剧疆绗竴绉嶇姸鎬? 
    state.setValue("state1");  
    context.method();  

    //璁剧疆绗簩绉嶇姸鎬? 
    state.setValue("state2");  
    context.method();  
}

}
杈撳嚭锛?br/>execute the first opt!
execute the second opt!
鏍规嵁杩欎釜鐗规€э紝鐘舵€佹ā寮忓湪鏃ュ父寮€鍙戜腑鐢ㄧ殑鎸哄鐨勶紝灏ゅ叾鏄仛缃戠珯鐨勬椂鍊欙紝鎴戜滑鏈夋椂甯屾湜鏍规嵁瀵硅薄鐨勬煇涓€灞炴€э紝鍖哄埆寮€浠栦滑鐨勪竴浜涘姛鑳斤紝姣斿璇寸畝鍗曠殑鏉冮檺鎺у埗绛夈€?
21銆佽闂€呮ā寮忥紙Visitor锛?br/>璁块棶鑰呮ā寮忔妸鏁版嵁缁撴瀯鍜屼綔鐢ㄤ簬缁撴瀯涓婄殑鎿嶄綔瑙h€﹀悎锛屼娇寰楁搷浣滈泦鍚堝彲鐩稿鑷敱鍦版紨鍖栥€傝闂€呮ā寮忛€傜敤浜庢暟鎹粨鏋勭浉瀵圭ǔ瀹氱畻娉曞張鏄撳彉鍖栫殑绯荤粺銆傚洜涓鸿闂€呮ā寮忎娇寰楃畻娉曟搷浣滃鍔犲彉寰楀鏄撱€傝嫢绯荤粺鏁版嵁缁撴瀯瀵硅薄鏄撲簬鍙樺寲锛岀粡甯告湁鏂扮殑鏁版嵁瀵硅薄澧炲姞杩涙潵锛屽垯涓嶉€傚悎浣跨敤璁块棶鑰呮ā寮忋€傝闂€呮ā寮忕殑浼樼偣鏄鍔犳搷浣滃緢瀹规槗锛屽洜涓哄鍔犳搷浣滄剰鍛崇潃澧炲姞鏂扮殑璁块棶鑰呫€傝闂€呮ā寮忓皢鏈夊叧琛屼负闆嗕腑鍒颁竴涓闂€呭璞′腑锛屽叾鏀瑰彉涓嶅奖鍝嶇郴缁熸暟鎹粨鏋勩€傚叾缂虹偣灏辨槸澧炲姞鏂扮殑鏁版嵁缁撴瀯寰堝洶闅俱€傗€斺€?From 鐧剧
绠€鍗曟潵璇达紝璁块棶鑰呮ā寮忓氨鏄竴绉嶅垎绂诲璞℃暟鎹粨鏋勪笌琛屼负鐨勬柟娉曪紝閫氳繃杩欑鍒嗙锛屽彲杈惧埌涓轰竴涓璁块棶鑰呭姩鎬佹坊鍔犳柊鐨勬搷浣滆€屾棤闇€鍋氬叾瀹冪殑淇敼鐨勬晥鏋溿€傜畝鍗曞叧绯诲浘锛?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
鏉ョ湅鐪嬪師鐮侊細涓€涓猇isitor绫伙紝瀛樻斁瑕佽闂殑瀵硅薄锛?br/>

 view plaincopy <br/>public interface Visitor { <br/>    public void visit(Subject sub); <br/>} <br/>1 view plaincopy <br/>public class MyVisitor implements Visitor {  <br/>11@Override  
public void visit(Subject sub) {  
    System.out.println("visit the subject锛?+sub.getSubject());  
}

}
Subject绫伙紝accept鏂规硶锛屾帴鍙楀皢瑕佽闂畠鐨勫璞★紝getSubject()鑾峰彇灏嗚琚闂殑灞炴€э紝

 view plaincopy <br/>public interface Subject { <br/>    public void accept(Visitor visitor); <br/>    public String getSubject(); <br/>} <br/>1 view plaincopy <br/>public class MySubject implements Subject {  <br/>11@Override  
public void accept(Visitor visitor) {  
    visitor.visit(this);  
}  

@Override  
public String getSubject() {  
    return "love";  
}

}
娴嬭瘯锛?

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  

    Visitor visitor = new MyVisitor();  
    Subject sub = new MySubject();  
    sub.accept(visitor);      
}

}
杈撳嚭锛歷isit the subject锛歭ove
璇ユā寮忛€傜敤鍦烘櫙锛氬鏋滄垜浠兂涓轰竴涓幇鏈夌殑绫诲鍔犳柊鍔熻兘锛屼笉寰椾笉鑰冭檻鍑犱釜浜嬫儏锛?銆佹柊鍔熻兘浼氫笉浼氫笌鐜版湁鍔熻兘鍑虹幇鍏煎鎬ч棶棰橈紵2銆佷互鍚庝細涓嶄細鍐嶉渶瑕佹坊鍔狅紵3銆佸鏋滅被涓嶅厑璁镐慨鏀逛唬鐮佹€庝箞鍔烇紵闈㈠杩欎簺闂锛屾渶濂界殑瑙e喅鏂规硶灏辨槸浣跨敤璁块棶鑰呮ā寮忥紝璁块棶鑰呮ā寮忛€傜敤浜庢暟鎹粨鏋勭浉瀵圭ǔ瀹氱殑绯荤粺锛屾妸鏁版嵁缁撴瀯鍜岀畻娉曡В鑰︼紝
22銆佷腑浠嬭€呮ā寮忥紙Mediator锛?br/>涓粙鑰呮ā寮忎篃鏄敤鏉ラ檷浣庣被绫讳箣闂寸殑鑰﹀悎鐨勶紝鍥犱负濡傛灉绫荤被涔嬮棿鏈変緷璧栧叧绯荤殑璇濓紝涓嶅埄浜庡姛鑳界殑鎷撳睍鍜岀淮鎶わ紝鍥犱负鍙淇敼涓€涓璞★紝鍏跺畠鍏宠仈鐨勫璞¢兘寰楄繘琛屼慨鏀广€傚鏋滀娇鐢ㄤ腑浠嬭€呮ā寮忥紝鍙渶鍏冲績鍜孧ediator绫荤殑鍏崇郴锛屽叿浣撶被绫讳箣闂寸殑鍏崇郴鍙婅皟搴︿氦缁橫ediator灏辫锛岃繖鏈夌偣鍍弒pring瀹瑰櫒鐨勪綔鐢ㄣ€傚厛鐪嬬湅鍥撅細
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
User绫荤粺涓€鎺ュ彛锛孶ser1鍜孶ser2鍒嗗埆鏄笉鍚岀殑瀵硅薄锛屼簩鑰呬箣闂存湁鍏宠仈锛屽鏋滀笉閲囩敤涓粙鑰呮ā寮忥紝鍒欓渶瑕佷簩鑰呯浉浜掓寔鏈夊紩鐢紝杩欐牱浜岃€呯殑鑰﹀悎搴﹀緢楂橈紝涓轰簡瑙h€︼紝寮曞叆浜哅ediator绫伙紝鎻愪緵缁熶竴鎺ュ彛锛孧yMediator涓哄叾瀹炵幇绫伙紝閲岄潰鎸佹湁User1鍜孶ser2鐨勫疄渚嬶紝鐢ㄦ潵瀹炵幇瀵筓ser1鍜孶ser2鐨勬帶鍒躲€傝繖鏍稶ser1鍜孶ser2涓や釜瀵硅薄鐩镐簰鐙珛锛屼粬浠彧闇€瑕佷繚鎸佸ソ鍜孧ediator涔嬮棿鐨勫叧绯诲氨琛岋紝鍓╀笅鐨勫叏鐢盡yMediator绫绘潵缁存姢锛佸熀鏈疄鐜帮細

 view plaincopy <br/>public interface Mediator { <br/>    public void createMediator(); <br/>    public void workAll(); <br/>} <br/>1 view plaincopy <br/>public class MyMediator implements Mediator {  <br/>11private User user1;  
private User user2;  

public User getUser1() {  
    return user1;  
}  

public User getUser2() {  
    return user2;  
}  

@Override  
public void createMediator() {  
    user1 = new User1(this);  
    user2 = new User2(this);  
}  

@Override  
public void workAll() {  
    user1.work();  
    user2.work();  
}

}

 view plaincopy <br/>public abstract class User {  <br/>11private Mediator mediator;  

public Mediator getMediator(){  
    return mediator;  
}  

public User(Mediator mediator) {  
    this.mediator = mediator;  
}  

public abstract void work();

}

 view plaincopy <br/>public class User1 extends User {  <br/>11public User1(Mediator mediator){  
    super(mediator);  
}  

@Override  
public void work() {  
    System.out.println("user1 exe!");  
}

}

 view plaincopy <br/>public class User2 extends User {  <br/>11public User2(Mediator mediator){  
    super(mediator);  
}  

@Override  
public void work() {  
    System.out.println("user2 exe!");  
}

}
娴嬭瘯绫伙細

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  
    Mediator mediator = new MyMediator();  
    mediator.createMediator();  
    mediator.workAll();  
}

}
杈撳嚭锛?
user1 exe!
user2 exe!
23銆佽В閲婂櫒妯″紡锛圛nterpreter锛?
瑙i噴鍣ㄦā寮忔槸鎴戜滑鏆傛椂鐨勬渶鍚庝竴璁诧紝涓€鑸富瑕佸簲鐢ㄥ湪OOP寮€鍙戜腑鐨勭紪璇戝櫒鐨勫紑鍙戜腑锛屾墍浠ラ€傜敤闈㈡瘮杈冪獎銆?
Java寮€鍙戜腑鐨?3绉嶈璁℃ā寮忚瑙o紙杞級 – 鐖辩▼搴忕綉-飞网
Context绫绘槸涓€涓笂涓嬫枃鐜绫伙紝Plus鍜孧inus鍒嗗埆鏄敤鏉ヨ绠楃殑瀹炵幇锛屼唬鐮佸涓嬶細

 view plaincopy <br/>public interface Expression { <br/>    public int interpret(Context context); <br/>} <br/>1 view plaincopy <br/>public class Plus implements Expression {  <br/>11@Override  
public int interpret(Context context) {  
    return context.getNum1()+context.getNum2();  
}

}

 view plaincopy <br/>public class Minus implements Expression {  <br/>11@Override  
public int interpret(Context context) {  
    return context.getNum1()-context.getNum2();  
}

}

 view plaincopy <br/>public class Context {  <br/>11private int num1;  
private int num2;  

public Context(int num1, int num2) {  
    this.num1 = num1;  
    this.num2 = num2;  
}  

public int getNum1() {  
    return num1;  
}  
public void setNum1(int num1) {  
    this.num1 = num1;  
}  
public int getNum2() {  
    return num2;  
}  
public void setNum2(int num2) {  
    this.num2 = num2;  
}

}

 view plaincopy <br/>public class Test {  <br/>11public static void main(String[] args) {  

    // 璁$畻9+2-8鐨勫€? 
    int result = new Minus().interpret((new Context(new Plus()  
            .interpret(new Context(9, 2)), 8)));  
    System.out.println(result);  
}

}
鏈€鍚庤緭鍑烘纭殑缁撴灉锛?銆?br/>鍩烘湰灏辫繖鏍凤紝瑙i噴鍣ㄦā寮忕敤鏉ュ仛鍚勭鍚勬牱鐨勮В閲婂櫒锛屽姝e垯琛ㄨ揪寮忕瓑鐨勮В閲婂櫒绛夌瓑锛?br/>璧勬簮
鍘熸枃
杞笘鍦板潃
java璁捐妯″紡designpattern闈㈠悜瀵硅薄

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。