單例的實現(xiàn)方式:
1、基于類
#encoding=utf-8
class Singleton(object):
def __init__(self):
pass
@classmethod
def instance(cls, *args, **kwargs):
if not hasattr(Singleton, "_instance"):
Singleton._instance = Singleton(*args, **kwargs)
return Singleton._instance
s1 = Singleton.instance()
s2 = Singleton.instance()
print(s1 is s2)
支持多線程:
#encoding=utf-8
import threading
class Singleton(object):
_instance_lock = threading.Lock()
def __init__(self):
pass
@classmethod
def instance(cls, *args, **kwargs):
if not hasattr(Singleton, "_instance"):#
with Singleton._instance_lock:#枷鎖
if not hasattr(Singleton, "_instance"):
Singleton._instance = Singleton(*args, **kwargs)
return Singleton._instance
def task(arg):
obj = Singleton.instance()
print(obj)
for i in range(10):
t = threading.Thread(target=task,args=(i,))
t.start()
obj = Singleton.instance()
print(obj)
2、利用__new__()
#encoding=utf-8
class Singleton(object):
def __init__(self):
pass
def __new__(cls,*args,**kwargs):
if not hasattr(cls,"_instance"):
cls._instance = object.__new__(cls)
return cls._instance
p1 = Singleton()
p2 = Singleton()
print(p1 is p2)
支持多線程方式
#encoding=utf-8
import threading
class Singleton(object):
_instance_lock = threading.Lock()
def __init__(self):
pass
def __new__(cls,*args,**kwargs):
if not hasattr(cls,"_instance"):
with cls._instance_lock:
if not hasattr(cls,"_instance"):
cls._instance = object.__new__(cls)
return cls._instance
def task():
obj = Singleton()
print(obj)
for i in range(20):
t = threading.Thread(target=task,args=())
t.start()
3、利用裝飾器
#encoding=utf-8
import threading
def Singleton(cls):
_instance = {}
def _singleton(*args,**kwargs):
if cls not in _instance:
_instance[cls] = cls(*args,**kwargs)
return _instance[cls]
return _singleton
@Singleton
class Person(object):#Person = Singleton(Person)
a = 10
def __init__(self,x=0):
self.x = x
p = Person(2)#_singleton(2)
p2 = Person(3)
print(p is p2)
print(p.x)
print(p2.x)
示例1:
#encoding=utf-8
class Person(object):
__instance = None#定義一個類變量,用于綁定實例對象
def __init__(self,name,age):
self.name = name
self.age = age
@classmethod
def get_instance(cls,name,age):
if cls.__instance == None:
cls.__instance =Person(name,age)
return cls.__instance
def get_info(self):
return self.name + ":" + str(self.age)
p1 = Person.get_instance("xxx",20)
p2 = Person.get_instance("yyy",50)
print(p1 is p2)
print(p1.get_info())
print(p2.get_info())
#此種實現(xiàn)方式無法在生成實例后改變實例變量的值
示例2:
#encoding=utf-8
class Singleton(object):
_instance = None
def __new__(cls,a,b,*args,**kwargs):
if cls._instance == None:
cls._instance = super(Singleton,cls).__new__(cls,*args,**kwargs)
return cls._instance
class Person(Singleton):
def __init__(self,name,age):
self.name = name
self.age = age
def getInfo(self):
return "姓名: %s,年齡: %s" %(self.name,self.age)
p1 = Person("張三",20)
print(p1.getInfo())
p2 = Person("李四",20)
print(p1.getInfo())
print(p1 is p2)
print("類變量: ",Person._instance)
示例3:
#encoding=utf-8
class Person(object):
__instance = None#創(chuàng)建一個類變量用戶存儲類的實例對象
def __init__(self,name,age):
self.name = name
self.age = age
def __new__(cls,name,age):#自定義Person自己的__new__()方法用于產(chǎn)生實例對象,此處需要傳入name,age兩個參數(shù)
if cls.__instance == None:#保證實例對象__instance只會被賦值一次,如果之前已經(jīng)創(chuàng)建過實例,直接返回之前創(chuàng)建的實例
#cls.__instance = super(Person,cls).__new__(cls)
cls.__instance = object.__new__(cls)#調(diào)用父類的__new__方法創(chuàng)建對象
return cls.__instance#返回實例對象
def getInfo(self):
return "姓名: %s,年齡: %s" %(self.name,self.age)
p1 = Person("h",20)
print(p1.getInfo())
p2 = Person("kkk",30)
print(p1.getInfo())
print(p2.getInfo())
print(p1 is p2)
'''
以上代碼執(zhí)行過程:
p1 = Person("h",20)
首先調(diào)用__new__方法產(chǎn)生實例p1,此時Person的__instance為none所以會創(chuàng)建一個對象,
然后調(diào)用__init__方法,把返回的實例對象__instance傳入self中,并初始實例變量name,age
print(p2.getInfo())
首先調(diào)用__new__方法產(chǎn)生實例p2,此時Person的__instance為不為None,為p1,所以會直接返回對象p1,
然后調(diào)用__init__方法,把返回的實例對象__instance傳入self中,并用新的name,age初始實例變量name,age
'''
如果__init__有其他參數(shù),__new__需要傳入?yún)?shù)
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,化州企業(yè)網(wǎng)站建設,化州品牌網(wǎng)站建設,網(wǎng)站定制,化州網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,化州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。