1、懒汉
public class Singleton {
private volatile static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (null == instance) {
synchronized(Singleton.class) {
if (null == instance) {
instance = new Singleton();
}
}
}
return instance;
}
}
2、静态内部类
public class Singleton {
private Singleton() {
}
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
3、枚举
public enum SingletonEnum {
INSTANCE;
private SingletonEnum() {
System.out.println("@@@@@@@@构造方法");
}
public void say(Integer i) {
System.out.println("######" + i);
}
}