单例模式(Singleton Pattern)

1 什么是单例模式?

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

2 几种单例模式的实现

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

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

2.1 饿汉单例(线程安全)

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

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 语句,造成实例化多个实例;

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 懒汉单例(锁方法)

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

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 懒汉单例(双重检查)

线程安全

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"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拜见老天師

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值