设计模式--单例模式

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

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

    private Singleton() {}

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

    private Singleton() {}

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

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

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值