概述
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
单例模式的几种实现方式
1、懒汉式,线程不安全
这种方式是最基本的实现方式,这种实现最大的问题就是不支持多线程。因为没有加锁 synchronized,所以严格意义上它并不算单例模式。
public class Singleton {
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2、懒汉式,线程安全
这种方式能够在多线程中很好的工作,但是,效率很低,99% 情况下不需要同步。
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3、饿汉式,线程安全
这种方式比较常用,但容易产生垃圾对象。没有加锁,执行效率会提高。但是类加载时就初始化,浪费内存。
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
4、双检锁/双重校验锁,线程安全
这种方式采用双锁机制,安全且在多线程情况下能保持高性能。经过volatile修饰的singleton具有可见性,确保在多线程环境中读取到的singleton值一致,和第一个singleton判空条件相结合,减少了线程等锁的情况,提高了性能。而synchronized则确保了单例模式在多线程环境下只创建一个对象的特点。
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
5、登记式/静态内部类,线程安全
这种创建单例的模式和饿汉式比较类似,都是采用类装载时实例化的方式。不同的是,饿汉式只要 Singleton 类被装载了,那么 instance 就会被实例化。而这种方式是 Singleton 类被装载了,instance 不一定被初始化。因为 SingletonHolder 类没有被主动使用,只有通过显式调用 getInstance 方法时,才会显式装载 SingletonHolder 类,从而实例化 instance。
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
6、枚举,线程安全
这种方式能避免多线程同步问题,而且还自动支持序列化机制,防止反序列化重新创建新的对象,防止多次实例化。
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}
public static void main(String[] args) throws IOException {
FileOutputStream fosRef = null;
ObjectOutputStream oosRef = null;
try {
Singleton singleton = Singleton.INSTANCE;
System.out.println("序列化-singleton=" + singleton.hashCode());
fosRef = new FileOutputStream(new File("singleton-File.txt"));
oosRef = new ObjectOutputStream(fosRef);
oosRef.writeObject(singleton);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
oosRef.close();
fosRef.close();
}
FileInputStream fisRef = null;
ObjectInputStream iosRef = null;
try {
fisRef = new FileInputStream(new File("singleton-File.txt"));
iosRef = new ObjectInputStream(fisRef);
Singleton singleton = (Singleton) iosRef.readObject();
System.out.println("反序列化-singleton=" + singleton.hashCode());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
iosRef.close();
fisRef.close();
}
}
输出结果:
序列化-singleton=1735600054
反序列化-singleton=1735600054
通过枚举类的方式来创建单例对象,反序列化并不会产生新的实例对象,从而真正实现了创建唯一对象的目的。