国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python設計模式之單例模式實例

2019-11-25 18:25:55
字體:
來源:轉載
供稿:網友

注:使用的是Python 2.7。

一個簡單實現

復制代碼 代碼如下:

class Foo(object):
    __instance = None
    def __init__(self):
        pass
    @classmethod
    def getinstance(cls):
        if(cls.__instance == None):
            cls.__instance = Foo()
        return cls.__instance

if __name__ == '__main__':
    foo1 = Foo.getinstance()
    foo2 = Foo.getinstance()
    print id(foo1)
    print id(foo2)
    print id(Foo())


輸出的前兩個結果是相同的(id(foo1)與id(foo2)的值相同),第三個結果和前兩個不同。這里類方法getinstance()用于獲取單例,但是類本身也可以實例化,這樣的方式其實并不符合單例模式的要求。但是這樣做也有好處,代碼簡單,大家約定好這樣子調用就行了。但是最好在類的命名上也體現了出來這是一個單例類,例如Foo_singleton。

換一個思路

先說一下init和new的區別:

復制代碼 代碼如下:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'
if __name__ == '__main__':
    foo = Foo()

運行結果是:
復制代碼 代碼如下:

init

而下面的示例:
復制代碼 代碼如下:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'
    def __new__(cls, *args, **kwargs):
        print 'new'

if __name__ == '__main__':
    foo = Foo()


運行結果是:
復制代碼 代碼如下:
new

new是一個類方法,會創建對象時調用。而init方法是在創建完對象后調用,對當前對象的實例做一些一些初始化,無返回值。如果重寫了new而在new里面沒有調用init或者沒有返回實例,那么init將不起作用。以下內容引用自http://docs.python.org/2/reference/datamodel.html#object.new

復制代碼 代碼如下:

If __new__() returns an instance of cls, then the new instance's __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked.


這樣做:
復制代碼 代碼如下:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'

    def __new__(cls, *args, **kwargs):
        print 'new'
        if cls.__instance == None:
            cls.__instance = cls.__new__(cls, *args, **kwargs)
        return cls.__instance

if __name__ == '__main__':
    foo = Foo()

    錯誤如下:

復制代碼 代碼如下:

RuntimeError: maximum recursion depth exceeded in cmp

而這樣也有一樣的錯誤:

復制代碼 代碼如下:

class Foo(object):
    __instance = None
    def __init__(self):
        if self.__class__.__instance == None:
            self.__class__.__instance = Foo()
        print 'init'

if __name__ == '__main__':
    foo = Foo()


該怎么做呢?

下面參考了http://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python/31887#31887:

復制代碼 代碼如下:

class Foo(object):
    __instance = None
    def __new__(cls, *args, **kwargs):
        print 'hhhhhhhhh'
        if not cls.__instance:
            cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def hi(self):
        print 'hi, world'
        print 'hi, letian'

if __name__ == '__main__':
    foo1 = Foo()
    foo2 = Foo()
    print id(foo1)
    print id(foo2)
    print isinstance(foo1, object)
    print isinstance(foo1, Foo)
    foo1.hi()


運行結果:
復制代碼 代碼如下:

hhhhhhhhh
hhhhhhhhh
39578896
39578896
True
True
hi, world
hi, letian

那么,到底發生了什么,我們先回顧一下super:

復制代碼 代碼如下:

>>> print super.__doc__
super(type) -> unbound super object
super(type, obj) -> bound super object; requires isinstance(obj, type)
super(type, type2) -> bound super object; requires issubclass(type2, type)
Typical use to call a cooperative superclass method:
class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

可以肯定上面的單例模式代碼中的這一行代碼:
復制代碼 代碼如下:

cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)

super(Foo, cls)是object,super(Foo, cls).new方法使用的是object的new方法。我們看一下object.new方法的作用:
復制代碼 代碼如下:

>>> print object.__new__.__doc__
T.__new__(S, ...) -> a new object with type S, a subtype of T

如果是一個繼承鏈

復制代碼 代碼如下:

class Fo(object):
    def __new__(cls, *args, **kwargs):
        print 'hi, i am Fo'
        return  super(Fo, cls).__new__(cls, *args, **kwargs)

class Foo(Fo):
    __instance = None
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            print Foo is cls
            print issubclass(cls, Fo)
            print issubclass(cls, object)
            cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def hi(self):
        print 'hi, world'

if __name__ == '__main__':
    foo1 = Foo()
    foo1.hi()
    print isinstance(foo1, Foo)
    print isinstance(foo1, Fo)
    print isinstance(foo1, object)


運行結果如下:
復制代碼 代碼如下:

True
True
True
hi, i am Fo
hi, world
True
True
True

如果如下定義Fo,也正常運行:
復制代碼 代碼如下:

class Fo(object):
    pass

但是,若這樣定義:
復制代碼 代碼如下:

class Fo(object):
    def __new__(cls, *args, **kwargs):
        print 'hi, i am Fo'

運行時報錯如下:
復制代碼 代碼如下:

AttributeError: 'NoneType' object has no attribute 'hi'

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永昌县| 阿拉尔市| 巴塘县| 闸北区| 静海县| SHOW| 南丹县| 墨玉县| 纳雍县| 光泽县| 泾阳县| 江安县| 山东省| 明星| 扶余县| 石景山区| 平原县| 鹰潭市| 平潭县| 胶南市| 桃园县| 武功县| 淮滨县| 辛集市| 西昌市| 锦屏县| 开平市| 隆德县| 无为县| 舒城县| 淮安市| 乐平市| 通州市| 宝清县| 即墨市| 祁连县| 光山县| 阿坝县| 炎陵县| 霍林郭勒市| 荔浦县|