单例模式

单例模式

单例模式(Singleton Pattern)属于创建型模式,提供了一种创建对象的最佳方式。即类仅负责创建自己的对象,同时确保只有单个对象被创建,并提供了一种访问其唯一的对象的方式。因此,该类的构造函数是私有的。

实现方式

1 懒汉式-线程不安全

  • 线程不安全
  • 支持懒加载
public class Singleton {

    private static Singleton instance;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

2 懒汉式-线程安全

  • 加 synchronized 保证线程安全
  • 支持懒加载
public class Singleton {

    private static Singleton instance;

    private Singleton() {
    }

    public synchronized static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

3 饿汉式

  • 基于 classloader 机制保证线程安全
  • instance 在类加载时就完成初始化,不支持懒加载
public class Singleton {

    private static final Singleton INSTANCE;

    static {
        INSTANCE = new Singleton();
    }

    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

4 双重校验锁

  • 采用双锁机制保证线程安全
  • 支持懒加载
public class Singleton {

    private volatile static Singleton instance;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class){
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

5 静态内部类

  • 基于 classloader 机制保证线程安全
  • 支持懒加载
public class Singleton {

    private static class InnerClass {
        private static final Singleton INSTANCE = new Singleton();
    }

    private Singleton() {
    }

    public static Singleton getInstance() {
        return InnerClass.INSTANCE;
    }
}

6 枚举

  • 线程安全
  • 不支持懒加载
  • 自动支持序列化
public enum Singleton {

    INSTANCE;

    public Object data;

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

7 容器

  • 线程不安全
public class Singleton {

    private static Map<String, Object> singletonMap = new HashMap<>();

    private Singleton() {
    }

    public static void putInstance(String key, Object instance) {
        if (StringUtils.isNotBlank(key) && instance != null) {
            if (!singletonMap.containsKey(key)) {
                singletonMap.put(key, instance);
            }
        }
    }

    public static Object getInstance(String key) {
        return singletonMap.get(key);
    }
}

8 线程

  • 线程安全
  • 线程唯一单例,并非全局唯一单例
public class Singleton {

    private static final ThreadLocal<Singleton> LOCAL = ThreadLocal.withInitial(Singleton::new);

    private Singleton() {
    }

    public static Singleton getInstance() {
        return LOCAL.get();
    }
}

参考

  1. GitHub
  2. 菜鸟教程|单例模式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值