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)
相關(guān)文章
Python模擬登陸網(wǎng)頁(yè)的三種方法小結(jié)
如何使用Python模擬登陸網(wǎng)頁(yè),尤其是在涉及到復(fù)雜的認(rèn)證機(jī)制時(shí)?這篇文章將詳細(xì)介紹Python模擬登陸網(wǎng)頁(yè)的三種方法,以及如何繞過(guò)一些常見的安全防護(hù)措施,需要的朋友可以參考下2024-01-01
pytorch DistributedDataParallel 多卡訓(xùn)練結(jié)果變差的解決方案
這篇文章主要介紹了pytorch DistributedDataParallel 多卡訓(xùn)練結(jié)果變差的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python實(shí)現(xiàn)輸入二叉樹的先序和中序遍歷,再輸出后序遍歷操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)輸入二叉樹的先序和中序遍歷,再輸出后序遍歷操作,涉及Python基于先序遍歷和中序遍歷構(gòu)造二叉樹,再后序遍歷輸出相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
Python基礎(chǔ)知識(shí)學(xué)習(xí)之類的繼承
今天帶大家學(xué)習(xí)Python的基礎(chǔ)知識(shí),文中對(duì)python類的繼承作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Python設(shè)計(jì)模式中的結(jié)構(gòu)型適配器模式
這篇文章主要介紹了Python設(shè)計(jì)中的結(jié)構(gòu)型適配器模式,適配器模式即Adapter?Pattern,將一個(gè)類的接口轉(zhuǎn)換成為客戶希望的另外一個(gè)接口,下文內(nèi)容具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-02-02
Python 專題二 條件語(yǔ)句和循環(huán)語(yǔ)句的基礎(chǔ)知識(shí)
本文主要介紹了Python條件語(yǔ)句和循環(huán)語(yǔ)句的基礎(chǔ)知識(shí)。主要內(nèi)容包括: 1.條件語(yǔ)句:包括單分支、雙分支和多分支語(yǔ)句,if-elif-else;2.循環(huán)語(yǔ)句:while的使用及簡(jiǎn)單網(wǎng)絡(luò)刷博器爬蟲;3.循環(huán)語(yǔ)句:for的使用及遍歷列表、元組、文件和字符串。2017-03-03
python 中的collections.OrderedDict() 用法
這篇文章主要介紹了python 中的collections.OrderedDict() 用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
pytorch?transforms圖像增強(qiáng)實(shí)現(xiàn)方法
這篇文章主要介紹了pytorch?transforms圖像增強(qiáng)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

