Python中單例模式總結(jié)
一、單例模式
a、單例模式分為四種:文件,類,基于__new__方法實(shí)現(xiàn)單例模式,基于metaclass方式實(shí)現(xiàn)
b、類實(shí)現(xiàn)如下:
class Sigletion(objects): import time def __init__(self): time.sleep(1) @classmethod def instance(cls,*args,**kwargs) if not hasattr(Sigletion,'_instance'): Sigletion._instance=Sigletion(*args,**kwargs) return Sigletion._instance import threading daf task(arg): obj=Sigletion.instance() print(obj) for i in range(10): t=threading.Thread(target=task,args=[i,]) t.start()
c、基于__new__方法實(shí)現(xiàn)單例模式
import time import threading class Singleton(object): _instance_lock=threading.Lock() def __init__(self): pass def __new__(cls, *args, **kwargs): if not hasattr(Singleton,"_instance"): with Singleton._instance_lock: if not hasattr(Singleton,"_instance"): Singleton._instance=object.__new__(cls,*args,**kwargs) return Singleton._instance obj1=Singleton() obj2=Singleton() print(obj1,obj2) def task(arg): obj = Singleton() print(obj) for i in range(10): t = threading.Thread(target=task,args=[i,]) t.start()
d、基于metaclass方式實(shí)現(xiàn)單例模式
"""
1.對(duì)象是類創(chuàng)建,創(chuàng)建對(duì)象時(shí)候類的__init__方法自動(dòng)執(zhí)行,對(duì)象()執(zhí)行類的 __call__ 方法
2.類是type創(chuàng)建,創(chuàng)建類時(shí)候type的__init__方法自動(dòng)執(zhí)行,類() 執(zhí)行type的 __call__方法(類的__new__方法,類的__init__方法)
# 第0步: 執(zhí)行type的 __init__ 方法【類是type的對(duì)象】 class Foo: def __init__(self): pass def __call__(self, *args, **kwargs): pass # 第1步: 執(zhí)行type的 __call__ 方法 # 1.1 調(diào)用 Foo類(是type的對(duì)象)的 __new__方法,用于創(chuàng)建對(duì)象。 # 1.2 調(diào)用 Foo類(是type的對(duì)象)的 __init__方法,用于對(duì)對(duì)象初始化。 obj = Foo() # 第2步:執(zhí)行Foodef __call__ 方法 obj() """ import threading class SingletonType(type): _instace_lock=threading.Lock() def __call__(cls, *args, **kwargs): if not hasattr(cls, "_instance"): with SingletonType._instace_lock: if not hasattr(cls, "_instance"): cls._instance = super(SingletonType,cls).__call__(*args, **kwargs) return cls._instance class Foo(metaclass=SingletonType): def __init__(self,name): self.name=name obj1 = Foo('name') obj2 = Foo('name') print(obj1,obj2)
- python 實(shí)現(xiàn)單例模式的5種方法
- Python單例模式的四種創(chuàng)建方式實(shí)例解析
- python單例模式原理與創(chuàng)建方法實(shí)例分析
- python單例模式的多種實(shí)現(xiàn)方法
- 簡(jiǎn)單了解python單例模式的幾種寫法
- 聊聊python里如何用Borg pattern實(shí)現(xiàn)的單例模式
- Python下簡(jiǎn)易的單例模式詳解
- Python中實(shí)現(xiàn)單例模式的n種方式和原理
- python單例模式獲取IP代理的方法詳解
- Python單例模式實(shí)例詳解
- python 6種方法實(shí)現(xiàn)單例模式
相關(guān)文章
Python multiprocessing.Manager介紹和實(shí)例(進(jìn)程間共享數(shù)據(jù))
這篇文章主要介紹了Python multiprocessing.Manager介紹和實(shí)例(進(jìn)程間共享數(shù)據(jù)),本文介紹了Manager的dict、list使用例子,同時(shí)介紹了namespace對(duì)象,需要的朋友可以參考下2014-11-11python-str,list,set間的轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇python-str,list,set間的轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python如何實(shí)現(xiàn)轉(zhuǎn)換URL詳解
這篇文章主要介紹了Python如何實(shí)現(xiàn)轉(zhuǎn)換URL詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07python?sklearn數(shù)據(jù)預(yù)處理之?dāng)?shù)據(jù)縮放詳解
數(shù)據(jù)的預(yù)處理是數(shù)據(jù)分析,或者機(jī)器學(xué)習(xí)訓(xùn)練前的重要步驟,這篇文章主要為大家詳細(xì)介紹了sklearn數(shù)據(jù)預(yù)處理中數(shù)據(jù)縮放的相關(guān)知識(shí),感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10Python 窗體(tkinter)按鈕 位置實(shí)例
今天小編就為大家分享一篇Python 窗體(tkinter)按鈕 位置實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python語(yǔ)音識(shí)別實(shí)踐之百度語(yǔ)音API
這篇文章主要為大家詳細(xì)介紹了python語(yǔ)音識(shí)別實(shí)踐之百度語(yǔ)音API,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08