单例模式适用于重新构造消耗大,又时常需要用到的类,一般又懒汉模式、饿汉模式、DCL模式和静态内部类模式这几种
懒汉模式
public class Example {
private static Example example;
private Example(){
}
public static synchronized Example getInstance() {
if (example == null) {
example = new Example();
}
return example;
}
}
懒汉模式在每次获得单例的时候都使用synchronized(加内部锁方式的同步)关键字进行同步,消耗较大
饿汉模式
public class Example {
private static final Example example = new Example();
private Example(){
}
public static Example getInstance() {
return example;
}
}
饿汉模式没有同步的问题,每次获取到的都是同一单例,但不管有没有用到都会创建对象,浪费空间
DCL模式
public class Example {
private static volatile Example example = null;
private Example(){
}
public static Example getInstance() {
if (example == null) {
synchronized (Example.class) {
if (example == null) {
example = new Example();
}
}
}
return example;
}
}
DCL方法有上面两种单例方法的优点,既保证了使用时才进行初始化,又保证了线程安全,但在某些情况下会有小概率出错,尽管如此,这仍然是用得最广泛的单例方法
静态内部类模式
public class Example {
private Example(){
}
public static Example getInstance() {
return ExampleHolder.example;
}
private static class ExampleHolder {
private static final Example example = new Example();
}
}
既保证了线程安全,也达到了节省资源的目的,是推荐使用的单例模式
枚举模式
public enum Example {
INSTANCE;
public void doSomething(){
...
}
}