粉粉蕉的笔记本粉粉蕉的笔记本
  • JAVA

    • 代码笔记
    • Java8实战
    • 分布式事务实战(Seata)
    • 模板引擎(FreeMarker)
    • SpringSecurity
  • PYTHON

    • 概述
    • python3
    • python3(菜鸟教程)
    • pandas
    • numpy
    • matplotlib
  • 中间件

    • Kafka
    • RocketMQ
    • Redis
    • MongoDB
    • Elastic Search
  • 数据库

    • Mysql
  • 设计模式
  • 运维

    • linux命令速查
    • windows命令速查
    • Docker笔记
    • kubernetes学习笔记
    • kubernetes实操笔记
    • 运维工具大全
    • git操作宝典
  • 大数据

    • 概览
    • Hadoop
    • Hive
  • 机器学习

    • 机器学习概览
  • 概率论
  • 线性代数
  • 统计学
  • 金融知识学习
  • 聚宽
  • 因子分析
  • RSS
  • 资源导航
  • 医保
  • 健身

    • 笔记
    • 训练计划
  • 装修攻略
  • 读书笔记

    • 《深度学习》
我也想搭建这样的博客!
🚋开往
  • JAVA

    • 代码笔记
    • Java8实战
    • 分布式事务实战(Seata)
    • 模板引擎(FreeMarker)
    • SpringSecurity
  • PYTHON

    • 概述
    • python3
    • python3(菜鸟教程)
    • pandas
    • numpy
    • matplotlib
  • 中间件

    • Kafka
    • RocketMQ
    • Redis
    • MongoDB
    • Elastic Search
  • 数据库

    • Mysql
  • 设计模式
  • 运维

    • linux命令速查
    • windows命令速查
    • Docker笔记
    • kubernetes学习笔记
    • kubernetes实操笔记
    • 运维工具大全
    • git操作宝典
  • 大数据

    • 概览
    • Hadoop
    • Hive
  • 机器学习

    • 机器学习概览
  • 概率论
  • 线性代数
  • 统计学
  • 金融知识学习
  • 聚宽
  • 因子分析
  • RSS
  • 资源导航
  • 医保
  • 健身

    • 笔记
    • 训练计划
  • 装修攻略
  • 读书笔记

    • 《深度学习》
我也想搭建这样的博客!
🚋开往
  • 设计模式

  • 创建型

    • 工厂模式
    • 单例模式
    • 建造者模式
    • 原型模式
  • 结构型

    • 适配器模式
    • 装饰模式
    • 代理模式
    • 外观模式
    • 桥接模式
    • 组合模式
  • 行为型

    • 享元模式
    • 策略模式
    • 模板方法模式
    • 观察者模式
    • 迭代子模式
    • 责任链模式
    • 命令模式
    • 备忘录模式
    • 状态模式
    • 访问者模式
    • 中介者模式
    • 解释器模式

单例模式

描述

控制一个类仅能产生一个实例。

  • 懒汉式:类加载时不会生成实例,只有在调用时才会生成实例。
  • 饿汉式:类加载时便会生成实例。

类名一般为:xxxSingleton

代码实现示例

/**
 * 最优实现:饿汉式-枚举(在Spring环境下)
 */
public enum MySingletonEnum{
    INSTANCE;
    
    private MyService myService;
    
    private MySingletonEnum(){
        myService = ApplicationContextUtil.getContext().getBean(MyService.class);
    }
    
    public String answer(){
        return myService.answer();
    }
    
    //调用:MySingletonEnum.INSTANCE.answer();
}

/**
* 推荐实现:懒汉式-静态内部类
 * 静态内部类的作用是:延迟加载&线程安全
 * 因为Singleton被加载时,静态内部类SingletonHolder不会被加载
 * 而是等到getUniqueInstance()被调用时,才会被加载。
**/
public class Singleton {

    private Singleton() {
    }

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

    public static Singleton getUniqueInstance() {
        return SingletonHolder.INSTANCE;
    }
}

/**
* 饿汉式-线程安全
**/
public class Singleton {

    private static Singleton uniqueInstance = new Singleton();

    private Singleton() {
    }

    public static Singleton getUniqueInstance() {
        return uniqueInstance;
    }
}


/**
* 懒汉式-线程安全-双重校验锁
**/
public class Singleton {

    private volatile static Singleton uniqueInstance;

    private Singleton() {
    }

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


/**
* 懒汉式-线程不安全
**/
public class Singleton {

    private static Singleton uniqueInstance;

    private Singleton() {
    }

    public static Singleton getUniqueInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new Singleton();
        }
        return uniqueInstance;
    }
}



实际应用场景

FAQ

Spring Bean 实例默认是单例的吗?

Spring Bean实例默认是单例的。

单例类如果不想注册到Spring,但又需要依赖Spring Bean,应该怎么写?

1.使用Spring的ApplicationContext:可以通过ApplicationContext获取Spring Bean实例。 2.使用Spring的依赖注入:可以使用Spring的依赖注入机制,通过构造函数或setter方法注入Spring Bean实例。

Last Updated: 2/18/25, 3:25 AM
Contributors: dongyz8
Prev
工厂模式
Next
建造者模式