单例模式(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"

相关推荐
七月丶2 小时前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞2 小时前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼2 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟19 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder19 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式