JAVAÖеÄSingletonģʽ
µ¥Àýģʽ£¬¹ËÃû˼Ò壬ֻÄÜÓÐÒ»¸öʵÀý¡£
Ò».´Ó¶àḬ̈߳²È«ËµÆð£¬ÈçÏÂͼ´úÂ룬´ËÎÊÌâ¿ÉÒÔÓÃsynchronized¹Ø¼ü×ÖÀ´½â¾ö¡£¸Ã·½·¨È±µã£ºÃ¿Ò»¸öÏß³ÌÔÚ»ñȡʵÀý¶ÔÏó֮ǰ¶¼ÒªÔÚsynchronizedÉÏͬ²½µÄ¶ÔÏóÉϽøÐеȴý£¬Òò´ËЧÂʲ»¸ß¡£
¶þ.Double Check·½·¨£¬¼ûÏÂͼ´úÂë¡£Double CheckµÄ³õÖÔÊÇÖ»Óе±instanceΪNULLʱִÐеÄÏ̲߳ÅÐèÒªÔÚsynchronizedͬ²½µÄ¶ÔÏóÉϵȴý£¬Õâ±ÜÃâÁË“Ò»”·½·¨ÖеÄÿ¸öÏß³ÌÔÚ»ñµÃʵÀý¶ÔÏó֮ǰ¾ùÒªÔÚsynchronizedͬ²½µÄ¶ÔÏóÉϽøÐеȴýµÄ±×¶Ë¡£µ«ÊÇÕâÒ»ÉèÏëÔÚJAVAÖÐÈ´²»ÊÊÓ¦¡£ÔÒò£ºJAVAÄÚ´æÄ£ÐͲ¢²»±£Ö¤Ò»¸ö¶ÔÏóµÄÒýÓÃÖ»ÓÐÔÚËü¹¹ÔìÍêÖ®ºó²Å±»¸³ÓèÒ»¸öʵÀý¡£ÀýÈ磺AÏß³ÌÖ´Ðе½ÁËinstance=new SingletonClass();"instance=new SingletonClass()"ÕâÒ»¹ý³ÌµÄÖ´Ðв½Öè¿ÉÄÜÊÇÏÂÊöÇéÐΣºµÚÒ»²½£¬JVM·ÖÅäÄڴ棻µÚ¶þ²¿£¬instanceÖ¸Ïò·ÖÅäµÄÄڴ棻µÚÈý²½£¬µ÷Óù¹Ôì·½·¨¡£¼ÙÉè´ËʱAÏß³ÌÖ´Ðе½Á˵ڶþ²½£¬¶øͬʱBÏ߳̿ªÊ¼Ö´ÐУ¬´ËʱBÏ̼߳ì²âµ½µÄinstance²»Îªnull£¬µ«ÒòΪAÏß³ÌÖл¹Î´Ö´ÐеÚÈý²½¼´»¹Ã»µ÷Óù¹Ô캯Êý£¬ËùÒÔBÏß³ÌÖзµ»ØµÄinstanceÊDz»ÕýÈ·µÄ¡£µ±È»£¬ÕâÖÖ´íÎóʱ¿ÉÄÜ·¢ÉúµÄ£¬¾¡¹ÜÏÂÃæ´úÂëÖеÄÖ´Ðеõ½µÄ½á¹ûÊÇÕýÈ·µÄ¡£
Èý.ʹÓþ²Ì¬ÄÚ²¿Àà¡£ºÃ´¦£º1.¿ÉÒÔʵÏÖÑÓʱʵÀý»¯¡£¼´Ö»Óе±Ààµ÷ÓÃgetInstance()·½·¨Ê±²Å»á¶Ôinstance½øÐÐʵÀý»¯£¬¶ø“Ò»”“¶þ”Á½ÖÖ·½·¨Öж¼ÊÇÔÚÀàµÚÒ»´Î¼ÓÔØʱ±ã¶Ôinstance½øÐÐÁËʵÀý»¯¡£2.½â¾öÁË“Ò»”·½·¨ÖУ¬Ã¿¸öÏß³ÌÔÚ»ñµÃʵÀý¶ÔÏó֮ǰ¶¼ÒªÔÚsynchronizedͬ²½µÄ¶ÔÏóÉϽøÐеȴýµÄȱÏÝ¡£
ÎÊÌ⣺ÕâÊÇÍøÂçÉÏ¿´µ½µÄ´ò¼ÜÆÕ±éÈÏͬµÄÒ»ÖÖ½â¾öjavaÖÐSingletonģʽµÄ·½·¨£¬µ«ÎªÊ²Ã´ÏÂÃæÕâÖÖ·½·¨²»´æÔÚDouble Check·½·¨Öеı׶ËÄØ£¿¼´ÏÂÃæ·½·¨ÖÐÊÇͨ¹ýʲô½â¾öÁËDouble CheckÖеÄÎÊÌâÄØ£¿
Ïà¹ØÎĵµ£º
1ÏÂÃæÄĸöÊÇÕýÈ·µÄÀàÉùÃ÷£¿¼ÙÉèÿһ¶ÎÎı¾¶¼×öΪһ¸öÃû³ÆΪFred.javaµÄÎļþµÄÈ«²¿ÄÚÈÝ£¿ a
a).
public class Fred{
public int x = 0;
public Fred (int x){
this.x=x;
}
}
b).
public class fred{
public int x = 0;
public Fred (int x){
this.x=x;
}
}
c).
public class ......
ÔÚJavaÖÐÈç¹ûList ½áºÏ´æÈ¡µÄÊÇÐí¶àÀàÐͲ»Í¬µÄÊý¾ÝÈçint,String ÀàÐͶ¼ÓУ¬ÄÇôÈçºÎʹÓÃJSTL±í´ïʽѻ·»ñÈ¡List¼¯ºÏÖеÄÔªËØÄØ£¿
È磺¼ÙÈçList list=new ArrayList(); int a=1; String b="sss"; String c="ddddddd";……ÔÚServletÖРʹÓÃSession ±£´æ
session.set ......
¡¡¡¡ÎªÑ°Çójava´úÂëµÄÐÔÄÜÓÅ»¯£¬´ÓÍøÉÏËѵ½ÀûÓÃfinal¹Ø¼ü×Ö½øÐбàÒëʱinlineÓÅ»¯µÄ·½·¨£¬µ«ÊÇÕæµÄÓÐЧÂð£¿Êµ¼Ê²âÊÔÖз¢ÏÖδ±Ø£¬ÉõÖÁÐÔÄÜÓ°Ïì¾Þ´ó£¬×îÖÕ·ÅÆúÁËʹÓÃfinalÓÅ»¯µÄÏë·¨¡£
¡¡¡¡²âÊÔ»·¾³£ºWindows XP SP2£¬JDK 1.6.0_15-b03£¬Eclipse 3.5 SR1¡£
package test;
public class Test {
public static void main(St ......
finalÀࣺΪ·ÀÖ¹ËûÈË´ÓÄãµÄÀàÉÏÅÉÉúÐÂÀ࣬´ËÀàÊDz»¿ÉÀ©Õ¹µÄ¡£
¡¡¡¡¶¯Ì¬µ÷ÓñȾ²Ì¬µ÷Óû¨·ÑµÄʱ¼äÒª³¤¡£
¡¡¡¡³éÏóÀࣺ¹æ¶¨Ò»¸ö»ò¶à¸ö³éÏó·½·¨µÄÀà±¾Éí±ØÐ붨ÒåΪabstract¡£
¡¡¡¡Àý£º public abstract string getDescripition
¡¡¡¡JavaÖеÄÿһ¸öÀ඼ÊÇ´ÓObjectÀàÀ©Õ¹¶øÀ´µÄ¡£
¡¡¡¡objectÀàÖеÄequalºÍtoString·½· ......
JAVA Ìåϵ¼°Æä±à³ÌµÄ¹ý³Ì
°¥£¬±ÏÒµÁËÒ²Ò»¶Îʱ¼äÁË£¬Ö»ÖªµÀÌìÌì³´úÂ룬 JAVA µÄ»ù´¡ÖªÊ¶»ù±¾ÊÇÍüµôÁË£¬¼ÓÉÏÒÔǰһֱûÓÐϵͳµØѧϰ¹ý£¬¸Ð¾õ¿ìÒª·ÏÁË¡£»¹ÊǺúþ²ÏÂÐÄÀ´Ñ§Ñ§»ù´¡°É¡£²»ÄÜÌìÌ쿼³´úÂë¹ýÈÕ×Ó°¡¡£¡£¡£¡££¨ÏÂÎÄ´ó¶àÀ´×Կα¾ÒÔ¼°Ò»Ð©¼¼ÊõÊÓƵÉϵÄÄÚÈÝ£¬»ù±¾Àí½âÁËһЩÁË£¬Ê£ÏµÄÓÉÒÔºóµÄѧϰÂýÂýÔ ......