在python中怎么實(shí)現(xiàn)單例模式?下面給大家?guī)砥叻N不同的方法:
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機(jī)域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、安新網(wǎng)站維護(hù)、網(wǎng)站推廣。
一:staticmethod
代碼如下:
class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print('a id=', id(a)) print('b id=', id(b))
該方法的要點(diǎn)是在__init__拋出異常,禁止通過類來實(shí)例化,只能通過靜態(tài)get_instance函數(shù)來獲取實(shí)例;因為不能通過類來實(shí)例化,所以靜態(tài)get_instance函數(shù)中可以通過父類object.__new__來實(shí)例化。
二:classmethod
和方法一類似,代碼:
class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(cls): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print('a id=', id(a)) print('b id=', id(b))
該方法的要點(diǎn)是在__init__拋出異常,禁止通過類來實(shí)例化,只能通過靜態(tài)get_instance函數(shù)來獲取實(shí)例;因為不能通過類來實(shí)例化,所以靜態(tài)get_instance函數(shù)中可以通過父類object.__new__來實(shí)例化。
三:類屬性方法
和方法一類似, 代碼:
class Singleton(object): instance = None def __init__(self): raise SyntaxError('can not instance, please use get_instance') def get_instance(): if Singleton.instance is None: Singleton.instance = object.__new__(Singleton) return Singleton.instance a = Singleton.get_instance() b = Singleton.get_instance() print(id(a)) print(id(b))
該方法的要點(diǎn)是在__init__拋出異常,禁止通過類來實(shí)例化,只能通過靜態(tài)get_instance函數(shù)來獲取實(shí)例;因為不能通過類來實(shí)例化,所以靜態(tài)get_instance函數(shù)中可以通過父類object.__new__來實(shí)例化。
四:__new__
常見的方法, 代碼如下:
class Singleton(object): instance = None def __new__(cls, *args, **kw): if not cls.instance: # cls.instance = object.__new__(cls, *args) cls.instance = super(Singleton, cls).__new__(cls, *args, **kw) return cls.instance a = Singleton() b = Singleton() print(id(a)) print(id(b))
五:裝飾器
代碼如下:
def Singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance class MyClass: pass a = MyClass() b = MyClass() c = MyClass() print(id(a)) print(id(b)) print(id(c))
六:元類
python2版:
class Singleton(type): def __init__(cls, name, bases, dct): super(Singleton, cls).__init__(name, bases, dct) cls.instance = None def __call__(cls, *args): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(*args) return cls.instance class MyClass(object): __metaclass__ = Singleton a = MyClass() b = MyClass() c = MyClass() print(id(a)) print(id(b)) print(id(c)) print(a is b) print(a is c)
或者:
class Singleton(type): def __new__(cls, name, bases, attrs): attrs["_instance"] = None return super(Singleton, cls).__new__(cls, name, bases, attrs) def __call__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super(Singleton, cls).__call__(*args, **kwargs) return cls._instance class Foo(object): __metaclass__ = Singleton x = Foo() y = Foo() print(id(x)) print(id(y))
python3版:
class Singleton(type): def __new__(cls, name, bases, attrs): attrs['instance'] = None return super(Singleton, cls).__new__(cls, name, bases, attrs) def __call__(cls, *args, **kwargs): if cls.instance is None: cls.instance = super(Singleton, cls).__call__(*args, **kwargs) return cls.instance class Foo(metaclass=Singleton): pass x = Foo() y = Foo() print(id(x)) print(id(y))
七:名字覆蓋
代碼如下:
class Singleton(object): def foo(self): print('foo') def __call__(self): return self Singleton = Singleton() Singleton.foo() a = Singleton() b = Singleton() print(id(a)) print(id(b))
以上就是在python中怎么實(shí)現(xiàn)單例模式的詳細(xì)內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!