设计模式--单例模式

单例模式是一种常用的软件设计模式,它确保一个类只有一个实例,并提供一个全局访问点。这种模式经常用于系统中只需要一个实例的对象,例如日志管理器、数据库连接池、线程池等。

下面是几种常见的单例模式实现方式:

  1. 懒汉式(线程不安全)
    这是最简单的实现方式,但是它不是线程安全的。如果多个线程同时访问 getInstance 方法,可能会创建多个实例。
java 复制代码
public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
  1. 懒汉式(线程安全)
    为了保证线程安全,可以在 getInstance 方法上加 synchronized 关键字,但这样会导致性能下降。
java 复制代码
public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
  1. 双重检查锁定(DCL)
    双重检查锁定是懒汉式的优化版本,它既保证了线程安全又提高了效率。
java 复制代码
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;
    }
}
  1. 饿汉式
    这种方式在类加载时就完成了初始化,避免了线程同步问题,但可能会导致在类加载时初始化实例造成资源浪费。
java 复制代码
public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}
  1. 静态内部类
    这种方式利用了 Java 类加载机制保证初始化实例时只有一个线程,既实现了线程安全,又实现了延迟加载。
java 复制代码
public class Singleton {
    private Singleton() {}

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

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

每种实现方式都有其优缺点,在选择时可以根据实际需求进行考虑。例如,如果你的应用程序是多线程环境并且希望实例在第一次使用时创建,那么可以采用双重检查锁定的方式;如果你的应用程序是单线程环境,那么简单的懒汉式实现就足够了。

相关推荐
Zane19942 天前
一个 new 就能创建对象,为什么还要拆出单例、工厂、建造者、原型四种模式?
设计模式
怕浪猫3 天前
一行命令复刻爆款视频,我把 Hypit 从安装跑到了出片
人工智能·设计模式·程序员
Zane19943 天前
函数式编程里的函数,其实不是你天天写的那个函数——三大编程范式的边界在哪
设计模式
Zane19944 天前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..4 天前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1505 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)5 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki6 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅666 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa6 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio