Java单例模式详解

单例模式详解

一、单例模式概述

单例模式(Singleton Pattern)是一种创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点来访问这个实例。

核心特点

  • 唯一实例:保证一个类只有一个实例存在
  • 全局访问:提供统一的访问入口
  • 延迟初始化:通常采用懒加载方式创建实例
  • 线程安全:需要考虑多线程环境下的安全性

二、单例模式的实现方式

1. 懒汉式(线程不安全)

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

缺点:多线程环境下可能创建多个实例

2. 懒汉式(线程安全)

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

缺点:每次获取实例都要同步,性能较差

3. 双重检查锁定(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;
    }
}

特点

  • volatile关键字防止指令重排序
  • 第一次检查避免不必要的同步
  • 第二次检查确保只创建一个实例

4. 静态内部类实现

public class Singleton {
    private Singleton() {}
    
    private static class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }
    
    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

优点

  • 线程安全
  • 延迟加载
  • 实现简单

5. 枚举实现(推荐)

public enum Singleton {
    INSTANCE;
    
    public void doSomething() {
        // 业务方法
    }
}

优势

  • 绝对防止多次实例化
  • 自动支持序列化机制
  • 线程安全
  • 代码简洁

三、单例模式的应用场景

1. 配置管理类

public class AppConfig {
    private static AppConfig instance;
    private Properties configs;
    
    private AppConfig() {
        // 初始化加载配置
        configs = new Properties();
        try (InputStream is = getClass().getResourceAsStream("/config.properties")) {
            configs.load(is);
        } catch (IOException e) {
            throw new RuntimeException("加载配置文件失败", e);
        }
    }
    
    public static synchronized AppConfig getInstance() {
        if (instance == null) {
            instance = new AppConfig();
        }
        return instance;
    }
    
    public String getConfig(String key) {
        return configs.getProperty(key);
    }
}

使用场景

  • 全局配置信息管理
  • 避免重复加载配置文件
  • 统一配置访问入口

2. 数据库连接池

public class ConnectionPool {
    private static ConnectionPool instance;
    private List<Connection> pool;
    
    private ConnectionPool() {
        // 初始化连接池
        pool = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            pool.add(createConnection());
        }
    }
    
    public static ConnectionPool getInstance() {
        if (instance == null) {
            synchronized (ConnectionPool.class) {
                if (instance == null) {
                    instance = new ConnectionPool();
                }
            }
        }
        return instance;
    }
    
    public Connection getConnection() {
        // 从池中获取连接
    }
    
    public void releaseConnection(Connection conn) {
        // 释放连接回池
    }
}

优势

  • 避免频繁创建连接开销
  • 统一管理数据库连接
  • 控制连接数量

四、单例模式的注意事项

  1. 序列化问题

    • 实现Serializable接口时需要添加readResolve()方法

    • 防止反序列化时创建新实例

      private Object readResolve() {
      return getInstance();
      }

  2. 反射攻击防护

    • 防止通过反射调用私有构造方法

    • 可以在构造方法中添加检查

      private Singleton() {
      if (instance != null) {
      throw new IllegalStateException(“单例实例已存在”);
      }
      }

五、总结

单例模式是最常用的设计模式之一,适用于需要全局唯一访问点的场景。在选择实现方式时:

  • Java 5+环境:优先使用枚举实现
  • 需要延迟加载:考虑静态内部类方式
  • 性能敏感场景:使用双重检查锁定
  • 简单场景:可以使用同步方法实现

正确使用单例模式可以:

  • 减少系统资源消耗
  • 提供一致的访问入口
  • 简化对象管理
  • 避免状态不一致问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值