Java 单例模式

简介

单例模式可以节省内存和计算,保证结果的正确.

方便管理

适用场景

  • 无状态的工具类
    • 如日志工具类
  • 全局信息类
    • 如统计网站的访问次数

优缺点:

优点:

  • 在内存中只有一个对象,节省内存空间;
  • 避免频繁的创建销毁对象,可以提高性能;
  • 避免对共享资源的多重占用,简化访问;
  • 为整个系统提供一个全局访问点。

缺点:

  • 不适用于变化频繁的对象;
  • 滥用单例将带来一些负面问题,如为了节省资源将数据库连接池对象设计为的单例类,可能会导致共享连接池对象的程序过多而出现连接池溢出;
  • 如果实例化的对象长时间不被利用,系统会认为该对象是垃圾而被回收,这可能会导致对象状态的丢失;

单例模式的多种写法

饿汉式(静态常量)

/**
 * 好处:在类加载的时候便完成了实例化.
 * */
public class singleton1 {

    private static final singleton1 SINGLETON_1 = new singleton1();

    private singleton1() {
    }

    public static singleton1 getInstance() {
        return SINGLETON_1;
    }
}

饿汉式(静态代码块)

/**
 * 好处:1.在类加载的时候便完成了实例化.
 * 2.在初始化的时候可以进行一些属性的初始化.
 * */
public class singleton2 {

    private static final singleton2 SINGLETON_2;

    int anInt;

    static {
        // 可以从配置文件中去读取一些值
        SINGLETON_2 = new singleton2(1);
    }

    private singleton2(int anInt) {
        this.anInt = anInt;
    }

    public static singleton2 getInstance() {
        return SINGLETON_2;
    }
}

枚举

public enum Singleton3 {
    INSTANCE_3;

    public void whatever() {
        
    }
}

懒汉式(线程不安全)

/**
 * 好处:1.在需要的时候才回被创建,可以节约内存.
 * */
public class singleton4 {

    private static singleton3 SINGLETON;

    private singleton4() {}

    /**
     * 线程不安全,不推荐使用
     * */
    public static singleton4 getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new singleton3();
        }
        return SINGLETON;
    }
}

懒汉式(线程安全)

/**
 * 好处:1.线程安全
 * 2.延迟加载
 * 3.效率较高
 * */
public class singleton5 {

    private volatile static singleton4 SINGLETON;

    private singleton5() {}

    public static singleton5 getInstance() {
        if (SINGLETON == null) { 
            synchronized (singleton5.class) {
                if (SINGLETON == null) {
                    SINGLETON = new singleton4();
                }
            }
        }
        return SINGLETON;
    }
}

静态内部类

public class Singleton6 {

    private Singleton6() {
    }

    private static class SingletonInstance {
        private static final Singleton6 SINGLETON_6 = new Singleton6();
    }

    public static Singleton6 getInstance() {
        return SingletonInstance.SINGLETON_6;
    }
}

单例模式的最佳实践

单例模式最好是使用枚举类型去创建,这样的好处就是既可以实现单例也可以面对序列化和反射操作时不受干扰.