单例模式(Singleton Pattern)

1 什么是单例模式?

单例模式就是保证运行过程中某个类的实例对象只有一个,例如在 JavaWeb 程序中,service 层的对象其实主要就是提供服务,全局获取一个对象足矣,一般情况下不需要重复定义对象。但是我最初学习的时候也是,在每个 controller 中 new service 层的对象实例,这样会造成资源的浪费;

单例模式属于创造型模式,用于实例化对象。

2 几种单例模式的实现

单例模式的实现有好几种,甚至有大佬列举了 8 种,详细情况如下:
java单例模式------详解JAVA单例模式及8种实现方式_单例模式总结-CSDN博客

个人之前学习的只知道以下几种,真是学无止境啊!

2.1 饿汉单例(线程安全)

直接在类加载的时候就 new 出对象

java 复制代码
package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:23
 * @description 饿汉单例
 */
public class Singleton1 {
    private static final Singleton1 SINGLETON_INSTANCE = new Singleton1();

    private Singleton1() {}
    
    public static Singleton1 getSingletonInstance() {
        return SINGLETON_INSTANCE;
    }
}

2.2 饿汉单例(线程不安全)

当多个线程同时访问可能会进入 if 语句,造成实例化多个实例;

java 复制代码
package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:27
 * @description 懒汉单例(线程不安全1)
 */
public class Singleton2 {
    private static Singleton2 singletonInstance;
    
    private Singleton2() {}
    
    private static Singleton2 getSingletonInstance() {
        if (singletonInstance == null) {
            singletonInstance = new Singleton2();
        }
        return singletonInstance;
    }
}

2.3 懒汉单例(锁方法)

这种方式是线程安全的,但是直接锁住方法的线程较低

java 复制代码
package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:27
 * @description 懒汉单例(锁方法)
 */
public class Singleton3 {
    private static Singleton3 singletonInstance;

    private Singleton3() {}

    private static synchronized Singleton3 getSingletonInstance() {
        if (singletonInstance == null) {
            singletonInstance = new Singleton3();
        }
        return singletonInstance;
    }
}

2.4 懒汉单例(双重检查)

线程安全

java 复制代码
package se.wangs.singleton;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/6 11:33
 * @description 饿汉单例(双重检查)
 */
public class Singleton4 {
    private volatile static Singleton4 singletonInstance;

    private Singleton4() {}

    private static synchronized Singleton4 getSingletonInstance() {
        if (singletonInstance == null) {
            synchronized (Singleton4.class) {
                if (singletonInstance == null) {
                    singletonInstance = new Singleton4();
                }
            }
        }
        return singletonInstance;
    }
}

3 总结

3.1 单例模式的关键

(1)私有化构造器:使得该类只能在内部实例化,外部不能实例化;

(2)保证内部能实例化出一个对象,并且仅实例化一次;

(3)考虑线程安全,需要注意加锁;

3.2 volatile 关键字

参考大佬博客
Java并发常见面试题总结(中)

该关键字可以保证可见性,修饰的变量,在每次访问的时候,都要去内存中获取最新的值

3.3 单例模式在 Spring 中的应用

spring 中 bean 的管理默认是使用的单例模式,即 scope 属性默认是 "scope=singleton"

相关推荐
老蒋每日coding1 天前
AI Agent 设计模式系列(十八)—— 安全模式
人工智能·安全·设计模式
老蒋每日coding1 天前
AI Agent 设计模式系列(十六)—— 资源感知优化设计模式
人工智能·设计模式·langchain
老蒋每日coding1 天前
AI Agent 设计模式系列(十七)—— 推理设计模式
人工智能·设计模式
冷崖1 天前
桥模式-结构型
c++·设计模式
连山齐名1 天前
设计模式之一——堵塞队列
设计模式
会员果汁1 天前
19.设计模式-命令模式
设计模式·命令模式
茶本无香1 天前
设计模式之六—组合模式:构建树形结构的艺术
java·设计模式·组合模式
橘色的喵1 天前
嵌入式 C++ 高性能流式架构的设计
数码相机·设计模式
会员果汁1 天前
20.设计模式-职责链模式
设计模式
她和夏天一样热2 天前
【设计模式】工厂方法模式在开发中的真实应用
设计模式·工厂方法模式