µ¥Àýģʽ(Singleton) JavaʵÏÖ
µ¥ÀýģʽµÄʵÏÖ·½Ê½ÓкܶàÖÖ£¬ÔÚÕâÀï½éÉÜ×î¼òµ¥µÄÁ½ÖÖ¡£
µ¥ÀýģʽÄܹ»È·±£Ò»¸öÀàÖ»ÓÐÒ»¸öʵÀý£¨Õý³£Çé¿öÎÒÃÇ¿ÉÒÔnew³öÒ»¸öÀàµÄÎÞÊý¸öʵÀý£©£¬×ÔÐÐÌṩÕâ¸öʵÀý²¢ÏòÕû¸öϵͳÌṩÕâ¸öʵÀý¡£ÏÂÃæ½éÉܵ¥ÀýģʽµÄʵÏÖ·½·¨£º
1. µ¥ÀýģʽÓÐÁ½ÖÖʵÏÖ·½Ê½£ºÒ»ÖÖÊǽ«ÀàµÄ¹¹Ôì·¨·½·¨Ë½Óл¯£¬ÓÃÒ»¸ö˽ÓеÄÀà±äÁ¿instance±£´æÀàµÄʵÀý£¬ÔÚ¼ÓÔØÀàʱ£¬´´½¨ÀàµÄʵÀý£¬²¢½«ÊµÀý¸³¸øinstance£¬ÔÙÌṩһ¸ö¹¤Óѵľ²Ì¬·½·¨getInstanceÓÃÓÚ»ñÈ¡ÀàµÄΨһʵÀý£¬¸Ã·½·¨Ö±½Ó·µ»Øinstance¡£ÁíÒ»ÖÖÊǽ«ÀàµÄ¹¹Ôì·½·¨Ë½Óл¯£¬ÓÃÒ»¸ö˽ÓеÄÀàµÄ±äÁ¿instance±£´æÀàµÄʵÀý£¬µ½´ËΪֹ¶¼ÓëµÚÒ»ÖÖÇé¿öÏàͬ£¬µ«ÊÇÔÚ¼ÓÔØÀàʱ£¬½«null¸³¸øinstance¶ø²»ÊÇ´´½¨ÀàµÄʵÀý£¬ÔÙÌṩһ¸öÀàµÄ¹¤Óѵľ²Ì¬·½·¨getInstance£¬ÓÃÓÚ»ñÈ¡ÀàµÄΨһʵÀý£¬¸Ã·½·¨Ê×ÏÈÅжÏinstanceÊÇ·ñΪnull£¬Èç¹ûΪnull£¬Ôò´´½¨ÊµÀý¶ÔÏ󣬷ñÔò£¬Ö±½Ó·µ»Øinstance¡£
2. Á½ÖÖ·½·¨µÄÇø±ðÔÚÓÚ£ºÇ°ÕßÔÚÀà±»¼ÓÔØʱ£¬ÀàµÄΨһʵÀý¾Í±»´´½¨³öÀ´ÁË£¬ºóÕßÔÚµÚÒ»¸öµ÷ÓÃgetInstance()·½·¨Ê±£¬ÀàµÄΨһʵÀý²Å±»´´½¨£¬µ«ÐèÒªÔÚgetInstance()·½·¨µÄÉùÃ÷ÖÐʹÓÃsynchronized¹Ø¼ü×Ö£¬±£Ö¤Ä³Ò»Ê±¿ÌÖ»ÓÐÒ»¸öÏ̵߳÷Óô˷½·¨£¬Èç¹û²»Ê¹ÓÃsynchronized¹Ø¼ü×ֵĻ°£¬¾ÍÓпÉÄܳöÏÖÓÐÁ½¸öÏß³Ìͬʱ·ÃÎÊgetInstance·½·¨£¬Èç¹û´ËʱinstanceÇ¡ºÃΪnull£¬ÔòÁ½¸öÏ̶߳¼»áÈ¥´´½¨Õâ¸öÀàµÄʵÀý£¬Õâ¾Í²»·ûºÏµ¥ÀýģʽµÄ˼ÏëÁË¡£
´úÂëʵÏÖ£ºÊ×ÏȽ¨Á¢Ò»¸öÎļþTestSingleton.java£¬È»ºó½«ÏÂÃæµÄ²âÊÔ´úÂ뿽±´µ½ÎļþÖÐÔËÐС£
class OneSingleton {
private static OneSingleton instance = new OneSingleton();
private static int number = 0;
private OneSingleton() {}
public static OneSingleton getInstance() {
return instance;
}
public synchronized int getNumber() {
return number;
}
public synchronized void nextNumber() {
number++;
}
}
class TwoSingleton {
private static TwoSingleton instance = null;
private static int number = 0;
private TwoSingleton() {}
public static synchronized TwoSingleton getInstance() {
if(instance == null) {
instance = new
Ïà¹ØÎĵµ£º
¡¡¡¡ÄÇôҪ³ÉΪ¼Ü¹¹Ê¦µÄ;¾¶ËƺõÖ»ÓÐÏÖÔÚ½ÏΪÁ÷ÐеÄÈí¼þѧԺºÍ¸öÈË×ÔÎÒÅàÑøÁË¡£¹ØÓÚÈí¼þѧԺÎÒ½Ó´¥¹ý²»ÉÙ£¬Æä×ÚÖ¼¾ø´ó²¿·Ö¶¼ÊÇÔì¾Í(or´òÔì)ÆóÒµ ÐèÒªµÄÈí¼þ¼Ü¹¹Ê¦(or³ÌÐòÔ±orÈ˲Å)¡£½ÌʦÀ´Ô´ÓëÆóÒµ¡¢Ñ§Ô±À´Ô´ÓëÆóÒµ¡¢È˲ÅÊäË͵½ÆóÒµÊÇËûÃÇ°ìѧµÄÊֶΡ£¾¡¹Ü¸÷¸öÈçÓêºó´ºËñ°ã³öÏÖµÄÈí¼þѧԺ¿ÚºÅ²î ²»¶à£¬µ«¿ÖÅ´ó¶àÖ»ÊÇΪÁËȦ ......
1¡¢StringÊÇ×î»ù±¾µÄÊý¾ÝÀàÐÍÂð?
»ù±¾Êý¾ÝÀàÐÍ°üÀ¨byte¡¢int¡¢char¡¢long¡¢float¡¢double¡¢booleanºÍshort¡£
java.lang.StringÀàÊÇfinalÀàÐ͵ģ¬Òò´Ë²»¿ÉÒԼ̳ÐÕâ¸öÀà¡¢²»ÄÜÐÞ¸ÄÕâ¸öÀࡣΪÁËÌá¸ßЧÂʽÚÊ¡¿Õ¼ä£¬ÎÒÃÇÓ¦¸ÃÓÃStringBufferÀà
2¡¢int ºÍ Integer ÓÐʲôÇø±ð
Java ÌṩÁ½ÖÖ²»Í¬µÄÀàÐÍ£ºÒýÓÃÀàÐͺÍÔʼÀàÐÍ£¨ ......
°æȨËùÓУ¬×ªÔØ×¢Ã÷³ö´¦£¡
תÔØÇëЯ´ø´ËÐÅÏ¢£ºhuangfox
-----------------------------------------------------------------
³éÏ󹤳§ºÍ¹¤³§·½·¨ ¸Õ¿ªÊ¼ºÜÈÝÒ×»ìÏý£¬ÖÁÉÙÎÒÊÇÕâÑùµÄ£¡
²»¹ýËûÃDzúÉúµÄÔµÓÉÀ´¿´£¬»¹ÊÇ¿ÉÒԺúÃÇø·ÖµÄ¡£
1.¹¤³§·½·¨£ºÊÇΪÁ˱ÜÃâÓ¦ÓÃÖ±½ÓʵÀýÀà²ã´Î½á¹¹ÖеľßÌåÀ࣬Ìṩһ¸ö·½·¨¸ù¾ÝÔËÐÐÇé ......
¾³£Óõ½µç¿ÏÂÔØ×ÊÁÏ,һЩhttpÄã¸ù±¾ÕÒ²»µ½µÄ×ÊÔ´,µ±È»ËÙ¶ÈÊDz»Äܹ§Î¬ÁË,¿×Óô,¸ÉµÄ¾ÍÊÇÂý»î,ÈÈÃÅ×ÊÔ´³ýÍâ…
ÎÒÓõÄverycd°æµÄµç¿,Ô°æµç¿µÄÒ»ÖÖmods,µç¿òËÆÓÐN¶à°æ±¾,²»¹ýÎÒ¾õµÃverycdµÄ¾Í¹»ÓÃÁË,±Ï¾¹¹úÄڰ˳ɵÄÈ˶¼ÔÚÓÃ.
×î½üÔÚÏÂж«·½µÄиÅÄîÓ¢Óï½Ì³Ì,½ø¶È87%,ÑÛ¿´ÒªÏÂÍêÁË,µÈÒªµÈ1¸ö¶àСʱ,ÏÂÔØÍê³ÉÓ ......
Java Cglib
1.CGLIB°üµÄ½éÉÜ
´úÀíΪ¿ØÖÆÒª·ÃÎʵÄÄ¿±ê¶ÔÏóÌṩÁËÒ»ÖÖ;¾¶¡£µ±·ÃÎʶÔÏóʱ£¬ËüÒýÈëÁËÒ»¸ö¼ä½ÓµÄ²ã¡£JDK×Ô´Ó1.3°æ±¾¿ªÊ¼£¬¾ÍÒýÈëÁ˶¯Ì¬´úÀí£¬²¢ÇÒ¾³£±»ÓÃÀ´¶¯Ì¬µØ´´½¨´úÀí¡£JDKµÄ¶¯Ì¬´úÀíÓÃÆðÀ´·Ç³£¼òµ¥£¬µ±ËüÓÐÒ»¸öÏÞÖÆ£¬¾ÍÊÇʹÓö¯Ì¬´úÀíµÄ¶ÔÏó±ØÐëʵÏÖÒ»¸ö»ò¶à¸ö½Ó¿Ú¡£Èç¹ûÏë´úÀ ......