Python使用單例模式創(chuàng)建類的實(shí)現(xiàn)示例
在 Python 中,實(shí)現(xiàn)單例模式有多種方式,每種方式都有其優(yōu)缺點(diǎn)。先上結(jié)論,如果對(duì)某種實(shí)現(xiàn)方式有興趣的話可以選擇性的閱讀。
1. 結(jié)論
| 實(shí)現(xiàn)方式 | 優(yōu)點(diǎn) | 缺點(diǎn) | 薦語(yǔ) |
|---|---|---|---|
| 元類 | 線程安全,靈活 | 實(shí)現(xiàn)復(fù)雜 | 適合需要靈活性和線程安全的場(chǎng)景 |
threading.Lock | 線程安全,實(shí)現(xiàn)簡(jiǎn)單 | 需要使用線程鎖 | 適合需要簡(jiǎn)單實(shí)現(xiàn)的場(chǎng)景 |
| 模塊 | 簡(jiǎn)單易用,線程安全 | 無(wú)法動(dòng)態(tài)創(chuàng)建單例實(shí)例 | 想要簡(jiǎn)單且可以接收靜態(tài)單例場(chǎng)景 |
importlib | 靈活,可動(dòng)態(tài)加載單例實(shí)例 | 需要額外的模塊支持 | 不推薦 |
__new__ 方法 | 簡(jiǎn)單直觀 | 非線程安全 | 不推薦 |
| 裝飾器 | 靈活,可應(yīng)用于多個(gè)類 | 非線程安全 | 不推薦 |
2. 使用元類
2.1 實(shí)現(xiàn)方式
通過(guò)自定義元類來(lái)控制類的創(chuàng)建過(guò)程,確保類只創(chuàng)建一個(gè)實(shí)例。
2.2 示例代碼
class SingletonMeta(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Singleton(metaclass=SingletonMeta):
def __init__(self, value):
self.value = value
# 測(cè)試
s1 = Singleton(10)
s2 = Singleton(20)
print(s1.value) # 輸出: 10
print(s2.value) # 輸出: 10
print(s1 is s2) # 輸出: True
2.3 優(yōu)點(diǎn)
- 線程安全,適合多線程環(huán)境。
- 靈活,可以應(yīng)用于多個(gè)類。
2.4 缺點(diǎn)
- 實(shí)現(xiàn)較為復(fù)雜,不易理解。
3. 使用 threading.Lock 實(shí)現(xiàn)線程安全的單例
3.1 實(shí)現(xiàn)方式
通過(guò) threading.Lock 確保在多線程環(huán)境下只創(chuàng)建一個(gè)實(shí)例。
3.2 示例代碼
import threading
class Singleton:
_instance = None
_lock = threading.Lock()
def __new__(cls, *args, **kwargs):
if not cls._instance:
with cls._lock:
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance
# 測(cè)試
s1 = Singleton()
s2 = Singleton()
print(s1 is s2) # 輸出: True
3.3 優(yōu)點(diǎn)
- 線程安全,適合多線程環(huán)境。
3.4 缺點(diǎn)
- 實(shí)現(xiàn)稍微復(fù)雜。
4. 使用模塊
4.1 實(shí)現(xiàn)方式
在 Python 中,模塊是天然的單例。因?yàn)槟K在第一次導(dǎo)入時(shí)會(huì)被初始化,后續(xù)導(dǎo)入時(shí)會(huì)使用已經(jīng)初始化的實(shí)例。
4.2 示例代碼
# singleton_module.py
class Singleton:
def __init__(self):
self.value = "Singleton Instance"
instance = Singleton()
# 在其他文件中導(dǎo)入
from singleton_module import instance
print(instance.value) # 輸出: Singleton Instance
4.3 優(yōu)點(diǎn)
- 簡(jiǎn)單易用,Python 原生支持。
- 線程安全,無(wú)需額外處理。
4.4 缺點(diǎn)
- 無(wú)法動(dòng)態(tài)創(chuàng)建單例實(shí)例。
5. 使用 importlib 模塊
5.1 實(shí)現(xiàn)方式
通過(guò) importlib 模塊動(dòng)態(tài)導(dǎo)入模塊,確保模塊只被導(dǎo)入一次。
5.2 示例代碼
import importlib
class Singleton:
_instance = None
@staticmethod
def get_instance():
if Singleton._instance is None:
Singleton._instance = importlib.import_module("singleton_module").instance
return Singleton._instance
# 測(cè)試
s1 = Singleton.get_instance()
s2 = Singleton.get_instance()
print(s1 is s2) # 輸出: True
5.3 優(yōu)點(diǎn)
- 靈活,可以動(dòng)態(tài)加載單例實(shí)例。
5.4 缺點(diǎn)
- 需要額外的模塊支持。
6. 使用 __new__ 方法
6.1 實(shí)現(xiàn)方式
通過(guò)重寫類的 __new__ 方法,確保類在創(chuàng)建實(shí)例時(shí)只返回同一個(gè)實(shí)例。
6.2 示例代碼
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance
# 測(cè)試
s1 = Singleton()
s2 = Singleton()
print(s1 is s2) # 輸出: True
6.3 優(yōu)點(diǎn)
- 簡(jiǎn)單直觀,易于理解。
6.4 缺點(diǎn)
- 非線程安全,在多線程環(huán)境下可能會(huì)創(chuàng)建多個(gè)實(shí)例。
7. 使用裝飾器
7.1 實(shí)現(xiàn)方式
通過(guò)裝飾器將類轉(zhuǎn)換為單例類。
7.2 示例代碼
def singleton(cls):
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
@singleton
class MyClass:
def __init__(self, value):
self.value = value
# 測(cè)試
m1 = MyClass(10)
m2 = MyClass(20)
print(m1.value) # 輸出: 10
print(m2.value) # 輸出: 10
print(m1 is m2) # 輸出: True
7.3 優(yōu)點(diǎn)
- 靈活,可以應(yīng)用于多個(gè)類。
7.4 缺點(diǎn)
- 非線程安全,在多線程環(huán)境下可能會(huì)創(chuàng)建多個(gè)實(shí)例。
到此這篇關(guān)于Python使用單例模式創(chuàng)建類的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python 單例模式創(chuàng)建類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pycharm 解決自動(dòng)格式化沖突的設(shè)置操作
這篇文章主要介紹了Pycharm 解決自動(dòng)格式化沖突的設(shè)置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
使用with torch.no_grad():顯著減少測(cè)試時(shí)顯存占用
這篇文章主要介紹了使用with torch.no_grad():顯著減少測(cè)試時(shí)顯存占用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
python 實(shí)現(xiàn)以相同規(guī)律打亂多組數(shù)據(jù)
這篇文章主要介紹了python 實(shí)現(xiàn)以相同規(guī)律打亂多組數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
小白學(xué)Python之實(shí)現(xiàn)OCR識(shí)別
將圖片翻譯成文字一般被稱為光學(xué)文字識(shí)別(Optical Character Recognition,OCR),這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)OCR識(shí)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

