µ¥Àýģʽ(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
Ïà¹ØÎĵµ£º
1¡¢StringÊÇ×î»ù±¾µÄÊý¾ÝÀàÐÍÂð?
»ù±¾Êý¾ÝÀàÐÍ°üÀ¨byte¡¢int¡¢char¡¢long¡¢float¡¢double¡¢booleanºÍshort¡£
java.lang.StringÀàÊÇfinalÀàÐ͵ģ¬Òò´Ë²»¿ÉÒԼ̳ÐÕâ¸öÀà¡¢²»ÄÜÐÞ¸ÄÕâ¸öÀࡣΪÁËÌá¸ßЧÂʽÚÊ¡¿Õ¼ä£¬ÎÒÃÇÓ¦¸ÃÓÃStringBufferÀà
2¡¢int ºÍ Integer ÓÐʲôÇø±ð
Java ÌṩÁ½ÖÖ²»Í¬µÄÀàÐÍ£ºÒýÓÃÀàÐͺÍÔʼÀàÐÍ£¨ ......
½ñÌ츴ϰÁËÒ»ÏÂÕâЩ֪ʶ£¬Ë³±ã×öÁËϱʼÇ.
1.¼Ä´æÆ÷£º×î¿ìµÄ´æ´¢Çø, ÓɱàÒëÆ÷¸ù¾ÝÐèÇó½øÐзÖÅä,ÎÒÃÇÔÚ³ÌÐòÖÐÎÞ·¨¿ØÖÆ.
2. Õ»£º´æ·Å»ù±¾ÀàÐ͵ıäÁ¿Êý¾ÝºÍ¶ÔÏóµÄÒýÓ㬵«¶ÔÏó±¾Éí²»´æ·ÅÔÚÕ»ÖУ¬¶øÊÇ´æ·ÅÔڶѣ¨new ³öÀ´µÄ¶ÔÏ󣩻òÕß³£Á¿³ØÖУ¨×Ö·û´®³£Á¿¶ÔÏó´æ·ÅÔÚ³£Á¿³ØÖС££©
3. ¶Ñ£º´æ·ÅËùÓÐnew³öÀ´µÄ¶ÔÏó¡£
4. ¾²Ì¬Óò£ ......
ÃæÏò¶ÔÏó³ÌÐòÉè¼Æ¹²ÓÐÈý´óÌØÐÔ£º·â×°¡¢¼Ì³Ð¡¢¶à̬£»
ÕâÈý´óÌØÐÔÖ®¼äÏ໥¹ØÁª£¬ÆäÖзâ×°ÐÔÊÇÃæÏò¶ÔÏóµÄ»ù´¡£¬¼Ì³ÐÐÔÊÇÈí¼þÖØÓõĹؼü¡£¶ø¶à̬ÐÔÔò±ØÐë´æÔÚÓڼ̳еĻ·¾³Ö®ÖУ¬ÊǶÔÃæÏò¶ÔÏó³ÌÐòÉè¼ÆµÄ²¹³ä¡£
·â×°£»
Java5.0ÐÂÌØÐÔ£»»ù±¾Êý¾ÝÀàÐÍà·â×°Àà ·âÏ䣻 ·â×°Ààà»ù±¾Êý¾ÝÀàÐÍ ²ðÏ䣻
package review;
......
¾ÍÊÇÒªµ½sunµÄÍøÕ¾ÏÂÔØJDK£¬jdk-6u10-beta-windows-i586-p.exe£¬ÎÒÓõÄÊÇÕâ¸ö¡£
»¹ÊÇÔÚsunµÄÍøÕ¾ÏÂÔØWTK£¬sun_java_wireless_toolkit-2_5_2-windows.exe£¬ÎÒÓõÄÕâ¸ö¡£
È»ºó¾ÍÊÇÏÈ°²×°jdkÔÙ°²×°wtk£¬Îҵݲװ·¾¶ÊÇ£ºD:\Java\jdk1.6.0_10ºÍD:\Java\WTK2.5.2
ÏÂÔØeclipse£¬ÎÒÓõÄÊÇ£ºwtp-all-in-one-sdk-R-1.5.5- ......
ÍøÉÏÓÐÁ˺ܶàÕâÑùµÄÎÄÕ£¬²»¹ý¸Ð¾õ²¢²»ÊǺÜÈ«£¬ÎÒ¶¼¶«ÕÒÎ÷ÕÒ²Å×îÖÕ°ÑJAVAµ÷ÓÃc++¸ø¸ã¶¨¡£
ÏÂÃæÒýÓÃÁ˺ܶàÍøÉÏÒѾÓеĵÄÄÚÈÝ£¬ÎÒÔÚÕâÀïÖ»ÊÇÕûºÏÒ»´Î£¬ÈÃÒÔºóµÄÅóÓÑÉÙ×ßÍø·¡£
¶à˵ÎÞÒ棬¾ßÌåÈçÏ£º
×÷Õß:mykoma
¼ÙÉèËùÓÐÎļþ¶¼·ÅÔÚd:\
Step 1:
дJAVAÎļþ
ÀýÈç³ÌÐòtestdll.java£¬ÄÚÈÝΪ£º
......