javaµ¥ÀýÀà¼Ì³ÐÎÊÌâ ʹÓõǼÇʽµ¥ÀýµÄ·¢ÏÖ
ÓÉÓÚ³£¼ûµÄµ¥Àýģʽ,¶öººÊ½µ¥ÀýºÍÀÁººÊ½µ¥Àý,¹¹Ôì·½·¨¶¼Ë½ÓÐÁË,Òò´Ë²»Äܱ»¼Ì³Ð.
×î½üÔÚ¿ª·¢ÖÐÓöµ½ÎÊÌâÁË,Ç¡ºÃÐèÒª¼Ì³Ðµ¥ÀýÀà.ÕÒ×ÊÁϲÅÕÒµ½ÁËÕâ¸ö½â¾ö°ì·¨---µÇ¼Çʽµ¥Àý.
µÇ¼Çʽµ¥ÀýÀàÊÇΪÁË¿Ë·þ¶öººÊ½µ¥ÀýÀ༰ÀÁººÊ½µ¥ÀýÀà¾ù²»¿É¼Ì³ÐµÄȱµã¶øÉè¼ÆµÄ¡£µ±µ¥ÀýÀàÐèÒª±»¼Ì³ÐʱÊÊÓÃ.
´úÂëÇåµ¥3£ºµÇ¼Çʽµ¥ÀýÀà
import java.util.HashMap;
public class RegSingleton
{
static private HashMap m_registry = new HashMap();
private List<Player> plsyers=new linkedList<Player>();
static
{
RegSingleton x = new RegSingleton();
m_registry.put( x.getClass().getName() £¬ x);
}
/**
* ±£»¤µÄĬÈϹ¹Ôì×Ó
*/
protected RegSingleton() {}
/**
* ¾²Ì¬¹¤³§·½·¨£¬·µ»¹´ËÀàΩһµÄʵÀý
*/
static public RegSingleton getInstance(String name)
{
if (name == null)
{
name = "com.javapatterns.singleton.demos.RegSingleton";
}
if (m_registry.get(name) == null)
{
try
{
m_registry.put( name£¬
Class.forName(name).newInstance() ) ;
}
catch(Exception e)
{
System.out.println("Error happened.");
}
}
return (RegSingleton) (m_registry.get(name) );
}
public void remove(Player player){
players.remove(Player);
}
public List<Player> getPlayers(){
return players;
}
}
ËüµÄ×ÓÀàRegSingletonChild ÐèÒª¸¸ÀàµÄ°ïÖú²ÅÄÜʵÀý»¯¡£ÏÂͼËùʾÊǵǼÇʽµ¥ÀýÀà×ÓÀàµÄÒ»¸öÀý×Ó¡£Í¼ÖеĹØϵ±íÃ÷£¬´ËÀàÊÇÓɸ¸Àཫ×ÓÀàʵÀý»¯µÄ¡£
ÏÂÃæÊÇ×ÓÀàµÄÔ´´úÂë¡£
´úÂëÇåµ¥4£ºµÇ¼Çʽµ¥ÀýÀàµÄ×ÓÀà
import java.util.HashMap;
public class RegSingletonChild extends RegSingleton
{
public RegSingletonChild() {}
/**
* ¾²Ì¬¹¤³§·½·¨
*/
static public RegSingletonChild getInstance()
{
return (RegSingletonChild)
RegSingleton.getInstance(
"com.javapatterns.singleton.demos.RegSingletonChild" );
}
public void test(){
int a= getPlayers().size();
int b =RegSingleton .getInstance().getPlayers().size();
}
}
ÔÚGoF ÔʼµÄÀý×ÓÖУ¬²¢Ã»ÓÐgetInstance() ·½·¨£¬ÕâÑùµÃµ½×ÓÀà±ØÐëµ÷ÓõÄgetInstance(String name)·½·¨
Ïà¹ØÎĵµ£º
ͨ¹ýjava.awt.RobotµÄcreateScreenCapture½ØÆÁ¡£
public static void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
& ......
JavaÐéÄâ»úµÄÄÚ²¿Ìåϵ½á¹¹Ò²ÐíºÜÉÙÓÐÈËÈ¥¹ØÐÄ£¬ÒòΪ¶ÔÓÚJava³ÌÐòÔ±À´Ëµ£¬Ò»°ãÖ»ÐèÒª¸úAPI´ò½»µÀ¾Í¿ÉÒÔÁË¡£ÕâЩÌåϵ½á¹¹Ö»ÊÇJavaÐéÄâ»úÄÚ²¿µÄ½á¹¹¶øÒÑ¡£µ«ÊÇÈç¹ûÀí½âÁËÆäÄÚ²¿½á¹¹£¬¶ÔÓÚÎÒÃÇ¿ª·¢ÏîÄ¿£¬ÒÔ¼°ÅųýϵͳÖеÄÏà¹Ø¹ÊÕÏÊÇÓÉ°ïÖúµÄ¡£
ÏÈ¿´¿´×ÜÌå½á¹¹Í¼£¬ÈçÏ£º
¸÷²¿·ÖµÄÖ÷Òª¹¦ÄÜ·Ö±ðÊÇ£º
1. Àà×°ÔØ×Óϵͳ:¸ºÔðJavaÀ ......
JAVA_HOME:
C:\Java\jdk1.6.0_17
Path:
%JAVA_HOME%\bin
ClassPath:
.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar
ÎÒÃÇÐèÒªÉèÖÃÈý¸ö»·¾³±äÁ¿£ºJAVA_HOME¡¢PATH ºÍ CLASSPATH¡£
JAVA_HOME£º¸Ã»·¾³±äÁ¿µÄÖµ¾ÍÊÇ Java ËùÔÚµÄĿ¼£¬Ò»Ð© Java °æµÄÈí¼þºÍÒ»
Щ Java µÄ¹¤¾ßÐèÒªÓõ½¸Ã±äÁ¿£¬ÉèÖà PATH ºÍ CLASSP ......
Java ±à³Ì¼¼ÊõÖкº×ÖÎÊÌâµÄ·ÖÎö¼°½â¾ö
ÏÖÔÚ Java ±à³ÌÓïÑÔÒѾ¹ã·ºÓ¦ÓÃÓÚ»¥ÁªÍøÊÀ½ç£¬ÔçÔÚ Sun ¹«Ë¾¿ª·¢ Java ÓïÑÔµÄʱºò£¬¾ÍÒѾ¿¼Âǵ½¶Ô·ÇÓ¢ÎÄ×Ö·ûµÄÖ§³ÖÁË¡£Sun ¹«Ë¾¹«²¼µÄ Java ÔËÐл·¾³£¨JRE£©±¾Éí¾Í·ÖÓ¢ÎÄ°æºÍ¹ú¼Ê°æ£¬µ«Ö»Óйú¼Ê°æ²ÅÖ§³Ö·ÇÓ¢ÎÄ×Ö·û¡£²»¹ýÔÚ Java ±à³ÌÓïÑÔµÄÓ¦ÓÃÖУ¬¶ÔÖÐÎÄ×Ö·ûµÄÖ§³Ö²¢·ÇÈçͬ Java S ......