Java 中的單例類(Singleton)應(yīng)用場(chǎng)景分析
Java 中的單例類(Singleton)
單例類是一種設(shè)計(jì)模式,確保一個(gè)類只有一個(gè)實(shí)例,并提供一個(gè)全局訪問點(diǎn)。
單例模式的核心特點(diǎn)
- 唯一實(shí)例:類只能創(chuàng)建一個(gè)對(duì)象實(shí)例
- 全局訪問:提供全局訪問點(diǎn)獲取該實(shí)例
- 自行實(shí)例化:類自己負(fù)責(zé)創(chuàng)建自己的實(shí)例
- 構(gòu)造器私有:防止外部通過 new 創(chuàng)建實(shí)例
單例模式的實(shí)現(xiàn)方式
1. 餓漢式(Eager Initialization)
public class EagerSingleton {
// 類加載時(shí)就創(chuàng)建實(shí)例
private static final EagerSingleton instance = new EagerSingleton();
// 私有構(gòu)造器
private EagerSingleton() {
// 防止反射攻擊
if (instance != null) {
throw new RuntimeException("單例模式禁止反射創(chuàng)建實(shí)例");
}
}
// 全局訪問點(diǎn)
public static EagerSingleton getInstance() {
return instance;
}
public void showMessage() {
System.out.println("餓漢式單例");
}
}優(yōu)點(diǎn):簡(jiǎn)單、線程安全
缺點(diǎn):如果實(shí)例未被使用,會(huì)造成內(nèi)存浪費(fèi)
2. 懶漢式(Lazy Initialization)
public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {}
// 線程不安全版本
public static LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}3. 線程安全的懶漢式
public class ThreadSafeSingleton {
private static volatile ThreadSafeSingleton instance;
private ThreadSafeSingleton() {}
// 方法同步(性能較差)
public static synchronized ThreadSafeSingleton getInstance() {
if (instance == null) {
instance = new ThreadSafeSingleton();
}
return instance;
}
}4. 雙重檢查鎖(Double-Checked Locking)
public class DoubleCheckedSingleton {
// 使用 volatile 保證可見性和禁止指令重排序
private static volatile DoubleCheckedSingleton instance;
private DoubleCheckedSingleton() {}
public static DoubleCheckedSingleton getInstance() {
if (instance == null) { // 第一次檢查
synchronized (DoubleCheckedSingleton.class) {
if (instance == null) { // 第二次檢查
instance = new DoubleCheckedSingleton();
}
}
}
return instance;
}
}5. 靜態(tài)內(nèi)部類(推薦使用)
public class InnerClassSingleton {
private InnerClassSingleton() {
// 防止反射攻擊
if (SingletonHolder.INSTANCE != null) {
throw new RuntimeException("單例模式禁止反射創(chuàng)建實(shí)例");
}
}
// 靜態(tài)內(nèi)部類在第一次被引用時(shí)才會(huì)加載
private static class SingletonHolder {
private static final InnerClassSingleton INSTANCE = new InnerClassSingleton();
}
public static InnerClassSingleton getInstance() {
return SingletonHolder.INSTANCE;
}
public void showMessage() {
System.out.println("靜態(tài)內(nèi)部類單例");
}
}優(yōu)點(diǎn):懶加載、線程安全、性能好
6. 枚舉單例(最安全的方式)
public enum EnumSingleton {
INSTANCE;
public void showMessage() {
System.out.println("枚舉單例");
}
// 可以添加其他方法
public void doSomething() {
System.out.println("執(zhí)行某些操作");
}
}
// 使用方式
EnumSingleton.INSTANCE.showMessage();優(yōu)點(diǎn):
- 絕對(duì)防止多次實(shí)例化
- 自動(dòng)支持序列化機(jī)制
- 防止反射攻擊
單例模式的應(yīng)用場(chǎng)景
// 1. 配置管理器
public class ConfigurationManager {
private static class Holder {
static final ConfigurationManager INSTANCE = new ConfigurationManager();
}
private Properties config;
private ConfigurationManager() {
// 加載配置文件
config = new Properties();
try {
config.load(getClass().getResourceAsStream("/config.properties"));
} catch (IOException e) {
throw new RuntimeException("加載配置文件失敗", e);
}
}
public static ConfigurationManager getInstance() {
return Holder.INSTANCE;
}
public String getProperty(String key) {
return config.getProperty(key);
}
}
// 2. 數(shù)據(jù)庫(kù)連接池
public class DatabaseConnectionPool {
private static final DatabaseConnectionPool instance = new DatabaseConnectionPool();
private List<Connection> connections;
private DatabaseConnectionPool() {
// 初始化連接池
connections = new ArrayList<>();
// ... 創(chuàng)建數(shù)據(jù)庫(kù)連接
}
public static DatabaseConnectionPool getInstance() {
return instance;
}
public Connection getConnection() {
// 從連接池獲取連接
return connections.isEmpty() ? null : connections.remove(0);
}
public void releaseConnection(Connection conn) {
connections.add(conn);
}
}
// 3. 日志記錄器
public class Logger {
private static volatile Logger instance;
private Logger() {
// 初始化日志系統(tǒng)
}
public static Logger getInstance() {
if (instance == null) {
synchronized (Logger.class) {
if (instance == null) {
instance = new Logger();
}
}
}
return instance;
}
public void log(String message) {
System.out.println("[LOG] " + new Date() + ": " + message);
}
}單例模式的注意事項(xiàng)
1. 序列化問題
public class SerializableSingleton implements Serializable {
private static final long serialVersionUID = 1L;
private static SerializableSingleton instance = new SerializableSingleton();
private SerializableSingleton() {}
public static SerializableSingleton getInstance() {
return instance;
}
// 防止反序列化創(chuàng)建新實(shí)例
protected Object readResolve() {
return getInstance();
}
}2. 反射攻擊防護(hù)
public class ReflectionSafeSingleton {
private static ReflectionSafeSingleton instance;
private static boolean initialized = false;
private ReflectionSafeSingleton() {
synchronized (ReflectionSafeSingleton.class) {
if (initialized) {
throw new RuntimeException("單例模式禁止反射創(chuàng)建實(shí)例");
}
initialized = true;
}
}
public static ReflectionSafeSingleton getInstance() {
if (instance == null) {
synchronized (ReflectionSafeSingleton.class) {
if (instance == null) {
instance = new ReflectionSafeSingleton();
}
}
}
return instance;
}
}3. 克隆防護(hù)
public class CloneSafeSingleton implements Cloneable {
private static final CloneSafeSingleton instance = new CloneSafeSingleton();
private CloneSafeSingleton() {}
public static CloneSafeSingleton getInstance() {
return instance;
}
// 防止克隆
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("單例模式禁止克隆");
}
}總結(jié)
| 實(shí)現(xiàn)方式 | 線程安全 | 懶加載 | 性能 | 推薦度 |
|---|---|---|---|---|
| 餓漢式 | ? | ? | 好 | ★★★ |
| 懶漢式(同步) | ? | ? | 差 | ★★ |
| 雙重檢查鎖 | ? | ? | 好 | ★★★★ |
| 靜態(tài)內(nèi)部類 | ? | ? | 好 | ★★★★★ |
| 枚舉 | ? | ? | 好 | ★★★★★ |
最佳實(shí)踐建議:
- 如果需要懶加載:使用靜態(tài)內(nèi)部類方式
- 如果不需要懶加載:使用枚舉方式(最安全)
- 避免使用簡(jiǎn)單的懶漢式(線程不安全)
- 考慮序列化、反射、克隆等安全問題
到此這篇關(guān)于Java 中的單例類(Singleton)應(yīng)用場(chǎng)景分析的文章就介紹到這了,更多相關(guān)java單例類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)
下面小編就為大家?guī)硪黄狫AVA 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
java文件復(fù)制代碼片斷(java實(shí)現(xiàn)文件拷貝)
本文介紹java實(shí)現(xiàn)文件拷貝的代碼片斷,大家可以直接放到程序里運(yùn)行2014-01-01
Redis框架Jedis及Redisson對(duì)比解析
這篇文章主要介紹了Redis框架Jedis及Redisson對(duì)比解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn)
這篇文章主要為大家介紹了Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

