您的位置:首页 > 娱乐 > 明星 > 设计模式--单例模式

设计模式--单例模式

2024/10/5 18:32:39 来源:https://blog.csdn.net/qq_29751083/article/details/141230714  浏览:    关键词:设计模式--单例模式

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

  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;}
}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com