一、基于装饰器实现单例模式
#装饰器实现单例模式
def singleTon(cls):
def wrapper(*args,**kwargs):
if not hasattr(cls,'_instance'):
cls._instance = cls(*args,**kwargs)
return cls._instance
return wrapper
@singleTon
class SingleTonTest:
pass
s1 = SingleTonTest()
s2 = SingleTonTest()
print(s1) #<__main__.SingleTonTest object at 0x0000000000656860>
print(s2) #<__main__.SingleTonTest object at 0x0000000000656860>
二、基于元类实现单例模式
class MyType(type):
def __call__(self, *args, **kwargs):
"""
self指的是SingleTonTest类
:param args:
:param kwargs:
:return:
"""
if not hasattr(self,'_instance'):
self._instance = super(MyType,self).__call__(*args,**kwargs)
return self._instance
class SingleTonTest(metaclass=MyType):
pass
#SingleTonTest是元类MyType的对象,对象加括号执行元类中的__call__方法
s1 = SingleTonTest()
s2 = SingleTonTest()
print(s1) #<__main__.SingleTonTest object at 0x0000000000646898>
print(s2) #<__main__.SingleTonTest object at 0x0000000000646898>
三、基于__new__
方法实现单例模式
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class SingleTonTest:
def __new__(cls, *args, **kwargs):
"""
cls:<class '__main__.SingleTonTest'>
:param args:
:param kwargs:
:return:
"""
if not hasattr(cls,'_instance'):
cls._instance = super(SingleTonTest,cls).__new__(cls, *args, **kwargs)
return cls._instance
s1 = SingleTonTest()
s2 = SingleTonTest()
print(s1) #<__main__.SingleTonTest object at 0x0000000000BD68D0>
print(s2) #<__main__.SingleTonTest object at 0x0000000000BD68D0>
四、基于静态方法实现单例模式
class SingleTonTest:
@classmethod
def getInstance(cls):
if not hasattr(cls,'_instance'):
cls._instance = cls()
return cls._instance
s1 = SingleTonTest.getInstance()
s2 = SingleTonTest.getInstance()
print(s1) #<__main__.SingleTonTest object at 0x0000000000646828>
print(s2) #<__main__.SingleTonTest object at 0x0000000000646828>
五、基于模块实现单例模式
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class SingleTonTest:
pass
s = SingleTonTest()
#在另一个py文件中导入该类对象
from base import s
print(s)
六、Monostate单例模式
将所有创建对象的__dict__
属性指向同一个字典,这样所有的对象实现了数据的共享。
class Borg:
__shared_state={}
def __new__(cls, *args, **kwargs):
obj = super(Borg,cls).__new__(cls, *args, **kwargs)
obj.__dict__ = cls.__shared_state
return obj
#共享同一个字典,一个对象改变属性,另一个也跟着改变
b1 = Borg()
b1.name = 'Bright'
b2 = Borg()
print(b1.__dict__) #{'name': 'Bright'}
print(b2.__dict__) #{'name': 'Bright'}
将__shared_state
变量作为一个中间变量,并且赋值给python中存储类所有对象状态的__dict__
变量,这在实例后,会产生两个不同的对象,但是b1.__dict__
和b2.__dict__
都指向了__shared_state
的内存地址。
注意,这种单例模式是数据的单例,对象还是两个不一样的对象。