JAvaプログラミング問題:単一のスキーマを作成する
1883 ワード
/**
* java :
*/
public class Singleton {
/*
//1. , , 。。。 new ,
private Singleton(){}
private static final Singleton Singleton = new Singleton();
public static Singleton getInstance(){
return Singleton;
}
*/
/*
//2. , 。。。 , ,
private Singleton(){}
private static Singleton Singleton = null;
public static Singleton getInstance(){
if(Singleton==null){
Singleton = new Singleton();
}
return Singleton;
}
*/
/*
//3. , , 。。。99%
private Singleton(){}
private static Singleton Singleton = null;
public static synchronized Singleton getInstance(){ // synchronized
if(Singleton==null){
Singleton = new Singleton();
}
return Singleton;
}
*/
/*
//4.
private Singleton(){}
//JDK5 , , volatile,
private static volatile Singleton Singleton = null;
public static Singleton getInstance(){
if(Singleton==null){
synchronized(Singleton.class){ // synchronized
if(Singleton==null){
Singleton = new Singleton();
}
}
}
return Singleton;
}
*/
/*
//5.
private Singleton(){}
private static class SingletonHolder {
private static final Singleton singleton = new Singleton();
}
public static final Singleton getInstance(){
return SingletonHolder.singleton;
}
*/
//6.
private static enum EnumSingleton{
INSTANCE;
private Singleton singleton;
//JVM
private EnumSingleton(){
singleton = new Singleton();
}
public Singleton getInstance(){
return singleton;
}
}
}
:
http://cantellow.iteye.com/blog/838473
http://www.cnblogs.com/yinxiaoqiexuxing/p/5605338.html
http://blog.csdn.net/zhanlanmg/article/details/49944991