常見的在Python中實(shí)現(xiàn)單例模式的三種方法
作者:吳文苑
這篇文章主要介紹了常見的在Python中實(shí)現(xiàn)單例模式的三種方法,單例模式在各個(gè)編程語言的學(xué)習(xí)中都是需要掌握的基礎(chǔ)知識(shí),需要的朋友可以參考下
單例模式是一種常用的軟件設(shè)計(jì)模式。在它的核心結(jié)構(gòu)中只包含一個(gè)被稱為單例類的特殊類。通過單例模式可以保證系統(tǒng)中一個(gè)類只有一個(gè)實(shí)例而且該實(shí)例易于外界訪問,從而方便對實(shí)例個(gè)數(shù)的控制并節(jié)約系統(tǒng)資源。如果希望在系統(tǒng)中某個(gè)類的對象只能存在一個(gè),單例模式是最好的解決方案。
單例模式的要點(diǎn)有三個(gè);一是某個(gè)類只能有一個(gè)實(shí)例;二是它必須自行創(chuàng)建這個(gè)實(shí)例;三是它必須自行向整個(gè)系統(tǒng)提供這個(gè)實(shí)例。在Python中,單例模式有以下幾種實(shí)現(xiàn)方式。
方法一、實(shí)現(xiàn)__new__方法,然后將類的一個(gè)實(shí)例綁定到類變量_instance上;如果cls._instance為None,則說明該類還沒有被實(shí)例化過,new一個(gè)該類的實(shí)例,并返回;如果cls._instance不為None,直接返回_instance,代碼如下:
class Singleton(object): def __new__(cls, *args, **kwargs): if not hasattr(cls, '_instance'): orig = super(Singleton, cls) cls._instance = orig.__new__(cls, *args, **kwargs) return cls._instance class MyClass(Singleton): a = 1 one = MyClass() two = MyClass() #one和two完全相同,可以用id(), ==, is檢測 print id(one) # 29097904 print id(two) # 29097904 print one == two # True print one is two # True
方法二、本質(zhì)上是方法一的升級(jí)版,使用__metaclass__(元類)的高級(jí)python用法,具體代碼如下:
class Singleton2(type): def __init__(cls, name, bases, dict): super(Singleton2, cls).__init__(name, bases, dict) cls._instance = None def __call__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super(Singleton2, cls).__call__(*args, **kwargs) return cls._instance class MyClass2(object): __metaclass__ = Singleton2 a = 1 one = MyClass2() two = MyClass2() print id(one) # 31495472 print id(two) # 31495472 print one == two # True print one is two # True
方法三、使用Python的裝飾器(decorator)實(shí)現(xiàn)單例模式,這是一種更Pythonic的方法;單利類本身的代碼不是單例的,通裝飾器使其單例化,代碼如下:
def singleton(cls, *args, **kwargs): instances = {} def _singleton(): if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return _singleton @singleton class MyClass3(object): a = 1 one = MyClass3() two = MyClass3() print id(one) # 29660784 print id(two) # 29660784 print one == two # True print one is two # True