Python實現單例模式的5種方法

基本介紹

一個對象隻允許被一次創建,一個類隻能創建一個對象,並且提供一個全局訪問點。

單例模式應該是應用最廣泛,實現最簡單的一種創建型模式。

特點:全局唯一,允許更改

優缺點

優點:

  • 避免對資源的多重占用,如寫入文件操作
  • 節省內存
  • 防止命名空間被污染

缺點:

  • 沒有接口,不能繼承,與單一職責原則沖突,一個類應該隻關心內部邏輯,而不關心外面怎麼樣來實例化

Python實現

方式1,元類實現:

class MetaClass(type):
    def __call__(self, *args, **kwargs):
        """
        self : class Singleton
        """
        if not hasattr(self, "ins"):
            insObject = super(__class__, self).__call__(*args, **kwargs)
            setattr(self, "ins", insObject)
        return getattr(self, "ins")


class Singleton(object, metaclass=MetaClass):
    pass


if __name__ == "__main__":
    ins = Singleton()
    print(id(ins))
    ins = Singleton()
    print(id(ins))

方式2,繼承實現:

class ParentClass:
    def __new__(cls, *args, **kwargs) -> object:
        """
        cls : class Singeton
        """
        if not hasattr(cls, "ins"):
            insObject = super(__class__, cls).__new__(cls, *args, **kwargs)
            setattr(cls, "ins", insObject)
        return getattr(cls, "ins")


class Singleton(ParentClass):
    pass


if __name__ == "__main__":
    ins = Singleton()
    print(id(ins))
    ins = Singleton()
    print(id(ins))

方式3,裝飾器實現:

def warpper(clsObject):
    def inner(*args, **kwargs):
        if not hasattr(clsObject, "ins"):
            insObject = clsObject(*args, **kwargs)
            setattr(clsObject, "ins", insObject)
        return getattr(clsObject, "ins")
    return inner


@warpper
class Singleton:
    pass


if __name__ == "__main__":
    ins = Singleton()
    print(id(ins))
    ins = Singleton()
    print(id(ins))

方式4,模塊實現:

- foo.py --> ins = Singleton()
- bar.py --> from foo import ins

方式5,@classmethod實現單例模式:

class Singleton:

    @classmethod
    def getSingletonInstanceObject(cls, *args, **kwargs):
        if not hasattr(cls, "ins"):
            insObject = cls(*args, **kwargs)
            setattr(cls, "ins", insObject)
        return getattr(cls, "ins")


if __name__ == "__main__":
    ins = Singleton.getSingletonInstanceObject()
    print(id(ins))
    ins = Singleton.getSingletonInstanceObject()
    print(id(ins))

到此這篇關於Python實現單例模式的5種方法的文章就介紹到這瞭,更多相關Python 單例模式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: