成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

在python中怎么實現(xiàn)單例模式-創(chuàng)新互聯(lián)

在python中怎么實現(xiàn)單例模式?下面給大家?guī)砥叻N不同的方法:

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、綏化網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5網(wǎng)站設計、購物商城網(wǎng)站建設、集團公司官網(wǎng)建設、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為綏化等各大城市提供網(wǎng)站開發(fā)制作服務。

一: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))

該方法的要點是在__init__拋出異常,禁止通過類來實例化,只能通過靜態(tài)get_instance函數(shù)來獲取實例;因為不能通過類來實例化,所以靜態(tài)get_instance函數(shù)中可以通過父類object.__new__來實例化。

二: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))

該方法的要點是在__init__拋出異常,禁止通過類來實例化,只能通過靜態(tài)get_instance函數(shù)來獲取實例;因為不能通過類來實例化,所以靜態(tài)get_instance函數(shù)中可以通過父類object.__new__來實例化。

三:類屬性方法

和方法一類似, 代碼:

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))

該方法的要點是在__init__拋出異常,禁止通過類來實例化,只能通過靜態(tài)get_instance函數(shù)來獲取實例;因為不能通過類來實例化,所以靜態(tài)get_instance函數(shù)中可以通過父類object.__new__來實例化。

四:__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中怎么實現(xiàn)單例模式的詳細內(nèi)容,更多請關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司其它相關文章!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享名稱:在python中怎么實現(xiàn)單例模式-創(chuàng)新互聯(lián)
URL分享:http://jinyejixie.com/article46/cceohg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、App設計小程序開發(fā)、定制網(wǎng)站企業(yè)網(wǎng)站制作、虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設
高阳县| 常熟市| 汉川市| 定安县| 拜泉县| 巴中市| 香格里拉县| 西城区| 苍梧县| 墨竹工卡县| 新绛县| 鸡泽县| 遂溪县| 武鸣县| 耒阳市| 沂水县| 疏勒县| 沐川县| 盱眙县| 西青区| 仁布县| 霍山县| 沙河市| 常州市| 泸西县| 西盟| 绵竹市| 惠安县| 南郑县| 赣州市| 达尔| 方正县| 抚州市| 奉节县| 晋州市| 金山区| 岳阳市| 桂阳县| 梁平县| 都江堰市| 普宁市|