DAY 13. 單例設計
13.1 什么是單例設計
一個類每次實例化返回的都是同一個對象,這種設計模式叫做單例設計,這個類叫做單例類
13.2 實現單例設計的方法
13.2.1 重寫__new__()
class Foo:def __new__(cls,*args, **kwargs):# 如果是第一次實例化,返回一個新對象if not hasattr(cls, '_object'):cls._object = super().__new__(cls)return cls._objectdef __init__(self, name):self.name = namedef Print(self):print(f'The name of this object is: {self.name}')if __name__ == '__main__':foo1 = Foo('foo1')foo2 = Foo('foo2')foo3 = Foo('foo3')foo1.Print() # The name of this object is: foo3foo2.Print() # The name of this object is: foo3foo3.Print() # The name of this object is: foo3
13.2.2 使用裝飾器
def singleton(cls):singleton_dict = {}def close(*args, **kwargs):# 利用字典的setdefault()方法,如果第一次實例化就加入字典,以后每次都返回這個對象return singleton_dict.setdefault('obj',cls(*args, **kwargs))return close@singleton
class MyClass:passif __name__ == '__main__':foo1 = MyClass()foo2 = MyClass()print(foo1) # <__main__.MyClass object at 0x000001DF618C8940>print(foo2) # <__main__.MyClass object at 0x000001DF618C8940>
13.2.3 使用模塊
用import導入的模塊就是天然的單例模式,如果想要實現一個單例類,不妨把它作為一個獨立的模塊,使用時導入由他實例化出來的對象
# mysingleton.py
class My_Singleton(object):def foo(self):passmy_singleton = My_Singleton()# to use
from mysingleton import my_singletonmy_singleton.foo()
13.2.4 共享屬性
共享屬性是指所有由“單例類”實例化出來的對象都共享“同一份屬性”,也就是所有對象的__dict__
都指向同一個字典,但這樣嚴格來說并不算真的單例模式,因為這樣還是有多個實例對象,但其行為確實和單例模式一樣
class Foo:_mydict = {}def __new__(cls, *args, **kwargs):ob = super().__new__(cls)ob.__dict__ = cls._mydictreturn obif __name__ == '__main__':foo1 = Foo()foo2 = Foo()foo1.name = 'foo1'print(foo2.name) # foo1# foo1 和 foo2 并不是同一個對象,只不過他們的方法和屬性公用同一塊內存print(foo1) # <__main__.Foo object at 0x0000023ADA4A8A90>print(foo2) # <__main__.Foo object at 0x0000023ADA4A8AC8>
13.3 總結
- 什么是單例模式
- 單例,即單個實例,一個類在實例化的過程中始終返回同一個實例
- 實現單例模式的四種方法
- 重寫
__new__(cls)
方法,該方法是一個類方法,用來為實例開辟內存空間,返回值是實例化對象,通過重寫該方法,可以使每次實例化都返回同一個對象 - 修飾器方法:與之前修飾器那一節的斐波那契數列類似,判斷字典中有沒有對象,有的話就直接返回,沒有才實例化
- 模塊:這是最簡單的方法,直接導入實例化對象
- 共享屬性:通過把所有對象的
__dict__
指向同一塊內存空間來實現,雖然不是“一個實例”,但其行為與“一個實例”一樣
- 重寫