找回密码
 立即注册
首页 业界区 业界 学习笔记-设计模式-创建型模式-单例模式 ...

学习笔记-设计模式-创建型模式-单例模式

欤夤 2025-6-9 06:25:41
单例模式

一个类只有一个实例,并提供一个全局访问此实例的点,哪怕多线程同时访问。
单例模式主要解决了一个全局使用的类被频繁的创建和消费的问题。
单例模式的案例场景


  • 数据库的连接池不会反复创建
  • spring中一个单例模式bean的生成和使用
  • 在我们平常的代码中需要设置全局的一些属性保存
七种单例模式的实现

1.懒汉模式(线程不安全,懒加载)
  1. public static class Singleton_01 {
  2.     private static Singleton_01 instance;
  3.    
  4.     private Singleton_01() {}
  5.    
  6.     public static Singleton_01 getInstance() {
  7.         if (null != instance) {
  8.             return instance;
  9.         }
  10.         instance = new Singleton_01();
  11.         return instance;
  12.     }
  13. }
复制代码
非常标准且简单地单例模式。
满足了懒加载,但是线程不安全。
2.懒汉模式(线程安全,懒加载)
  1. public static class Singleton_02 {
  2.     private static Singleton_02 instance;
  3.    
  4.     private Singleton_02() {}
  5.    
  6.     public static synchronized Singleton_02 getInstance() {
  7.         if (null != instance) {
  8.             return instance;
  9.         }
  10.         instance = new Singleton_02();
  11.         return instance;
  12.     }
  13. }
复制代码
线程安全,但是效率低,不建议使用。
3.饿汉模式(线程安全)
  1. public class Singleton_03 {
  2.     private static Singleton_03 instance = new Singleton_03();
  3.    
  4.     private Singleton_03() { }
  5.    
  6.     public static Singleton_03 getInstance() {
  7.         return instance;
  8.     }   
  9. }
复制代码
饿汉模式,在程序启动的时候直接运行加载。
4.使用内部静态类(线程安全、懒加载)
  1. public class Singleton_04 {
  2.     private static class SingletonHolder {
  3.         private static Singleton_04 instance = new Singleton_04();
  4.     }
  5.    
  6.     private Singleton_04() { }
  7.    
  8.     public static Singleton_04 getInstance() {
  9.         return SingletonHolder.instance;
  10.     }
  11. }
复制代码
通过内部静态类实现了懒加载和线程安全,推荐使用。
5.锁+双重校验(线程安全,懒加载)
  1. private class Singleton_05 {
  2.     private static volatile Singleton_05 instance;
  3.    
  4.     private Singleton_05() { }
  5.    
  6.     public static Singleton_05 getInstance() {
  7.         if (null != instance) {
  8.             return instance;
  9.         }
  10.         synchronized (Singleton_05.class) {
  11.             if (null == instance) {
  12.                 instance = new Singleton_05();
  13.             }
  14.         }
  15.         return instance;
  16.     }
  17. }
复制代码
相较于线程安全的懒汉模式,这种方法效率更高。
6.CAS【AtomicReference】(懒加载,线程安全)
  1. public class Singleton_06 {
  2.     private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<Singleton_06>();
  3.    
  4.     private Singleton_06() {  }
  5.    
  6.     public static final Singleton_06 getInstance() {
  7.         for(;;) {
  8.             Singleton_06 instance = INSTANCE.get();
  9.             if (null != instance) {
  10.                 return instance;
  11.             }
  12.             INSTANCE.compareAndSet(null, new Singleton_06());
  13.             return INSTANCE.get();
  14.         }
  15.     }
  16. }
复制代码
AtomicReference可以封装引用一个V实例,支持并发访问。
使用CAS的好处就是不需要使用传统的加锁方式保证线程安全,而是依赖于CAS的忙等算法,依赖于底层硬件的实现,来保证线程安全。相对于其他锁的实现,没有线程的切换和阻塞,也就没有了额外的开销,并且可以迟迟较大的并发性。
但CAS的一个缺点就是忙等,如果一直没有获取到将会处于死循环中。
7.枚举单例(线程安全)
  1. public enum Singleton_07 {
  2.    
  3.     INSTANCE;
  4.    
  5.     private Map<String, String> cache = new ConcurrentHashMap<>();
  6.     public Map<String, String> getCache() {
  7.         return cache;
  8.     }
  9.     public void addToCache(String key, String value) {
  10.         cache.put(key, value);
  11.     }
  12. }
复制代码
《Effective Java》作者推荐使用枚举的方式解决单例模式,这种方法解决了线程安全、自由串行化和单一实例的问题。
调用时也很方便:Singleton_07.INSTANCE.getCache();
枚举单例十分简洁,并且它无常地提供了串行化机制,绝对防止对此实例化(毕竟是枚举)。
但是它在继承的场景下是不可使用的,并且不是懒加载(毕竟是枚举)。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册