C++整数转字符串自动补零:从基础到高性能的三种实现方案
2026/8/25 7:55:30
单例模式(Singleton Pattern)是Java中最简单也是最常用的设计模式之一。它保证一个类只有一个实例,并提供一个全局访问点来访问这个实例。在Java企业级项目中,单例模式广泛应用于配置管理、数据库连接池、缓存管理等场景。
public class EagerSingleton { // 在类加载时就创建实例,线程安全但可能造成资源浪费 private static final EagerSingleton instance = new EagerSingleton(); private EagerSingleton() {} public static EagerSingleton getInstance() { return instance; } }特点:
public class LazySingleton { private static LazySingleton instance; private LazySingleton() {} public static LazySingleton getInstance() { if (instance == null) { instance = new LazySingleton(); } return instance; } }**问题:**在多线程环境下,可能创建多个实例。
public class ThreadSafeLazySingleton { private static ThreadSafeLazySingleton instance; private ThreadSafeLazySingleton() {} public static synchronized ThreadSafeLazySingleton getInstance() { if (instance == null) { instance = new ThreadSafeLazySingleton(); } return instance; } }特点:
public class DCLSingleton { private static volatile DCLSingleton instance; private DCLSingleton() {} public static DCLSingleton getInstance() { if (instance == null) { synchronized (DCLSingleton.class) { if (instance == null) { instance = new DCLSingleton(); } } } return instance; } }特点:
public class InnerClassSingleton { private InnerClassSingleton() {} private static class SingletonHolder { private static final InnerClassSingleton INSTANCE = new InnerClassSingleton(); } public static InnerClassSingleton getInstance() { return SingletonHolder.INSTANCE; } }特点:
public enum EnumSingleton { INSTANCE; public void doSomething() { // 业务逻辑 } }特点:
在大型企业应用中,配置信息通常需要全局共享且只加载一次。
public class ConfigManager { private static volatile ConfigManager instance; private Properties properties; private ConfigManager() { properties = new Properties(); loadConfig(); } public static ConfigManager getInstance() { if (instance == null) { synchronized (ConfigManager.class) { if (instance == null) { instance = new ConfigManager(); } } } return instance; } private void loadConfig() { try { properties.load(ConfigManager.class.getClassLoader().getResourceAsStream("config.properties")); } catch (IOException e) { throw new RuntimeException("加载配置文件失败", e); } } public String getProperty(String key) { return properties.getProperty(key); } }数据库连接池是单例模式的典型应用,确保全局只有一个连接池实例。
public class ConnectionPool { private static volatile ConnectionPool instance; private DataSource dataSource; private ConnectionPool() { initDataSource(); } public static ConnectionPool getInstance() { if (instance == null) { synchronized (ConnectionPool.class) { if (instance == null) { instance = new ConnectionPool(); } } } return instance; } private void initDataSource() { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb"); config.setUsername("root"); config.setPassword("password"); config.setMaximumPoolSize(20); dataSource = new HikariDataSource(config); } public Connection getConnection() throws SQLException { return dataSource.getConnection(); } }在电商系统中,缓存管理器通常需要全局唯一实例。
public class CacheManager { private static volatile CacheManager instance; private Cache<String, Object> cache; private CacheManager() { cache = Caffeine.newBuilder() .maximumSize(1000) .expireAfterWrite(10, TimeUnit.MINUTES) .build(); } public static CacheManager getInstance() { if (instance == null) { synchronized (CacheManager.class) { if (instance == null) { instance = new CacheManager(); } } } return instance; } public void put(String key, Object value) { cache.put(key, value); } public Object get(String key) { return cache.getIfPresent(key); } public void remove(String key) { cache.invalidate(key); } }**问题:**在多线程环境下,单例模式可能创建多个实例。
解决方案:
**问题:**通过反射可以破坏单例模式。
解决方案:
private Singleton() { if (instance != null) { throw new RuntimeException("单例模式不允许反射创建实例"); } }**问题:**通过序列化和反序列化可以破坏单例模式。
解决方案:
protected Object readResolve() { return getInstance(); }**问题:**同步方法会影响性能。
解决方案:
单例模式是Java企业级开发中的重要设计模式,它保证一个类只有一个实例,并提供全局访问点。在实际项目中,我们需要根据具体场景选择合适的实现方式,并注意线程安全、反射攻击、序列化破坏等问题。
掌握单例模式对于Java开发者来说是非常重要的,它不仅能够帮助我们写出更好的代码,还能够在面试中展示我们的设计能力。
感谢读者观看!