单例模式(singleton):是JAVA中最简单的一种设计模式,属于创建型模式。所谓单例,就是整个程序有且仅有一个实例。
特点:
构造方法私有化
在本类中实例化一个对象作为本类的属性
对外提供一个访问本类对象的方法
饿汉式:类加载时就加载对象
应用场景:小对象,频繁用,高并发
特点:线程安全,比较常用,但容易产生垃圾,影响性能,因为一开始就初始化。
- 1 class Singleton{
- 2 //构造方法私有化
- 3 private Singleton() {
- 4 System.out.println("构造方法");
- 5 }
- 6 //对象在类加载时初始化
- 7 private static final Singleton instance = new Singleton();
- 8 //提供对外的访问方法
- 9 public static Singleton getInstance() {
- 10 return instance;
- 11 }
- 12 }
懒汉式:对象何时需要何时创建,线程不安全
应用场景:单线程,大对象
特点:线程不安全,延迟初始化。
- 1 class Singleton{
- 2 private Singleton() {
- 3 System.out.println("构造方法");
- 4 }
- 5 private static Singleton instance;
- 6 public static Singleton getInstance() {
- 7 if (instance == null) {
- 8 instance = new Singleton();
- 9 }
- 10 return instance;
- 11 }
- 12 }
同步锁机制
应用场景:多线程,大对象,稀少用。
特点:通过加锁保证了线程安全,性能会下降。
- 1 class Singleton{
- 2 private Singleton() {
- 3 System.out.println("构造方法");
- 4 }
- 5 private static Singleton instance;
- 6 //同步方法,线程安全,但性能会下降
- 7 public static synchronized Singleton getInstance() {
- 8 if (instance == null) {
- 9 instance = new Singleton();
- 10 }
- 11 return instance;
- 12 }
- 13 }
双重验证机制
应用场景:大对象,稀少用,并发量不能太大
特点:线程安全,延迟初始化。
- 1 class Singleton{
- 2 private Singleton() {
- 3 System.out.println("构造方法");
- 4 }
- 5 private static volatile Singleton instance;
- 6 //同步方法,双重验证,减少阻塞次数,提高性能
- 7 public static Singleton getInstance() {
- 8 if (instance == null) {
- 9 synchronized (Singleton.class) {
- 10 if (instance == null) {
- 11 instance = new Singleton();
- 12 }
- 13 }
- 14 }
- 15 return instance;
- 16 }
- 17 }
静态内部类
引用场景:大对象,频繁用,高并发
特点:延时对象创建,减少资源占用,提高系统性能
- 1 class Singleton{
- 2 private Singleton() {
- 3 System.out.println("构造方法");
- 4 }
- 5 static class Inner{
- 6 private static final Singleton instance = new Singleton();
- 7 }
- 8 public static Singleton getInstance() {
- 9 return Inner.instance;
- 10 }
- 11 }
枚举
- 1 enum Singleton{
- 2 //类加载时创建
- 3 INSTANCE;
- 4 }
由于单例模式是创建型模式,每次调用都会新建一个实例。那么一个重要的问题就是反序列化。当实例被写入到文件到反序列化成实例时,我们需要重写readResolve
方法,以让实例唯一。
- 1 private Object readResolve() throws ObjectStreamException{
- 2 return singleton;
- 3 }